.../Core/TypedData/DataDefinitionInterface.php | 2 +- .../TypedData/ExposableDataDefinitionTrait.php | 4 +--- core/lib/Drupal/Core/TypedData/TypedDataHelper.php | 27 ---------------------- .../EntityTestExposedPropertyNormalizerTest.php | 3 +-- .../src/Normalizer/ComplexDataNormalizer.php | 3 ++- .../ComplexDataPropertiesNormalizerTrait.php | 20 +++++++++++++++- .../Unit/Normalizer/ComplexDataNormalizerTest.php | 4 ++-- .../Unit/Normalizer/ExposedTypedDataTestTrait.php | 2 +- .../schema/entity_test.data_types.schema.yml | 14 ++--------- .../src/Plugin/DataType/ComputedStringData.php | 6 ++--- .../FieldType/ExposedPropertyTestFieldItem.php | 6 ++--- 11 files changed, 34 insertions(+), 57 deletions(-) diff --git a/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php b/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php index 7ea68e4..cd294ec 100644 --- a/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php +++ b/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php @@ -222,7 +222,7 @@ public function addConstraint($constraint_name, $options = NULL); * Sets the whether the data value should be exposed. * * @param bool $exposed - * Whether the data value is exposed. + * Whether the data value should be exposed. * * @return static * The object itself for chaining. diff --git a/core/lib/Drupal/Core/TypedData/ExposableDataDefinitionTrait.php b/core/lib/Drupal/Core/TypedData/ExposableDataDefinitionTrait.php index 60d8ca7..821727c 100644 --- a/core/lib/Drupal/Core/TypedData/ExposableDataDefinitionTrait.php +++ b/core/lib/Drupal/Core/TypedData/ExposableDataDefinitionTrait.php @@ -3,9 +3,7 @@ namespace Drupal\Core\TypedData; /** - * Exposable property methods. - * - * Methods are implemented for \Drupal\Core\TypedData\DataDefinitionInterface. + * Trait for data definitions that have configurable exposed properties. * * @see \Drupal\Core\TypedData\DataDefinitionInterface */ diff --git a/core/lib/Drupal/Core/TypedData/TypedDataHelper.php b/core/lib/Drupal/Core/TypedData/TypedDataHelper.php deleted file mode 100644 index 1df10eb..0000000 --- a/core/lib/Drupal/Core/TypedData/TypedDataHelper.php +++ /dev/null @@ -1,27 +0,0 @@ -getProperties(TRUE), function (TypedDataInterface $property) { - return $property->getDataDefinition()->isExposed(); - }); - } - -} 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 b24fb81..1119c94 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestExposedPropertyNormalizerTest.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestExposedPropertyNormalizerTest.php @@ -74,14 +74,13 @@ protected function createEntity() { * {@inheritdoc} */ protected function getNormalizedPostEntity() { - $post_entity = parent::getNormalizedPostEntity() + [ + return parent::getNormalizedPostEntity() + [ 'field_test_exposed' => [ [ 'value' => 'value to expose', ], ], ]; - return $post_entity; } } diff --git a/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php b/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php index d27b23e..399fcd5 100644 --- a/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php +++ b/core/modules/serialization/src/Normalizer/ComplexDataNormalizer.php @@ -17,6 +17,7 @@ class ComplexDataNormalizer extends NormalizerBase { use ComplexDataPropertiesNormalizerTrait; + /** * The interface or class that this Normalizer supports. * @@ -30,7 +31,7 @@ class ComplexDataNormalizer extends NormalizerBase { public function normalize($object, $format = NULL, array $context = []) { // $object will not always match $supportedInterfaceOrClass. // @see \Drupal\serialization\Normalizer\EntityNormalizer - // Other normalizer that extend this class may only provide $object that + // Other normalizers that extend this class may only provide $object that // implements \Traversable. if ($object instanceof ComplexDataInterface) { $attributes = $this->normalizeProperties($object, $format, $context); diff --git a/core/modules/serialization/src/Normalizer/ComplexDataPropertiesNormalizerTrait.php b/core/modules/serialization/src/Normalizer/ComplexDataPropertiesNormalizerTrait.php index d9d146e..7dcae68 100644 --- a/core/modules/serialization/src/Normalizer/ComplexDataPropertiesNormalizerTrait.php +++ b/core/modules/serialization/src/Normalizer/ComplexDataPropertiesNormalizerTrait.php @@ -4,9 +4,12 @@ use Drupal\Core\TypedData\ComplexDataInterface; use Drupal\Core\TypedData\TypedDataHelper; +use Drupal\Core\TypedData\TypedDataInterface; /** * Normalization methods for complex data properties. + * + * @see \Drupal\Core\TypedData\ComplexDataInterface */ trait ComplexDataPropertiesNormalizerTrait { @@ -25,10 +28,25 @@ */ protected function normalizeProperties(ComplexDataInterface $data, $format, array $context) { $attributes = []; - foreach (TypedDataHelper::getExposedProperties($data) as $name => $property) { + foreach (static::getExposedProperties($data) as $name => $property) { $attributes[$name] = $this->serializer->normalize($property, $format, $context); } return $attributes; } + /** + * Gets an array exposed properties from a complex data object. + * + * @param \Drupal\Core\TypedData\ComplexDataInterface $data + * The complex data object. + * + * @return \Drupal\Core\TypedData\TypedDataInterface[] + * The exposed properties, keyed by property name. + */ + protected static function getExposedProperties(ComplexDataInterface $data) { + return array_filter($data->getProperties(TRUE), function (TypedDataInterface $property) { + return $property->getDataDefinition()->isExposed(); + }); + } + } diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php index a56a723..c96b730 100644 --- a/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ComplexDataNormalizerTest.php @@ -45,8 +45,8 @@ protected function setUp() { * @covers ::supportsNormalization */ public function testSupportsNormalization() { - $complexData = $this->prophesize(ComplexDataInterface::class)->reveal(); - $this->assertTrue($this->normalizer->supportsNormalization($complexData)); + $complex_data = $this->prophesize(ComplexDataInterface::class)->reveal(); + $this->assertTrue($this->normalizer->supportsNormalization($complex_data)); // Also test that an object not implementing ComplexDataInterface fails. $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); } diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/ExposedTypedDataTestTrait.php b/core/modules/serialization/tests/src/Unit/Normalizer/ExposedTypedDataTestTrait.php index e16b60f..fed10e5 100644 --- a/core/modules/serialization/tests/src/Unit/Normalizer/ExposedTypedDataTestTrait.php +++ b/core/modules/serialization/tests/src/Unit/Normalizer/ExposedTypedDataTestTrait.php @@ -6,7 +6,7 @@ use Drupal\Core\TypedData\TypedDataInterface; /** - * Test trait that provides functions for retrieving typed data properties. + * Trait that provides mocked typed data objects. */ trait ExposedTypedDataTestTrait { diff --git a/core/modules/system/tests/modules/entity_test/config/schema/entity_test.data_types.schema.yml b/core/modules/system/tests/modules/entity_test/config/schema/entity_test.data_types.schema.yml index bda7a93..f5165db 100644 --- a/core/modules/system/tests/modules/entity_test/config/schema/entity_test.data_types.schema.yml +++ b/core/modules/system/tests/modules/entity_test/config/schema/entity_test.data_types.schema.yml @@ -1,15 +1,5 @@ # Schema for the configuration of the exposed string field type. field.storage_settings.exposed_string_test: - type: mapping - label: 'String settings' - mapping: - max_length: - type: integer - label: 'Maximum length' - case_sensitive: - type: boolean - label: 'Case sensitive' - is_ascii: - type: boolean - label: 'Contains US ASCII characters only' + type: field.storage_settings.string + label: 'Exposed string settings' diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ComputedStringData.php b/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ComputedStringData.php index 6e7462d..75ab0ed 100644 --- a/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ComputedStringData.php +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/DataType/ComputedStringData.php @@ -6,13 +6,11 @@ use Drupal\entity_test\ComputedString; /** - * The exposed string test data type. - * - * This simply concatenates 'Exposed! ' before the 'value' property. + * The computed string data type. * * @DataType( * id = "computed_string", - * label = @Translation("Exposed String") + * label = @Translation("Computed string") * ) */ class ComputedStringData extends StringData { diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ExposedPropertyTestFieldItem.php b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ExposedPropertyTestFieldItem.php index 35b37f1..7874ed2 100644 --- a/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ExposedPropertyTestFieldItem.php +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/ExposedPropertyTestFieldItem.php @@ -14,7 +14,7 @@ * @FieldType( * id = "exposed_string_test", * label = @Translation("Exposed Property (test)"), - * description = @Translation("A field containing two computed string values, one exposed and one not exposed."), + * description = @Translation("A field containing one string, from which two strings are computed (one exposed, one not)."), * category = @Translation("Test"), * default_widget = "string_textfield", * default_formatter = "string" @@ -30,13 +30,13 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel // Add a computed property that is exposed. $properties['exposed_value'] = DataDefinition::create('computed_string') - ->setLabel(new TranslatableMarkup('Text value exposed')) + ->setLabel(new TranslatableMarkup('Computed string, exposed')) ->setComputed(TRUE) ->setClass(ComputedStringData::class) ->setExposed(TRUE); // Add a computed property that is NOT exposed. $properties['non_exposed_value'] = DataDefinition::create('computed_string') - ->setLabel(new TranslatableMarkup('Text value non-exposed')) + ->setLabel(new TranslatableMarkup('Computed string, non-exposed ')) ->setComputed(TRUE) ->setClass(ComputedStringData::class); return $properties;