diff --git a/core/lib/Drupal/Core/Field/Annotation/FieldType.php b/core/lib/Drupal/Core/Field/Annotation/FieldType.php index de42c80..3ea4589 100644 --- a/core/lib/Drupal/Core/Field/Annotation/FieldType.php +++ b/core/lib/Drupal/Core/Field/Annotation/FieldType.php @@ -91,4 +91,11 @@ class FieldType extends DataType { */ public $list_class; + /** + * Computed that should be exported in processes such as normalization. + * + * @var array + */ + public $export_computed_properties = []; + } diff --git a/core/modules/hal/hal.services.yml b/core/modules/hal/hal.services.yml index f856128..97fcbfb 100644 --- a/core/modules/hal/hal.services.yml +++ b/core/modules/hal/hal.services.yml @@ -8,12 +8,6 @@ services: class: Drupal\hal\Normalizer\FieldItemNormalizer tags: - { name: normalizer, priority: 10 } - serializer.normalizer.text_item.hal: - class: Drupal\hal\Normalizer\TextItemBaseNormalizer - arguments: ['@renderer'] - tags: - # The priority needs to higher than serializer.normalizer.field_item.hal. - - { name: normalizer, priority: 20 } serializer.normalizer.field.hal: class: Drupal\hal\Normalizer\FieldNormalizer tags: diff --git a/core/modules/hal/src/Normalizer/FieldItemNormalizer.php b/core/modules/hal/src/Normalizer/FieldItemNormalizer.php index 4ccf597..cf4769a 100644 --- a/core/modules/hal/src/Normalizer/FieldItemNormalizer.php +++ b/core/modules/hal/src/Normalizer/FieldItemNormalizer.php @@ -3,6 +3,7 @@ namespace Drupal\hal\Normalizer; use Drupal\Core\Field\FieldItemInterface; +use Drupal\serialization\Normalizer\ComputedPropertiesNormalizerTrait; use Symfony\Component\Serializer\Exception\InvalidArgumentException; /** @@ -10,6 +11,8 @@ */ class FieldItemNormalizer extends NormalizerBase { + use ComputedPropertiesNormalizerTrait; + /** * The interface or class that this Normalizer supports. * @@ -30,6 +33,8 @@ public function normalize($field_item, $format = NULL, array $context = []) { $values[$property_name] = $this->serializer->normalize($property, $format, $context); } + $values = array_merge($values, $this->getComputedAttributes($field_item, $format, $context)); + if (isset($context['langcode'])) { $values['lang'] = $context['langcode']; } diff --git a/core/modules/hal/src/Normalizer/TextItemBaseNormalizer.php b/core/modules/hal/src/Normalizer/TextItemBaseNormalizer.php deleted file mode 100644 index 84adaf3..0000000 --- a/core/modules/hal/src/Normalizer/TextItemBaseNormalizer.php +++ /dev/null @@ -1,58 +0,0 @@ -renderer = $renderer; - } - - /** - * {@inheritdoc} - */ - public function normalize($field_item, $format = NULL, array $context = array()) { - /** @var \Drupal\text\Plugin\Field\FieldType\TextItemBase $field_item */ - $values = parent::normalize($field_item, $format, $context); - - $field = $field_item->getParent(); - - $processed_text = $field_item->process_result; - if (!empty($context['cacheability'])) { - /** @var \Drupal\Core\Cache\CacheableMetadata $cacheability */ - $cacheability = $context['cacheability']; - $cacheability->addCacheableDependency($processed_text); - } - $values[$field->getName()][0]['processed'] = $this->serializer->normalize($processed_text->getProcessedText(), $format, $context); - return $values; - } - -} diff --git a/core/modules/hal/tests/src/Kernel/NormalizeTest.php b/core/modules/hal/tests/src/Kernel/NormalizeTest.php index 34555cd..e77d4b4 100644 --- a/core/modules/hal/tests/src/Kernel/NormalizeTest.php +++ b/core/modules/hal/tests/src/Kernel/NormalizeTest.php @@ -175,7 +175,7 @@ public function testNormalize() { [ 'value' => $values['field_test_text']['value'], 'format' => $values['field_test_text']['format'], - 'processed' => "

