diff --git a/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php b/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php index bc7ee20..b8c4b33 100644 --- a/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php +++ b/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php @@ -3,7 +3,7 @@ namespace Drupal\Core\Cache; /** - * Provides bubble function to apply cache dependency to render context. + * Provides bubble function to apply cacheable dependency to render context. * * This trait should be used with great care. It should only be used by classes * that may be used both inside and outside of a render context. @@ -14,29 +14,19 @@ trait ConditionalCacheabilityMetadataBubblingTrait { /** - * Bubbles the bubbleable cacheability metadata to the current render context. + * Bubbles cacheability metadata to the current render context. * * This method does not bubble attachments. * * @param \Drupal\Core\Cache\CacheableDependencyInterface $object - * The cacheable dependency object. + * A cacheable dependency object. */ protected function bubble(CacheableDependencyInterface $object) { - if ($this->renderer()->hasRenderContext()) { + if ($this->renderer->hasRenderContext()) { $build = []; CacheableMetadata::createFromObject($object)->applyTo($build); - $this->renderer()->render($build); + $this->renderer->render($build); } } - /** - * Gets the render service. - * - * @return \Drupal\Core\Render\RendererInterface - * The renderer. - */ - protected function renderer() { - return isset($this->renderer) ? $this->renderer : \Drupal::service('renderer'); - } - } diff --git a/core/modules/filter/src/FilterProcessResult.php b/core/modules/filter/src/FilterProcessResult.php index 4b07fad..02c89d0 100644 --- a/core/modules/filter/src/FilterProcessResult.php +++ b/core/modules/filter/src/FilterProcessResult.php @@ -77,7 +77,7 @@ class FilterProcessResult extends BubbleableMetadata { * @param string $processed_text * The text as processed by a text filter. */ - public function __construct($processed_text) { + public function __construct($processed_text = '') { $this->processedText = $processed_text; } diff --git a/core/modules/text/src/Normalizer/TextItemBaseNormalizer.php b/core/modules/text/src/Normalizer/TextItemBaseNormalizer.php index 613b5ec..faf267e 100644 --- a/core/modules/text/src/Normalizer/TextItemBaseNormalizer.php +++ b/core/modules/text/src/Normalizer/TextItemBaseNormalizer.php @@ -2,10 +2,10 @@ namespace Drupal\text\Normalizer; -use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\ConditionalCacheabilityMetadataBubblingTrait; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Render\RendererInterface; +use Drupal\filter\FilterProcessResult; use Drupal\serialization\Normalizer\ComplexDataNormalizer; use Drupal\text\Plugin\Field\FieldType\TextItemBase; @@ -37,7 +37,7 @@ class TextItemBaseNormalizer extends ComplexDataNormalizer { * * @var \Drupal\Core\Config\ConfigFactoryInterface */ - protected $config; + protected $configFactory; /** * Constructs a TextItemBaseNormalizer object. @@ -49,7 +49,7 @@ class TextItemBaseNormalizer extends ComplexDataNormalizer { */ public function __construct(RendererInterface $renderer, ConfigFactoryInterface $config_factory) { $this->renderer = $renderer; - $this->config = $config_factory; + $this->configFactory = $config_factory; } /** @@ -57,31 +57,27 @@ public function __construct(RendererInterface $renderer, ConfigFactoryInterface */ public function normalize($field_item, $format = NULL, array $context = []) { $attributes = parent::normalize($field_item, $format, $context); - /** @var \Drupal\text\Plugin\Field\FieldType\TextItemBase $field_item */ - $value = $field_item->getValue(); - - $attributes['processed'] = $this->normalizeProcessed($value, $format, $context); - + $processed_text = $this->normalizeProcessed($field_item); + $this->bubble($processed_text); + $attributes['processed'] = $this->serializer->normalize($processed_text->getProcessedText(), $format, $context); return $attributes; } /** * Normalizes the processed attribute. * - * @param array $value - * The field item value. - * @param string $format - * The format the normalization result will be encoded as. - * @param array $context - * The context options for the normalizer. + * @param \Drupal\text\Plugin\Field\FieldType\TextItemBase $field_item + * The field item. * - * @return string - * The normalized 'processed' attribute. + * @return FilterProcessResult + * The filter process result for the text item. */ - protected function normalizeProcessed($value, $format, $context) { + protected function normalizeProcessed(TextItemBase $field_item) { + $value = $field_item->getValue(); if (empty($value['format'])) { - // The fallback filter format. - $filter_format_id = $this->config->get('filter.settings')->get('fallback_format'); + $filter_settings = $this->configFactory->get('filter.settings'); + $this->bubble($filter_settings); + $filter_format_id = $filter_settings->get('fallback_format'); } else { $filter_format_id = $value['format']; @@ -96,10 +92,8 @@ protected function normalizeProcessed($value, $format, $context) { // The rendering process needs to be applied to caching. New cacheability // metadata can be introduced in the process. // @see \Drupal\editor\Plugin\Filter\EditorFileReference::process(). - $processed = $this->serializer->normalize($this->renderer->renderPlain($build), $format, $context); - $this->bubble(CacheableMetadata::createFromRenderArray($build)); - - return $processed; + $processed_text = $this->renderer->renderPlain($build); + return FilterProcessResult::createFromRenderArray($build)->setProcessedText($processed_text); } } diff --git a/core/modules/text/tests/src/Kernel/Normalizer/TextItemBaseNormalizerTest.php b/core/modules/text/tests/src/Kernel/Normalizer/TextItemBaseNormalizerTest.php index 8d6504a..0d217a2 100644 --- a/core/modules/text/tests/src/Kernel/Normalizer/TextItemBaseNormalizerTest.php +++ b/core/modules/text/tests/src/Kernel/Normalizer/TextItemBaseNormalizerTest.php @@ -107,14 +107,14 @@ public function testNormalize($text_item, array $expected, CacheableMetadata $ex return $this->serializer->normalize($entity); }); - $cacheability = new BubbleableMetadata(); - $cacheability->setCacheTags($text_format->getCacheTags()); + $expected_cacheability = new BubbleableMetadata(); + $expected_cacheability->setCacheTags($text_format->getCacheTags()); $contexts = $this->container->getParameter('renderer.config')['required_cache_contexts']; - $cacheability->setCacheContexts($contexts); + $expected_cacheability->setCacheContexts($contexts); // Merge the CacheableMetadata that is specific to this test. - $cacheability = $cacheability->merge($extra_cacheability); + $expected_cacheability = $expected_cacheability->merge($extra_cacheability); - $this->assertEquals($cacheability, $context->pop()); + $this->assertEquals($expected_cacheability, $context->pop()); $this->assertEquals($expected, $data['field_text'][0]); if ($filter_config_update) { @@ -132,7 +132,7 @@ public function testNormalize($text_item, array $expected, CacheableMetadata $ex }); $this->assertFalse($context->isEmpty()); - $this->assertEquals($cacheability, $context->pop()); + $this->assertEquals($expected_cacheability, $context->pop()); $expected['processed'] = $updated_processed; $this->assertEquals($expected, $data['field_text'][0]); } @@ -142,22 +142,17 @@ public function testNormalize($text_item, array $expected, CacheableMetadata $ex * Data provider for testNormalize(). */ public function testNormalizeProvider() { - $cacheability = new CacheableMetadata(); - $test_cases['no_format_specified'] = [ + $test_cases['no format specified'] = [ 'text item', [ 'value' => 'text item', 'processed' => "

text item

\n", 'format' => NULL, ], - $cacheability, + (new CacheableMetadata()) + ->setCacheTags(['config:filter.settings']), ]; - $cacheability = new CacheableMetadata(); - // Add the tags for the 'filter_test_cache_tags' filter. - $cacheability->setCacheTags(['foo:bar', 'foo:baz']); - // Add the contexts for the 'filter_test_cache_contexts' filter. - $cacheability->setCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]); - $test_cases['my_text_format'] = [ + $test_cases['my text format'] = [ [ 'value' => 'This is important.', 'format' => 'my_text_format', @@ -167,7 +162,11 @@ public function testNormalizeProvider() { 'format' => 'my_text_format', 'processed' => "

This is important.

\n", ], - $cacheability, + (new CacheableMetadata()) + // Add the tags for the 'filter_test_cache_tags' filter. + ->setCacheTags(['foo:bar', 'foo:baz']) + // Add the contexts for the 'filter_test_cache_contexts' filter. + ->setCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]), [ 'filter_html' => [ 'status' => TRUE,