Skip to content

Commit

Permalink
Merge pull request ezsystems#151 from webhdx/EZP-27628_richtext
Browse files Browse the repository at this point in the history
EZP-27628: Implement editing support for RichText FieldType
  • Loading branch information
lserwatka committed Sep 20, 2017
2 parents 8113ee3 + c2d6fe5 commit 48c1ff4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
5 changes: 4 additions & 1 deletion bundle/Resources/config/services.yml
Expand Up @@ -229,8 +229,11 @@ services:

ezrepoforms.field_type.form_mapper.ezrichtext:
class: "%ezrepoforms.field_type.form_mapper.ezrichtext.class%"
arguments:
- "@ezpublish.api.service.field_type"
- "@ezpublish.fieldType.ezrichtext.converter.edit.xhtml5"
tags:
- { name: ez.fieldFormMapper.definition, fieldType: ezrichtext }
- { name: ez.fieldFormMapper.value, fieldType: ezrichtext }

ezrepoforms.field_type.form_mapper.ezselection:
class: "%ezrepoforms.field_type.form_mapper.ezselection.class%"
Expand Down
6 changes: 6 additions & 0 deletions doc/bc/changes-1.10.md
Expand Up @@ -3,6 +3,12 @@
Changes affecting version compatibility with former or future versions.

## Changes
- EZP-27628: Implement editing support for RichText FieldType

What changed: `ezrepoforms.field_type.form_mapper.ezrichtext` requires `ezpublish.api.service.field_type` and `ezpublish.fieldType.ezrichtext.converter.edit.xhtml5` dependencies.

How it might affect your code: If you are implementing a subclass, make sure you are injecting required services.

- EZP-27641: Implement editing support for Relation FieldType

What changed: Removed `ezpublish.translation_helper` dependency from `ezrepoforms.field_type.form_mapper.abstractrelation` service.
Expand Down
63 changes: 63 additions & 0 deletions lib/FieldType/DataTransformer/RichTextValueTransformer.php
@@ -0,0 +1,63 @@
<?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\API\Repository\FieldType;
use eZ\Publish\Core\FieldType\RichText\Converter;
use eZ\Publish\Core\FieldType\RichText\Value;
use Symfony\Component\Form\DataTransformerInterface;

/**
* DataTransformer for RichText\Value.
*/
class RichTextValueTransformer implements DataTransformerInterface
{
/** @var FieldType */
private $fieldType;

/**
* @var \eZ\Publish\Core\FieldType\RichText\Converter
*/
protected $docbookToXhtml5EditConverter;

public function __construct(FieldType $fieldType, Converter $docbookToXhtml5EditConverter)
{
$this->fieldType = $fieldType;
$this->docbookToXhtml5EditConverter = $docbookToXhtml5EditConverter;
}

/**
* @param mixed $value
*
* @return string
*/
public function transform($value)
{
if (!$value instanceof Value) {
return '';
}

return $this->docbookToXhtml5EditConverter->convert($value->xml)->saveXML();
}

/**
* @param mixed $value
*
* @return Value|null
*/
public function reverseTransform($value)
{
if ($value === null || empty($value)) {
return $this->fieldType->getEmptyValue();
}

return $this->fieldType->fromHash(['xml' => $value]);
}
}
42 changes: 38 additions & 4 deletions lib/FieldType/Mapper/RichTextFormMapper.php
Expand Up @@ -8,15 +8,49 @@
*/
namespace EzSystems\RepositoryForms\FieldType\Mapper;

use EzSystems\RepositoryForms\Data\FieldDefinitionData;
use EzSystems\RepositoryForms\FieldType\FieldDefinitionFormMapperInterface;
use eZ\Publish\API\Repository\FieldTypeService;
use eZ\Publish\Core\FieldType\RichText\Converter;
use EzSystems\RepositoryForms\Data\Content\FieldData;
use EzSystems\RepositoryForms\FieldType\DataTransformer\RichTextValueTransformer;
use EzSystems\RepositoryForms\FieldType\FieldValueFormMapperInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RichTextFormMapper implements FieldDefinitionFormMapperInterface
class RichTextFormMapper implements FieldValueFormMapperInterface
{
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
/**
* @var FieldTypeService
*/
private $fieldTypeService;

/**
* @var \eZ\Publish\Core\FieldType\RichText\Converter
*/
private $docbookToXhtml5EditConverter;

public function __construct(FieldTypeService $fieldTypeService, Converter $docbookToXhtml5EditConverter)
{
$this->fieldTypeService = $fieldTypeService;
$this->docbookToXhtml5EditConverter = $docbookToXhtml5EditConverter;
}

public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
{
$fieldDefinition = $data->fieldDefinition;
$formConfig = $fieldForm->getConfig();

$fieldForm
->add(
$formConfig->getFormFactory()->createBuilder()
->create('value', TextareaType::class, [
'required' => $fieldDefinition->isRequired,
'label' => $fieldDefinition->getName($formConfig->getOption('languageCode')),
])
->addModelTransformer(new RichTextValueTransformer($this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier), $this->docbookToXhtml5EditConverter))
->setAutoInitialize(false)
->getForm()
);
}

/**
Expand Down

0 comments on commit 48c1ff4

Please sign in to comment.