From 2f1493c500bc4e609b192910a9fb520b391621c1 Mon Sep 17 00:00:00 2001 From: konradoboza <34310128+konradoboza@users.noreply.github.com> Date: Wed, 29 Aug 2018 09:03:01 +0200 Subject: [PATCH] EZP-29290: As an Editor, I want to have preconfigured "Author" section while creating the content (#251) * EZP-29290: As an Editor, I want to have preconfigured "Author" section while creating the content * EZP-29290: As an Editor, I want to have preconfigured "Author" section while creating the content --- .../ezrepoforms_content_type.en.xlf | 15 ++++++++ .../views/ContentType/field_types.html.twig | 8 ++++ lib/FieldType/Mapper/AuthorFormMapper.php | 37 +++++++++++++++++-- lib/Form/Type/FieldType/AuthorFieldType.php | 29 ++++++++++++++- 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/bundle/Resources/translations/ezrepoforms_content_type.en.xlf b/bundle/Resources/translations/ezrepoforms_content_type.en.xlf index 869f1145f..93598f179 100644 --- a/bundle/Resources/translations/ezrepoforms_content_type.en.xlf +++ b/bundle/Resources/translations/ezrepoforms_content_type.en.xlf @@ -171,6 +171,21 @@ Description key: field_definition.description + + Current User + Current User + key: field_definition.ezauthor.default_user_current + + + Empty + Empty + key: field_definition.ezauthor.default_user_empty + + + Default value + Default value + key: field_definition.ezauthor.default_author + Maximum file size (MB) 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;