{$values['field_test_text']['value']}

", + 'process_result' => "

{$values['field_test_text']['value']}

", ], ], ]; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php index 2b582ec..0244f00 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php @@ -201,7 +201,7 @@ protected function getExpectedNormalizedEntity() { [ 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.', 'format' => 'plain_text', - 'processed' => '

The name "llama" was adopted by European settlers from native Peruvians.

' . "\n", + 'process_result' => '

The name "llama" was adopted by European settlers from native Peruvians.

' . "\n", ], ], ]; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestTextItemNormalizerTest.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestTextItemNormalizerTest.php index b6f0a0f..c563b5c 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestTextItemNormalizerTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestTextItemNormalizerTest.php @@ -31,7 +31,7 @@ protected function getExpectedNormalizedEntity() { [ 'value' => 'Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.', 'format' => 'plain_text', - 'processed' => '

Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.

' . "\n", + 'process_result' => '

Cádiz is the oldest continuously inhabited city in Spain and a nice place to spend a Sunday with friends.

' . "\n", ], ]; return $expected; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php index c050789..4d6b300 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php @@ -97,7 +97,7 @@ protected function getExpectedNormalizedEntity() { [ 'value' => 'It is a little known fact that llamas cannot count higher the seven.', 'format' => NULL, - 'processed' => "

It is a little known fact that llamas cannot count higher the seven.

\n", + 'process_result' => "

It is a little known fact that llamas cannot count higher the seven.

\n", ], ], 'parent' => [], diff --git a/core/modules/serialization/src/Normalizer/ComputedPropertiesNormalizerTrait.php b/core/modules/serialization/src/Normalizer/ComputedPropertiesNormalizerTrait.php new file mode 100644 index 0000000..3cf6148 --- /dev/null +++ b/core/modules/serialization/src/Normalizer/ComputedPropertiesNormalizerTrait.php @@ -0,0 +1,45 @@ +getPluginDefinition(); + foreach ($plugin_definition['export_computed_properties'] as $export_computed_property) { + $property = $field_item->get($export_computed_property); + $attribute = $this->serializer->normalize($property, $format, $context); + + if ($attribute instanceof CacheableDependencyInterface && isset($context['cacheability'])) { + $context['cacheability']->addCacheableDependency($attribute); + + } + if (is_object($attribute) && method_exists($attribute, '__toString')) { + $attribute = (string) $attribute; + } + $attributes[$export_computed_property] = $attribute; + } + return $attributes; + } + +} diff --git a/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php b/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php index decca43..0f8dbfe 100644 --- a/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php +++ b/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php @@ -11,6 +11,8 @@ */ class FieldItemNormalizer extends ComplexDataNormalizer implements DenormalizerInterface { + use ComputedPropertiesNormalizerTrait; + /** * {@inheritdoc} */ @@ -54,4 +56,18 @@ protected function constructValue($data, $context) { return $data; } + /** + * {@inheritdoc} + */ + public function normalize($object, $format = NULL, array $context = []) { + $attributes = []; + /** @var \Drupal\Core\Field\FieldItemInterface $object */ + foreach ($object as $name => $field) { + $attributes[$name] = $this->serializer->normalize($field, $format, $context); + } + + $attributes = array_merge($attributes, $this->getComputedAttributes($object, $format, $context)); + return $attributes; + } + } diff --git a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php index 35e69e4..094a877 100644 --- a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php +++ b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @@ -154,7 +154,7 @@ public function testNormalize() { [ 'value' => $this->values['field_test_text']['value'], 'format' => $this->values['field_test_text']['format'], - 'processed' => "

{$this->values['field_test_text']['value']}

", + 'process_result' => "

{$this->values['field_test_text']['value']}

