Maximum file size (MB)
diff --git a/bundle/Resources/views/ContentType/field_types.html.twig b/bundle/Resources/views/ContentType/field_types.html.twig
index edeeb6b4e..77119bab4 100644
--- a/bundle/Resources/views/ContentType/field_types.html.twig
+++ b/bundle/Resources/views/ContentType/field_types.html.twig
@@ -212,6 +212,14 @@
{% endblock %}
+{% block ezauthor_field_definition_edit %}
+
+ {{- form_label(form.defaultAuthor) -}}
+ {{- form_errors(form.defaultAuthor) -}}
+ {{- form_widget(form.defaultAuthor) -}}
+
+{% endblock %}
+
{% block ezselection_field_definition_edit %}
{{- form_label(form.isMultiple) -}}
diff --git a/lib/FieldType/Mapper/AuthorFormMapper.php b/lib/FieldType/Mapper/AuthorFormMapper.php
index a74169222..819445087 100644
--- a/lib/FieldType/Mapper/AuthorFormMapper.php
+++ b/lib/FieldType/Mapper/AuthorFormMapper.php
@@ -8,24 +8,54 @@
*/
namespace EzSystems\RepositoryForms\FieldType\Mapper;
+use eZ\Publish\Core\FieldType\Author\Type;
use EzSystems\RepositoryForms\Data\Content\FieldData;
+use EzSystems\RepositoryForms\Data\FieldDefinitionData;
+use EzSystems\RepositoryForms\FieldType\FieldDefinitionFormMapperInterface;
use EzSystems\RepositoryForms\FieldType\FieldValueFormMapperInterface;
use EzSystems\RepositoryForms\Form\Type\FieldType\AuthorFieldType;
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* FormMapper for ezauthor FieldType.
*/
-class AuthorFormMapper implements FieldValueFormMapperInterface
+class AuthorFormMapper implements FieldDefinitionFormMapperInterface, FieldValueFormMapperInterface
{
/**
- * @param FormInterface $fieldForm
- * @param FieldData $data
+ * @param \Symfony\Component\Form\FormInterface $fieldDefinitionForm
+ * @param \EzSystems\RepositoryForms\Data\FieldDefinitionData $data
+ */
+ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
+ {
+ $fieldDefinitionForm
+ ->add(
+ 'defaultAuthor',
+ ChoiceType::class,
+ [
+ 'choices' => [
+ 'field_definition.ezauthor.default_user_empty' => Type::DEFAULT_VALUE_EMPTY,
+ 'field_definition.ezauthor.default_user_current' => Type::DEFAULT_CURRENT_USER,
+ ],
+ 'choices_as_values' => true,
+ 'expanded' => true,
+ 'required' => true,
+ 'property_path' => 'fieldSettings[defaultAuthor]',
+ 'label' => 'field_definition.ezauthor.default_author',
+ 'translation_domain' => 'ezrepoforms_content_type',
+ ]
+ );
+ }
+
+ /**
+ * @param \Symfony\Component\Form\FormInterface $fieldForm
+ * @param \EzSystems\RepositoryForms\Data\Content\FieldData $data
*/
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
{
$fieldDefinition = $data->fieldDefinition;
+ $fieldSettings = $fieldDefinition->getFieldSettings();
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);
@@ -34,6 +64,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
->add(
$formConfig->getFormFactory()->createBuilder()
->create('value', AuthorFieldType::class, [
+ 'default_author' => $fieldSettings['defaultAuthor'],
'required' => $fieldDefinition->isRequired,
'label' => $label,
])
diff --git a/lib/Form/Type/FieldType/AuthorFieldType.php b/lib/Form/Type/FieldType/AuthorFieldType.php
index 0362bf29d..794825da1 100644
--- a/lib/Form/Type/FieldType/AuthorFieldType.php
+++ b/lib/Form/Type/FieldType/AuthorFieldType.php
@@ -8,6 +8,7 @@
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Repository;
+use eZ\Publish\Core\FieldType\Author\Type as AuthorType;
use eZ\Publish\Core\FieldType\Author\Author;
use eZ\Publish\Core\FieldType\Author\Value;
use EzSystems\RepositoryForms\Form\Type\FieldType\Author\AuthorCollectionType;
@@ -18,6 +19,8 @@
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
+use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormView;
/**
* Form Type representing ezauthor field type.
@@ -27,6 +30,9 @@ class AuthorFieldType extends AbstractType
/** @var \eZ\Publish\API\Repository\Repository */
private $repository;
+ /** @var int */
+ private $defaultAuthor;
+
/**
* @param \eZ\Publish\API\Repository\Repository $repository
*/
@@ -57,18 +63,33 @@ public function getBlockPrefix()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
+ $this->defaultAuthor = $options['default_author'];
+
$builder
->add('authors', AuthorCollectionType::class, [])
->addViewTransformer($this->getViewTransformer())
->addEventListener(FormEvents::POST_SUBMIT, [$this, 'filterOutEmptyAuthors']);
}
+ /**
+ * @param \Symfony\Component\Form\FormView $view
+ * @param \Symfony\Component\Form\FormInterface $form
+ * @param array $options
+ */
+ public function buildView(FormView $view, FormInterface $form, array $options)
+ {
+ $view->vars['attr']['default-author'] = $options['default_author'];
+ }
+
/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
- $resolver->setDefaults(['data_class' => Value::class]);
+ $resolver->setDefaults([
+ 'data_class' => Value::class,
+ 'default_author' => AuthorType::DEFAULT_VALUE_EMPTY,
+ ])->setAllowedTypes('default_author', 'integer');
}
/**
@@ -80,7 +101,11 @@ public function getViewTransformer(): DataTransformerInterface
{
return new CallbackTransformer(function (Value $value) {
if (0 === $value->authors->count()) {
- $value->authors->append($this->fetchLoggedAuthor());
+ if ($this->defaultAuthor === AuthorType::DEFAULT_CURRENT_USER) {
+ $value->authors->append($this->fetchLoggedAuthor());
+ } else {
+ $value->authors->append(new Author());
+ }
}
return $value;