.../serialization/src/Normalizer/NormalizerBase.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/core/modules/serialization/src/Normalizer/NormalizerBase.php b/core/modules/serialization/src/Normalizer/NormalizerBase.php index 4215fb3..37fde6d 100644 --- a/core/modules/serialization/src/Normalizer/NormalizerBase.php +++ b/core/modules/serialization/src/Normalizer/NormalizerBase.php @@ -3,8 +3,10 @@ namespace Drupal\serialization\Normalizer; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemListInterface; +use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\field_normalization_test\Normalization\TextItemSillyNormalizer; use Drupal\hal\Normalizer\FieldNormalizer as HalFieldNormalizer; use Drupal\hal\Normalizer\FieldItemNormalizer as HalFieldItemNormalizer; @@ -123,9 +125,6 @@ protected static function disallowFieldLevelNormalizers(array $supported, $norma FieldItemNormalizer::class, HalFieldNormalizer::class, HalFieldItemNormalizer::class, - // Entity reference field normalizers for both normalizations. - EntityReferenceFieldItemNormalizer::class, - HalEntityReferenceItemNormalizer::class, // The NULL normalizer allows one to prevent listed classes from ever // being normalized. By default, this is applied only to the 'password' // field type. @@ -137,14 +136,20 @@ protected static function disallowFieldLevelNormalizers(array $supported, $norma HalTimestampItemNormalizer::class, ]; - $is_disallowed = function ($name) { + $is_field_class = function ($name) { return in_array($name, [FieldItemInterface::class, FieldItemListInterface::class], TRUE) || is_subclass_of($name, FieldItemInterface::class, TRUE) || is_subclass_of($name, FieldItemListInterface::class, TRUE); }; + $is_entity_reference_field_class = function ($name) { + return in_array($name, [EntityReferenceItem::class, EntityReferenceFieldItemListInterface::class], TRUE) + || is_subclass_of($name, EntityReferenceItem::class, TRUE) + || is_subclass_of($name, EntityReferenceFieldItemListInterface::class, TRUE); + }; + foreach ($supported as $name) { - if (!in_array($normalizer_class, $allowed_exceptions, TRUE) && $is_disallowed($name)) { + if (!in_array($normalizer_class, $allowed_exceptions, TRUE) && !$is_entity_reference_field_class($name) && $is_field_class($name)) { @trigger_error(sprintf('%s is a normalizer for a @FieldType plugin. This is very strongly discouraged because it requires similar logic to be implemented for every normalization. Please write this as a normalizer for a @DataType plugin instead, then it works for the entire Drupal API-First ecosystem!', $normalizer_class), E_USER_DEPRECATED); } }