diff --git a/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php b/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php index 690fd8db25..4a94f7086c 100644 --- a/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php +++ b/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php @@ -18,13 +18,9 @@ class TypedDataNormalizer extends NormalizerBase { * {@inheritdoc} */ public function normalize($object, $format = NULL, array $context = []) { + /* @var \Drupal\Core\TypedData\TypedDataInterface $object */ $this->addCacheableDependency($context, $object); - $value = $object->getValue(); - // Support for stringable value objects: avoid numerous custom normalizers. - if (is_object($value) && method_exists($value, '__toString')) { - $value = (string) $value; - } - return $value; + return $object->getString(); } } diff --git a/core/modules/text/src/TextProcessed.php b/core/modules/text/src/TextProcessed.php index 1e3c880d58..9755146ed6 100644 --- a/core/modules/text/src/TextProcessed.php +++ b/core/modules/text/src/TextProcessed.php @@ -25,11 +25,11 @@ class TextProcessed extends TypedData implements CacheableDependencyInterface { protected $value = NULL; /** - * Whether the property's value has already been computed or not. + * The render build that was used to compute the field. * - * @var bool + * @var array */ - protected $valueComputed = FALSE; + protected $computedBuild; /** * {@inheritdoc} @@ -46,9 +46,10 @@ public function __construct(DataDefinitionInterface $definition, $name = NULL, T * Ensures that values are only computed once. */ protected function ensureComputedValue() { - if ($this->valueComputed === FALSE) { + $build = $this->getRenderBuild(); + if ($build !== $this->computedBuild) { $this->computeValue(); - $this->valueComputed = TRUE; + $this->computedBuild = $build; } } @@ -64,23 +65,13 @@ public function getValue() { * {@inheritdoc} */ protected function computeValue() { - /** @var \Drupal\Core\Field\FieldItemInterface $item */ - $item = $this->getParent(); - $text = $item->{($this->definition->getSetting('text source'))}; - + $build = $this->getRenderBuild(); // Avoid running \Drupal\Core\Render\RendererInterface::renderPlain // on empty strings. - if (!isset($text) || $text === '') { + if (!isset($build['#text']) || $build['#text'] === '') { $this->value = new FilterProcessResult(''); } else { - $build = [ - '#type' => 'processed_text', - '#text' => $text, - '#format' => $item->format, - '#filter_types_to_skip' => [], - '#langcode' => $item->getLangcode(), - ]; // Capture the cacheability metadata associated with the processed text. $processed_text = $this->getRenderer()->renderPlain($build); $this->value = FilterProcessResult::createFromRenderArray($build)->setProcessedText((string) $processed_text); @@ -120,4 +111,24 @@ protected function getRenderer() { return \Drupal::service('renderer'); } + /** + * Gets the render build for the processed value. + * + * @return array + * The render array. + */ + protected function getRenderBuild(): array { + /** @var \Drupal\Core\Field\FieldItemInterface $item */ + $item = $this->getParent(); + $text = $item->{($this->definition->getSetting('text source'))}; + $build = [ + '#type' => 'processed_text', + '#text' => $text, + '#format' => $item->format, + '#filter_types_to_skip' => [], + '#langcode' => $item->getLangcode(), + ]; + return $build; + } + } diff --git a/core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php b/core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php index fa9db4018d..a48c191b5e 100644 --- a/core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php +++ b/core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php @@ -81,8 +81,6 @@ public function testCrudAndUpdate() { // Change the format, this should update the processed properties. $entity->summary_field->format = 'no_filters'; - $entity->save(); - $entity = $storage->load($entity->id()); $this->assertEqual($entity->summary_field->processed, $value); $this->assertEqual($entity->summary_field->summary_processed, $summary);