forked from ezsystems/repository-forms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-27638: Implement editing support for Keyword
- Loading branch information
Maciej Kobus
committed
Sep 19, 2017
1 parent
d4e2f2c
commit 144d4bb
Showing
3 changed files
with
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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\Keyword\Value; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
|
||
/** | ||
* DataTransformer for Keyword\Value. | ||
*/ | ||
class KeywordValueTransformer implements DataTransformerInterface | ||
{ | ||
public function transform($value) | ||
{ | ||
if (!$value instanceof Value) { | ||
return null; | ||
} | ||
|
||
return implode(', ', $value->values); | ||
} | ||
|
||
public function reverseTransform($value) | ||
{ | ||
if (empty($value)) { | ||
return null; | ||
} | ||
|
||
return new Value($value); | ||
} | ||
} |
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,52 @@ | ||
<?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\DataTransformer\KeywordValueTransformer; | ||
use EzSystems\RepositoryForms\FieldType\FieldValueFormMapperInterface; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* FormMapper for ezkeyword FieldType. | ||
*/ | ||
class KeywordFormMapper implements FieldValueFormMapperInterface | ||
{ | ||
public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data) | ||
{ | ||
$fieldDefinition = $data->fieldDefinition; | ||
$formConfig = $fieldForm->getConfig(); | ||
|
||
$fieldForm | ||
->add( | ||
$formConfig->getFormFactory()->createBuilder() | ||
->create( | ||
'value', | ||
TextType::class, | ||
[ | ||
'required' => $fieldDefinition->isRequired, | ||
'label' => $fieldDefinition->getName($formConfig->getOption('languageCode')), | ||
] | ||
) | ||
->addModelTransformer(new KeywordValueTransformer()) | ||
->setAutoInitialize(false) | ||
->getForm() | ||
); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver | ||
->setDefaults([ | ||
'translation_domain' => 'ezrepoforms_content_type', | ||
]); | ||
} | ||
} |