EntityReferenceFieldItemNormalizerTrait.php

Same filename in other branches
  1. 9 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizerTrait.php
  2. 10 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizerTrait.php
  3. 11.x core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizerTrait.php

Namespace

Drupal\serialization\Normalizer

File

core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizerTrait.php

View source
<?php

namespace Drupal\serialization\Normalizer;

use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;

/**
 * Converts empty reference values for entity reference items.
 */
trait EntityReferenceFieldItemNormalizerTrait {
    protected function normalizeRootReferenceValue(&$values, EntityReferenceItem $field_item) {
        // @todo Generalize for all tree-structured entity types.
        if ($this->fieldItemReferencesTaxonomyTerm($field_item) && empty($values['target_id'])) {
            $values['target_id'] = NULL;
        }
    }
    
    /**
     * Determines if a field item references a taxonomy term.
     *
     * @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item
     *
     * @return bool
     */
    protected function fieldItemReferencesTaxonomyTerm(EntityReferenceItem $field_item) {
        return $field_item->getFieldDefinition()
            ->getSetting('target_type') === 'taxonomy_term';
    }

}

Traits

Title Deprecated Summary
EntityReferenceFieldItemNormalizerTrait Converts empty reference values for entity reference items.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.