diff --git a/core/modules/serialization/serialization.install b/core/modules/serialization/serialization.install index 84be1768bf..afc3311b4c 100644 --- a/core/modules/serialization/serialization.install +++ b/core/modules/serialization/serialization.install @@ -17,7 +17,7 @@ function serialization_requirements($phase) { 'severity' => REQUIREMENT_INFO, ]; - if (\Drupal::config('serialization.settings')->get('bc_primitives_as_strings')) { + if (\Drupal::config('serialization.settings')->get('bc_primitives_as_strings')) { $requirements['serialization_as_strings']['value'] = t('Enabled'); $requirements['serialization_as_strings']['description'] = t('The Serialization API is configured to output only string values for REST and other applications (instead of integers or Booleans when appropriate). Disabling this backwards compatibility mode is recommended unless your sites or applications require string output.'); } diff --git a/core/modules/serialization/src/Normalizer/NormalizerBase.php b/core/modules/serialization/src/Normalizer/NormalizerBase.php index 5e829f65e7..9385b99122 100644 --- a/core/modules/serialization/src/Normalizer/NormalizerBase.php +++ b/core/modules/serialization/src/Normalizer/NormalizerBase.php @@ -2,6 +2,7 @@ namespace Drupal\serialization\Normalizer; +use Drupal\Core\Cache\CacheableDependencyInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer; @@ -81,4 +82,18 @@ protected function checkFormat($format = NULL) { return in_array($format, (array) $this->format, TRUE); } + /** + * Adds cacheability if applicable. + * + * @param array $context + * Context options for the normalizer. + * @param $data + * The data that might have cacheability information. + */ + protected function addCacheableDependency(array $context, $data) { + if ($data instanceof CacheableDependencyInterface && isset($context['cacheability'])) { + $context['cacheability']->addCacheableDependency($data); + } + } + } diff --git a/core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php b/core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php index 86d92531ed..cce108cacf 100644 --- a/core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php +++ b/core/modules/serialization/src/Normalizer/PrimitiveDataNormalizer.php @@ -2,7 +2,6 @@ namespace Drupal\serialization\Normalizer; -use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\TypedData\PrimitiveInterface; /** @@ -27,16 +26,7 @@ public function normalize($object, $format = NULL, array $context = []) { // getCastedValue would return NULL, but as typed data is not aware of real // optional values on the primitive level, we implement our own optional // value normalization here. - $value = $object->getValue(); - if ($value === NULL) { - return NULL; - } - else { - if ($value instanceof CacheableDependencyInterface && isset($context['cacheability'])) { - $context['cacheability']->addCacheableDependency($value); - } - return $object->getCastedValue(); - } + return $object->getValue() === NULL ? NULL : $object->getCastedValue(); } } diff --git a/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php b/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php index 9603147780..958b987dc7 100644 --- a/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php +++ b/core/modules/serialization/src/Normalizer/TypedDataNormalizer.php @@ -2,8 +2,6 @@ namespace Drupal\serialization\Normalizer; -use Drupal\Core\Cache\CacheableDependencyInterface; - /** * Converts typed data objects to arrays. */ @@ -20,19 +18,8 @@ class TypedDataNormalizer extends NormalizerBase { * {@inheritdoc} */ public function normalize($object, $format = NULL, array $context = []) { - $value = $object->getValue(); - if ($value === NULL) { - return NULL; - } - else { - if ($value instanceof CacheableDependencyInterface && isset($context['cacheability'])) { - $context['cacheability']->addCacheableDependency($value); - return (string) $value; - } - - } - return $value; - + $this->addCacheableDependency($context, $object); + return $object->getValue(); } } diff --git a/core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php b/core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php index 579e0a47fa..1767102d7a 100644 --- a/core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php +++ b/core/modules/system/tests/modules/entity_test/src/TypedData/ComputedString.php @@ -2,36 +2,23 @@ namespace Drupal\entity_test\TypedData; +use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; -use Drupal\Core\Cache\RefinableCacheableDependencyTrait; use Drupal\Core\TypedData\TypedData; -use Drupal\Core\Render\BubbleableMetadata; - /** * A computed property for test strings. */ class ComputedString extends TypedData implements CacheableDependencyInterface { - use RefinableCacheableDependencyTrait; - - /** - * @var string - */ - protected $computedValue; - /** * {@inheritdoc} */ public function getValue() { /** @var \Drupal\Core\Field\FieldItemInterface $item */ $item = $this->getParent(); - $this->cacheContexts = ['request_format']; - $this->cacheTags = ['you_are_it', 'no_tag_backs']; - - $this->computedValue = "Computed! " . $item->get('value')->getString(); - return $this; + return 'Computed! ' . $item->get('value')->getString(); } /** @@ -44,7 +31,21 @@ public function getCastedValue() { /** * {@inheritdoc} */ - function __toString() { - return $this->computedValue; + public function getCacheTags() { + return ['you_are_it', 'no_tag_backs']; + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + return ['request_format']; + } + + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + return Cache::PERMANENT; } }