", ], ], ]; @@ -220,7 +220,7 @@ public function testSerialize() { 'revision_id' => '' . $this->entity->getRevisionId() . '', 'default_langcode' => '1', 'non_rev_field' => '', - 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '' . $this->values['field_test_text']['value'] . '

]]>
', + 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '' . $this->values['field_test_text']['value'] . '

]]>
', ]; // Sort it in the same order as normalised. $expected = array_merge($normalized, $expected); diff --git a/core/modules/text/src/Normalizer/TextItemBaseNormalizer.php b/core/modules/text/src/Normalizer/TextItemBaseNormalizer.php deleted file mode 100644 index 495efa2..0000000 --- a/core/modules/text/src/Normalizer/TextItemBaseNormalizer.php +++ /dev/null @@ -1,55 +0,0 @@ -renderer = $renderer; - } - - /** - * {@inheritdoc} - */ - public function normalize($field_item, $format = NULL, array $context = []) { - $attributes = parent::normalize($field_item, $format, $context); - /** @var \Drupal\filter\FilterProcessResult $processed_text */ - $processed_text = $field_item->process_result; - if (!empty($context['cacheability'])) { - /** @var \Drupal\Core\Cache\CacheableMetadata $cacheability */ - $cacheability = $context['cacheability']; - $cacheability->addCacheableDependency($processed_text); - } - $attributes['processed'] = $this->serializer->normalize($processed_text->getProcessedText(), $format, $context); - return $attributes; - } - -} diff --git a/core/modules/text/src/Plugin/Field/FieldType/TextItem.php b/core/modules/text/src/Plugin/Field/FieldType/TextItem.php index 447a0b1..a22e561 100644 --- a/core/modules/text/src/Plugin/Field/FieldType/TextItem.php +++ b/core/modules/text/src/Plugin/Field/FieldType/TextItem.php @@ -14,7 +14,8 @@ * description = @Translation("This field stores a text with a text format."), * category = @Translation("Text"), * default_widget = "text_textfield", - * default_formatter = "text_default" + * default_formatter = "text_default", + * export_computed_properties = { "process_result" } * ) */ class TextItem extends TextItemBase { diff --git a/core/modules/text/src/Plugin/Field/FieldType/TextLongItem.php b/core/modules/text/src/Plugin/Field/FieldType/TextLongItem.php index 15e4eb6..e04c328 100644 --- a/core/modules/text/src/Plugin/Field/FieldType/TextLongItem.php +++ b/core/modules/text/src/Plugin/Field/FieldType/TextLongItem.php @@ -13,7 +13,8 @@ * description = @Translation("This field stores a long text with a text format."), * category = @Translation("Text"), * default_widget = "text_textarea", - * default_formatter = "text_default" + * default_formatter = "text_default", + * export_computed_properties = { "process_result" } * ) */ class TextLongItem extends TextItemBase { diff --git a/core/modules/text/src/Plugin/Field/FieldType/TextWithSummaryItem.php b/core/modules/text/src/Plugin/Field/FieldType/TextWithSummaryItem.php index 6af6d04..5d48488 100644 --- a/core/modules/text/src/Plugin/Field/FieldType/TextWithSummaryItem.php +++ b/core/modules/text/src/Plugin/Field/FieldType/TextWithSummaryItem.php @@ -15,7 +15,8 @@ * description = @Translation("This field stores long text with a format and an optional summary."), * category = @Translation("Text"), * default_widget = "text_textarea_with_summary", - * default_formatter = "text_default" + * default_formatter = "text_default", + * export_computed_properties = { "process_result" } * ) */ class TextWithSummaryItem extends TextItemBase { diff --git a/core/modules/text/text.services.yml b/core/modules/text/text.services.yml deleted file mode 100644 index 0d13007..0000000 --- a/core/modules/text/text.services.yml +++ /dev/null @@ -1,8 +0,0 @@ -services: - serializer.normalizer.text_item_base: - class: Drupal\text\Normalizer\TextItemBaseNormalizer - arguments: ['@renderer', '@config.factory'] - tags: - # This normalizer needs to be the same or greater priority than serializer.normalizer.field_item.hal - - { name: normalizer, priority: 10 } -