Skip to content

Commit

Permalink
EZP-29290: As an Editor, I want to have preconfigured "Author" sectio…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
konradoboza authored and Łukasz Serwatka committed Aug 29, 2018
1 parent 4f69c2e commit 2f1493c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
15 changes: 15 additions & 0 deletions bundle/Resources/translations/ezrepoforms_content_type.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@
<target>Description</target>
<note>key: field_definition.description</note>
</trans-unit>
<trans-unit id="f64d0d1be6b62d75f7c77bf08ec560abd3addfb4" resname="field_definition.ezauthor.default_user_current">
<source>Current User</source>
<target>Current User</target>
<note>key: field_definition.ezauthor.default_user_current</note>
</trans-unit>
<trans-unit id="904471ac6da01e95b90c1f0f8ec7740b59931e6b" resname="field_definition.ezauthor.default_user_empty">
<source>Empty</source>
<target>Empty</target>
<note>key: field_definition.ezauthor.default_user_empty</note>
</trans-unit>
<trans-unit id="60b0ee9222a502c38adf1be704d4984564190e8e" resname="field_definition.ezauthor.default_author">
<source>Default value</source>
<target>Default value</target>
<note>key: field_definition.ezauthor.default_author</note>
</trans-unit>
<trans-unit id="41f781284e4bae2abbb6cfdf1126cc90b48dd35b" resname="field_definition.ezbinaryfile.max_file_size">
<source>Maximum file size (MB)</source>
<target>Maximum file size (MB)</target>
Expand Down
8 changes: 8 additions & 0 deletions bundle/Resources/views/ContentType/field_types.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@
</div>
{% endblock %}

{% block ezauthor_field_definition_edit %}
<div class="ezauthor-settings default-author{% if group_class is not empty %} {{ group_class }}{% endif %}">
{{- form_label(form.defaultAuthor) -}}
{{- form_errors(form.defaultAuthor) -}}
{{- form_widget(form.defaultAuthor) -}}
</div>
{% endblock %}

{% block ezselection_field_definition_edit %}
<div class="ezselection-settings is-multiple{% if group_class is not empty %} {{ group_class }}{% endif %}">
{{- form_label(form.isMultiple) -}}
Expand Down
37 changes: 34 additions & 3 deletions lib/FieldType/Mapper/AuthorFormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
])
Expand Down
29 changes: 27 additions & 2 deletions lib/Form/Type/FieldType/AuthorFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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
*/
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit 2f1493c

Please sign in to comment.