...onditionalCacheabilityMetadataBubblingTrait.php | 32 ++++ core/modules/filter/src/Element/ProcessedText.php | 7 +- core/modules/filter/src/FilterProcessResult.php | 2 +- .../tests/src/Kernel/EntitySerializationTest.php | 34 +++- .../text/src/Normalizer/TextItemBaseNormalizer.php | 101 ++++++++++++ .../Normalizer/TextItemBaseNormalizerTest.php | 183 +++++++++++++++++++++ core/modules/text/text.services.yml | 8 + 7 files changed, 361 insertions(+), 6 deletions(-) diff --git a/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php b/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php new file mode 100644 index 0000000..b8c4b33 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php @@ -0,0 +1,32 @@ +renderer->hasRenderContext()) { + $build = []; + CacheableMetadata::createFromObject($object)->applyTo($build); + $this->renderer->render($build); + } + } + +} diff --git a/core/modules/filter/src/Element/ProcessedText.php b/core/modules/filter/src/Element/ProcessedText.php index 0d09870..27b797e 100644 --- a/core/modules/filter/src/Element/ProcessedText.php +++ b/core/modules/filter/src/Element/ProcessedText.php @@ -3,6 +3,7 @@ namespace Drupal\filter\Element; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\Element\RenderElement; use Drupal\filter\Entity\FilterFormat; @@ -69,7 +70,11 @@ public static function preRenderText($element) { $langcode = $element['#langcode']; if (!isset($format_id)) { - $format_id = static::configFactory()->get('filter.settings')->get('fallback_format'); + $filter_settings = static::configFactory()->get('filter.settings'); + $format_id = $filter_settings->get('fallback_format'); + CacheableMetadata::createFromRenderArray($element) + ->addCacheableDependency($filter_settings) + ->applyTo($element); } /** @var \Drupal\filter\Entity\FilterFormat $format **/ $format = FilterFormat::load($format_id); 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/serialization/tests/src/Kernel/EntitySerializationTest.php b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php index 0f84d3d..ad23a04 100644 --- a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php +++ b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @@ -4,6 +4,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\entity_test\Entity\EntityTestMulRev; +use Drupal\filter\Entity\FilterFormat; /** * Tests that entities can be serialized to supported core formats. @@ -60,6 +61,29 @@ protected function setUp() { // User create needs sequence table. $this->installSchema('system', array('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', + 'filters' => [ + 'filter_html' => [ + 'module' => 'filter', + 'status' => TRUE, + 'weight' => 10, + 'settings' => [ + 'allowed_html' => '
', + ], + ], + 'filter_autop' => [ + 'module' => 'filter', + 'status' => TRUE, + 'weight' => 10, + 'settings' => [], + ], + ], + ])->save(); + // Create a test user to use as the entity owner. $this->user = \Drupal::entityManager()->getStorage('user')->create([ 'name' => 'serialization_test_user', @@ -69,12 +93,13 @@ protected function setUp() { $this->user->save(); // Create a test entity to serialize. + $test_text_value = $this->randomMachineName(); $this->values = array( 'name' => $this->randomMachineName(), 'user_id' => $this->user->id(), 'field_test_text' => array( - 'value' => $this->randomMachineName(), - 'format' => 'full_html', + 'value' => $test_text_value, + 'format' => 'my_text_format', ), ); $this->entity = EntityTestMulRev::create($this->values); @@ -127,6 +152,7 @@ public function testNormalize() { array( 'value' => $this->values['field_test_text']['value'], 'format' => $this->values['field_test_text']['format'], + 'processed' => "
{$this->values['field_test_text']['value']}
" ), ), ); @@ -134,7 +160,7 @@ public function testNormalize() { $normalized = $this->serializer->normalize($this->entity); foreach (array_keys($expected) as $fieldName) { - $this->assertEqual($expected[$fieldName], $normalized[$fieldName], "ComplexDataNormalizer produces expected array for $fieldName."); + $this->assertEquals($expected[$fieldName], $normalized[$fieldName], "Field normalization produces expected array for $fieldName."); } $this->assertEqual(array_diff_key($normalized, $expected), array(), 'No unexpected data is added to the normalized array.'); } @@ -192,7 +218,7 @@ public function testSerialize() { 'revision_id' => 'text item
\n", + 'format' => NULL, + ], + (new CacheableMetadata()) + ->setCacheTags(['config:filter.settings']), + ]; + $test_cases['my text format'] = [ + [ + 'value' => 'This is important.', + 'format' => 'my_text_format', + ], + [ + 'value' => "This is important.", + 'format' => 'my_text_format', + 'processed' => "This is important.
\n", + ], + (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, + 'settings' => [ + 'allowed_html' => ' ', + ], + ], + ], + "This is important.
\n", + ]; + return $test_cases; + } + +} diff --git a/core/modules/text/text.services.yml b/core/modules/text/text.services.yml new file mode 100644 index 0000000..0d13007 --- /dev/null +++ b/core/modules/text/text.services.yml @@ -0,0 +1,8 @@ +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 } +