.../Core/TypedData/ComputedPropertyTrait.php | 66 ---------------------- core/modules/filter/src/Element/ProcessedText.php | 1 + .../modules/hal/tests/src/Kernel/NormalizeTest.php | 3 +- .../tests/src/Kernel/EntitySerializationTest.php | 2 - core/modules/text/src/TextProcessed.php | 44 ++++++++++++++- 5 files changed, 43 insertions(+), 73 deletions(-) diff --git a/core/lib/Drupal/Core/TypedData/ComputedPropertyTrait.php b/core/lib/Drupal/Core/TypedData/ComputedPropertyTrait.php deleted file mode 100644 index aa78f9d..0000000 --- a/core/lib/Drupal/Core/TypedData/ComputedPropertyTrait.php +++ /dev/null @@ -1,66 +0,0 @@ -valueComputed === FALSE) { - $this->computeValue(); - $this->valueComputed = TRUE; - } - } - - /** - * {@inheritdoc} - */ - public function getValue() { - $this->ensureComputedValue(); - return parent::getValue(); - } - - /** - * {@inheritdoc} - */ - public function getCacheTags() { - $this->ensureComputedValue(); - return $this->value->getCacheTags(); - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts() { - $this->ensureComputedValue(); - return $this->value->getCacheContexts(); - } - - /** - * {@inheritdoc} - */ - public function getCacheMaxAge() { - $this->ensureComputedValue(); - return $this->value->getCacheMaxAge(); - } - -} diff --git a/core/modules/filter/src/Element/ProcessedText.php b/core/modules/filter/src/Element/ProcessedText.php index e3ba8fd..1a3392b 100644 --- a/core/modules/filter/src/Element/ProcessedText.php +++ b/core/modules/filter/src/Element/ProcessedText.php @@ -72,6 +72,7 @@ public static function preRenderText($element) { if (!isset($format_id)) { $filter_settings = static::configFactory()->get('filter.settings'); $format_id = $filter_settings->get('fallback_format'); + // Ensure 'filter.settings' config's cacheability is respected. CacheableMetadata::createFromRenderArray($element) ->addCacheableDependency($filter_settings) ->applyTo($element); diff --git a/core/modules/hal/tests/src/Kernel/NormalizeTest.php b/core/modules/hal/tests/src/Kernel/NormalizeTest.php index 34555cd..ffea170 100644 --- a/core/modules/hal/tests/src/Kernel/NormalizeTest.php +++ b/core/modules/hal/tests/src/Kernel/NormalizeTest.php @@ -19,8 +19,7 @@ class NormalizeTest extends NormalizerTestBase { */ protected function setUp() { parent::setUp(); - // Create a text format because it is needed for TextItemBase normalization. - // @see \Drupal\text\Normalizer\TextItemBaseNormalizer::normalize(). + FilterFormat::create([ 'format' => 'my_text_format', 'name' => 'My Text Format', diff --git a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php index 481d0d5..5df9d75 100644 --- a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php +++ b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @@ -65,8 +65,6 @@ protected function setUp() { // User create needs sequence table. $this->installSchema('system', ['sequences']); - // Create a text format because it is needed for TextItemBase normalization. - // @see \Drupal\text\Normalizer\TextItemBaseNormalizer::normalize(). FilterFormat::create([ 'format' => 'my_text_format', 'name' => 'My Text Format', diff --git a/core/modules/text/src/TextProcessed.php b/core/modules/text/src/TextProcessed.php index be63dbf..1e3c880 100644 --- a/core/modules/text/src/TextProcessed.php +++ b/core/modules/text/src/TextProcessed.php @@ -3,7 +3,6 @@ namespace Drupal\text; use Drupal\Core\Cache\CacheableDependencyInterface; -use Drupal\Core\TypedData\ComputedPropertyTrait; use Drupal\Core\TypedData\DataDefinitionInterface; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TypedData; @@ -18,8 +17,6 @@ */ class TextProcessed extends TypedData implements CacheableDependencyInterface { - use ComputedPropertyTrait; - /** * Cached processed text. * @@ -28,6 +25,13 @@ class TextProcessed extends TypedData implements CacheableDependencyInterface { protected $value = NULL; /** + * Whether the property's value has already been computed or not. + * + * @var bool + */ + protected $valueComputed = FALSE; + + /** * {@inheritdoc} */ public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) { @@ -39,6 +43,16 @@ public function __construct(DataDefinitionInterface $definition, $name = NULL, T } /** + * Ensures that values are only computed once. + */ + protected function ensureComputedValue() { + if ($this->valueComputed === FALSE) { + $this->computeValue(); + $this->valueComputed = TRUE; + } + } + + /** * {@inheritdoc} */ public function getValue() { @@ -74,6 +88,30 @@ protected function computeValue() { } /** + * {@inheritdoc} + */ + public function getCacheTags() { + $this->ensureComputedValue(); + return $this->value->getCacheTags(); + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + $this->ensureComputedValue(); + return $this->value->getCacheContexts(); + } + + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + $this->ensureComputedValue(); + return $this->value->getCacheMaxAge(); + } + + /** * Returns the renderer service. * * @return \Drupal\Core\Render\RendererInterface