diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index 92aa3e0cc5..84d73ec5b3 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -5,7 +5,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\TypedData\DataDefinitionInterface; -use Drupal\Core\TypedData\ExposableDataDefinitionInterface; use Drupal\Core\TypedData\Plugin\DataType\Map; use Drupal\Core\TypedData\TypedDataInterface; @@ -277,28 +276,4 @@ public static function onDependencyRemoval(FieldDefinitionInterface $field_defin return FALSE; } - /** - * {@inheritdoc} - * - * Overridden to exposed properties that implement - * \Drupal\Core\TypedData\ExposableDataDefinitionInterface that would not - * otherwise be exposed. - */ - public function getIterator() { - $properties = $this->getProperties(TRUE); - /* @var \Drupal\Core\TypedData\TypedDataInterface[] $properties */ - foreach (array_keys($properties) as $property_name) { - $data_definition = $properties[$property_name]->getDataDefinition(); - if ($data_definition instanceof ExposableDataDefinitionInterface) { - if (!$data_definition->isExposed()) { - unset($properties[$property_name]); - } - } - elseif ($data_definition->isComputed()) { - unset($properties[$property_name]); - } - } - return new \ArrayIterator($properties); - } - } diff --git a/core/modules/serialization/src/Normalizer/FieldItemPropertiesNormalizerTrait.php b/core/modules/serialization/src/Normalizer/FieldItemPropertiesNormalizerTrait.php index a0824d5b06..6d6005440d 100644 --- a/core/modules/serialization/src/Normalizer/FieldItemPropertiesNormalizerTrait.php +++ b/core/modules/serialization/src/Normalizer/FieldItemPropertiesNormalizerTrait.php @@ -4,6 +4,7 @@ use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Field\FieldItemInterface; +use Drupal\Core\TypedData\ExposableDataDefinitionInterface; /** * Normalization methods for computed field properties. @@ -25,7 +26,7 @@ */ protected function normalizeFieldProperties(FieldItemInterface $field_item, $format, array $context) { $attributes = []; - foreach ($field_item as $name => $property) { + foreach ($this->getExposedProperties($field_item) as $name => $property) { $attribute = $this->serializer->normalize($property, $format, $context); if ($attribute instanceof CacheableDependencyInterface && isset($context['cacheability'])) { $context['cacheability']->addCacheableDependency($attribute); @@ -38,4 +39,30 @@ protected function normalizeFieldProperties(FieldItemInterface $field_item, $for return $attributes; } + /** + * Gets exposed properties for the field item. + * + * @param \Drupal\Core\Field\FieldItemInterface $field_item + * The field item being normalized. + * + * @return \Drupal\Core\TypedData\TypedDataInterface[] + * The exposed properties. + */ + public function getExposedProperties(FieldItemInterface $field_item) { + $properties = $field_item->getProperties(TRUE); + /* @var \Drupal\Core\TypedData\TypedDataInterface[] $properties */ + foreach (array_keys($properties) as $property_name) { + $data_definition = $properties[$property_name]->getDataDefinition(); + if ($data_definition instanceof ExposableDataDefinitionInterface) { + if (!$data_definition->isExposed()) { + unset($properties[$property_name]); + } + } + elseif ($data_definition->isComputed()) { + unset($properties[$property_name]); + } + } + return $properties; + } + }