-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-27631: Implement editing support for Author FieldType
- Loading branch information
Maciej Kobus
committed
Sep 8, 2017
1 parent
ea812cd
commit cf0f328
Showing
7 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the eZ RepositoryForms package. | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
* @version //autogentag// | ||
*/ | ||
namespace EzSystems\RepositoryForms\FieldType\DataTransformer; | ||
|
||
use eZ\Publish\Core\FieldType\Author\Author; | ||
use eZ\Publish\Core\FieldType\Author\Value; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
|
||
/** | ||
* DataTransformer for Author\Value. | ||
*/ | ||
class AuthorValueTransformer implements DataTransformerInterface | ||
{ | ||
public function transform($value) | ||
{ | ||
if (is_array($value)) { | ||
return $value; | ||
} | ||
|
||
if (!$value instanceof Value || $value->authors->count() == 0) { | ||
return [[]]; | ||
} | ||
|
||
$authors = []; | ||
foreach ($value->authors as $author) { | ||
$authors[] = [ | ||
'id' => $author->id, | ||
'name' => $author->name, | ||
'email' => $author->email, | ||
]; | ||
} | ||
|
||
return $authors; | ||
} | ||
|
||
public function reverseTransform($value) | ||
{ | ||
if ($value === null || !is_array($value)) { | ||
return null; | ||
} | ||
|
||
$authors = []; | ||
foreach ($value as $authorProperties) { | ||
$authors[] = new Author($authorProperties); | ||
} | ||
|
||
return new Value($authors); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the eZ RepositoryForms package. | ||
* | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\RepositoryForms\FieldType\Mapper; | ||
|
||
use EzSystems\RepositoryForms\Data\Content\FieldData; | ||
use EzSystems\RepositoryForms\FieldType\FieldValueFormMapperInterface; | ||
use EzSystems\RepositoryForms\Form\Type\FieldValue\AuthorsType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* FormMapper for ezauthor FieldType. | ||
*/ | ||
class AuthorFormMapper implements FieldValueFormMapperInterface | ||
{ | ||
/** | ||
* @param FormInterface $fieldForm | ||
* @param FieldData $data | ||
*/ | ||
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data) | ||
{ | ||
$fieldDefinition = $data->fieldDefinition; | ||
$formConfig = $fieldForm->getConfig(); | ||
|
||
$fieldForm | ||
->add( | ||
$formConfig->getFormFactory()->createBuilder() | ||
->create('value', AuthorsType::class, [ | ||
'required' => $fieldDefinition->isRequired, | ||
'label' => $fieldDefinition->getName($formConfig->getOption('languageCode')), | ||
]) | ||
->setAutoInitialize(false) | ||
->getForm() | ||
); | ||
} | ||
|
||
/** | ||
* Fake method to set the translation domain for the extractor. | ||
* | ||
* @param OptionsResolver $resolver | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver | ||
->setDefaults([ | ||
'translation_domain' => 'ezrepoforms_content_type', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\RepositoryForms\Form\Type\FieldValue\Author; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CollectionType; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Combined Type for ezauthor. | ||
*/ | ||
class AuthorCollectionType extends AbstractType | ||
{ | ||
public function getName() | ||
{ | ||
return $this->getBlockPrefix(); | ||
} | ||
|
||
public function getBlockPrefix() | ||
{ | ||
return 'ezrepoforms_fieldtype_ezauthor_authors'; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
parent::configureOptions($resolver); | ||
|
||
$resolver->setDefaults([ | ||
'allow_add' => true, | ||
'allow_delete' => true, | ||
'entry_type' => AuthorEntryType::class, | ||
'prototype' => true, | ||
'prototype_name' => '__index__', | ||
]); | ||
} | ||
|
||
public function getParent() | ||
{ | ||
return CollectionType::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\RepositoryForms\Form\Type\FieldValue\Author; | ||
|
||
use eZ\Publish\Core\FieldType\Author\Author; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Combined entry type for ezauthor. | ||
*/ | ||
class AuthorEntryType extends AbstractType | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->getBlockPrefix(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getBlockPrefix() | ||
{ | ||
return 'ezrepoforms_fieldtype_ezauthor_authors_entry'; | ||
} | ||
|
||
/** | ||
* @param FormBuilderInterface $builder | ||
* @param array $options | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add( | ||
'id', | ||
HiddenType::class, | ||
[ | ||
'label' => 'content.field_type.ezauthor.id', | ||
] | ||
) | ||
->add( | ||
'name', | ||
TextType::class, | ||
[ | ||
'label' => 'content.field_type.ezauthor.name', | ||
'required' => $options['required'], | ||
] | ||
) | ||
->add( | ||
'email', | ||
EmailType::class, | ||
[ | ||
'label' => 'content.field_type.ezauthor.email', | ||
'required' => $options['required'], | ||
] | ||
); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
parent::configureOptions($resolver); | ||
|
||
$resolver->setDefault('data_class', Author::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\RepositoryForms\Form\Type\FieldValue; | ||
|
||
use eZ\Publish\Core\FieldType\Author\Author; | ||
use eZ\Publish\Core\FieldType\Author\Value; | ||
use EzSystems\RepositoryForms\Form\Type\FieldValue\Author\AuthorCollectionType; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Combined Type for ezauthor. | ||
*/ | ||
class AuthorsType extends AbstractType | ||
{ | ||
public function getName() | ||
{ | ||
return $this->getBlockPrefix(); | ||
} | ||
|
||
public function getBlockPrefix() | ||
{ | ||
return 'ezrepoforms_fieldtype_ezauthor'; | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder | ||
->add('authors', AuthorCollectionType::class, []) | ||
->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'addEmptyEntry']); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
parent::configureOptions($resolver); | ||
|
||
$resolver->setDefault('data_class', Value::class); | ||
} | ||
|
||
public function addEmptyEntry(FormEvent $event) | ||
{ | ||
$data = $event->getData(); | ||
$contentStruct = $event->getForm()->getRoot()->getData(); | ||
|
||
if ($contentStruct->isNew()) { | ||
$data->authors->offsetSet(0, new Author(['id' => null, 'name' => null, 'email' => null])); | ||
} | ||
} | ||
} |