diff --git a/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php b/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php index 321c924dbc..5b6b58b4a4 100644 --- a/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php +++ b/core/lib/Drupal/Core/TypedData/ComplexDataInterface.php @@ -103,7 +103,7 @@ public function toArray(); public function isEmpty(); /** - * Gets exposed properties for the field item. + * Gets an array of property objects for exposed properties. * * @return \Drupal\Core\TypedData\TypedDataInterface[] * The exposed properties. diff --git a/core/modules/hal/src/Normalizer/FieldItemNormalizer.php b/core/modules/hal/src/Normalizer/FieldItemNormalizer.php index 02910c9b41..d162ff106f 100644 --- a/core/modules/hal/src/Normalizer/FieldItemNormalizer.php +++ b/core/modules/hal/src/Normalizer/FieldItemNormalizer.php @@ -4,7 +4,7 @@ use Drupal\Core\Field\FieldItemInterface; use Drupal\serialization\Normalizer\ComputedPropertiesNormalizerTrait; -use Drupal\serialization\Normalizer\FieldItemPropertiesNormalizerTrait; +use Drupal\serialization\Normalizer\ComplexDataPropertiesNormalizerTrait; use Symfony\Component\Serializer\Exception\InvalidArgumentException; /** @@ -12,7 +12,7 @@ */ class FieldItemNormalizer extends NormalizerBase { - use FieldItemPropertiesNormalizerTrait; + use ComplexDataPropertiesNormalizerTrait; /** * The interface or class that this Normalizer supports. @@ -26,7 +26,7 @@ class FieldItemNormalizer extends NormalizerBase { */ public function normalize($field_item, $format = NULL, array $context = []) { /** @var \Drupal\Core\Field\FieldItemInterface $field_item */ - $values = $this->normalizeFieldProperties($field_item, $format, $context); + $values = $this->normalizeProperties($field_item, $format, $context); if (isset($context['langcode'])) { $values['lang'] = $context['langcode']; diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestExposedPropertyNormalizerTest.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestExposedPropertyNormalizerTest.php index ee88d9bb18..27729afa82 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestExposedPropertyNormalizerTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestExposedPropertyNormalizerTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\rest\Functional\EntityResource\EntityTest; +use Drupal\Core\Cache\Cache; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\Tests\rest\Functional\AnonResourceTestTrait; @@ -45,7 +46,6 @@ protected function getExpectedNormalizedEntity() { * {@inheritdoc} */ protected function createEntity() { - // If the exposed test field has not been created create it. if (!FieldStorageConfig::loadByName('entity_test', 'field_test_exposed')) { // Auto-create a field for testing. FieldStorageConfig::create([ @@ -84,4 +84,18 @@ protected function getNormalizedPostEntity() { return $post_entity; } + /** + * {@inheritdoc} + */ + protected function getExpectedCacheContexts() { + return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['request_format']); + } + + /** + * {@inheritdoc} + */ + protected function getExpectedCacheTags() { + return Cache::mergeTags(parent::getExpectedCacheTags(), ['you_are_it', 'no_tag_backs']); + } + } diff --git a/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php b/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php index 3d2031218f..6c742d0f9c 100644 --- a/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php +++ b/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php @@ -14,6 +14,7 @@ */ class ComplexDataNormalizer extends NormalizerBase { + use ComplexDataPropertiesNormalizerTrait; /** * The interface or class that this Normalizer supports. * @@ -25,12 +26,8 @@ class ComplexDataNormalizer extends NormalizerBase { * {@inheritdoc} */ public function normalize($object, $format = NULL, array $context = []) { - $attributes = []; - /** @var \Drupal\Core\TypedData\TypedDataInterface $field */ - foreach ($object as $name => $field) { - $attributes[$name] = $this->serializer->normalize($field, $format, $context); - } - return $attributes; + /** @var \Drupal\Core\TypedData\ComplexDataInterface $object */ + return $this->normalizeProperties($object, $format, $context); } } diff --git a/core/modules/serialization/src/Normalizer/FieldItemPropertiesNormalizerTrait.php b/core/modules/serialization/src/Normalizer/ComplexDataPropertiesNormalizerTrait.php similarity index 58% rename from core/modules/serialization/src/Normalizer/FieldItemPropertiesNormalizerTrait.php rename to core/modules/serialization/src/Normalizer/ComplexDataPropertiesNormalizerTrait.php index 1760188310..0969629faf 100644 --- a/core/modules/serialization/src/Normalizer/FieldItemPropertiesNormalizerTrait.php +++ b/core/modules/serialization/src/Normalizer/ComplexDataPropertiesNormalizerTrait.php @@ -3,30 +3,29 @@ namespace Drupal\serialization\Normalizer; use Drupal\Core\Cache\CacheableDependencyInterface; -use Drupal\Core\Field\FieldItemInterface; -use Drupal\Core\TypedData\ExposableDataDefinitionInterface; +use Drupal\Core\TypedData\ComplexDataInterface; /** - * Normalization methods for computed field properties. + * Normalization methods for complex data properties. */ -trait FieldItemPropertiesNormalizerTrait { +trait ComplexDataPropertiesNormalizerTrait { /** - * Normalizes field properties. + * Normalizes complex data properties. * - * @param \Drupal\Core\Field\FieldItemInterface $field_item - * The field item being normalized. + * @param \Drupal\Core\TypedData\ComplexDataInterface $data + * The complex data being normalized. * @param string $format * The normalization format. * @param array $context * The context passed into the Normalizer. * * @return array - * The normalized field properties. + * The normalized complex data properties. */ - protected function normalizeFieldProperties(FieldItemInterface $field_item, $format, array $context) { + protected function normalizeProperties(ComplexDataInterface $data, $format, array $context) { $attributes = []; - foreach ($field_item->getExposedProperties() as $name => $property) { + foreach ($data->getExposedProperties() as $name => $property) { $attribute = $this->serializer->normalize($property, $format, $context); if ($attribute instanceof CacheableDependencyInterface && isset($context['cacheability'])) { $context['cacheability']->addCacheableDependency($attribute); diff --git a/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php b/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php index 77479a0cb8..c2f62f1d59 100644 --- a/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php +++ b/core/modules/serialization/src/Normalizer/FieldItemNormalizer.php @@ -2,7 +2,6 @@ namespace Drupal\serialization\Normalizer; -use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Field\FieldItemInterface; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; @@ -12,7 +11,7 @@ */ class FieldItemNormalizer extends ComplexDataNormalizer implements DenormalizerInterface { - use FieldItemPropertiesNormalizerTrait; + use ComplexDataPropertiesNormalizerTrait; /** * {@inheritdoc} @@ -57,12 +56,4 @@ protected function constructValue($data, $context) { return $data; } - /** - * {@inheritdoc} - */ - public function normalize($object, $format = NULL, array $context = []) { - $attributes = $this->normalizeFieldProperties($object, $format, $context); - return $attributes; - } - } diff --git a/core/modules/system/tests/modules/entity_test/src/ComputedString.php b/core/modules/system/tests/modules/entity_test/src/ComputedString.php new file mode 100644 index 0000000000..0c78bdc02d --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/ComputedString.php @@ -0,0 +1,41 @@ +notComputedValue = $not_computed_value; + $this->cacheContexts = ['request_format']; + $this->cacheTags = ['you_are_it', 'no_tag_backs']; + } + + /** + * {@inheritdoc} + */ + public function __toString() { + // Computation is simple concatenation for test. + return "Exposed! " . $this->notComputedValue; + } + +} diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ExposedStringData.php b/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ExposedStringData.php index 946e4d2f1a..650121754f 100644 --- a/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ExposedStringData.php +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ExposedStringData.php @@ -3,6 +3,7 @@ namespace Drupal\entity_test\Plugin\DataType; use Drupal\Core\TypedData\Plugin\DataType\StringData; +use Drupal\entity_test\ComputedString; /** * The exposed string test data type. @@ -22,9 +23,15 @@ class ExposedStringData extends StringData { public function getValue() { /** @var \Drupal\Core\Field\FieldItemInterface $item */ $item = $this->getParent(); - /** @var \Drupal\Core\TypedData\Plugin\DataType\StringData $string_value */ - $string_value = $item->get('value'); - return "Exposed! " . $string_value->getString(); + $computed = new ComputedString($item->get('value')->getString()); + return $computed; + } + + /** + * {@inheritdoc} + */ + public function getCastedValue() { + return $this->getValue(); } }