Skip to content

Commit

Permalink
EZP-27638: Implement editing support for Keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Kobus committed Sep 19, 2017
1 parent d4e2f2c commit 144d4bb
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bundle/Resources/config/services.yml
Expand Up @@ -40,6 +40,7 @@ parameters:
ezrepoforms.field_type.form_mapper.ezuser.class: EzSystems\RepositoryForms\FieldType\Mapper\UserAccountFieldValueFormMapper
ezrepoforms.field_type.form_mapper.ezurl.class: EzSystems\RepositoryForms\FieldType\Mapper\UrlFormMapper
ezrepoforms.field_type.form_mapper.ezgmaplocation.class: EzSystems\RepositoryForms\FieldType\Mapper\MapLocationFormMapper
ezrepoforms.field_type.form_mapper.ezkeyword.class: EzSystems\RepositoryForms\FieldType\Mapper\KeywordFormMapper

ezrepoforms.validator.unique_content_type_identifier.class: EzSystems\RepositoryForms\Validator\Constraints\UniqueContentTypeIdentifierValidator
ezrepoforms.validator.validator_configuration.class: EzSystems\RepositoryForms\Validator\Constraints\ValidatorConfigurationValidator
Expand Down Expand Up @@ -295,6 +296,12 @@ services:
tags:
- { name: ez.fieldFormMapper.value, fieldType: ezgmaplocation }

ezrepoforms.field_type.form_mapper.ezkeyword:
class: "%ezrepoforms.field_type.form_mapper.ezkeyword.class%"
arguments: ["@ezpublish.api.service.field_type"]
tags:
- { name: ez.fieldFormMapper.value, fieldType: ezkeyword }

# Validators
ezrepoforms.validator.unique_content_type_identifier:
class: "%ezrepoforms.validator.unique_content_type_identifier.class%"
Expand Down
38 changes: 38 additions & 0 deletions lib/FieldType/DataTransformer/KeywordValueTransformer.php
@@ -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);
}
}
52 changes: 52 additions & 0 deletions lib/FieldType/Mapper/KeywordFormMapper.php
@@ -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',
]);
}
}

0 comments on commit 144d4bb

Please sign in to comment.