You are here

trait EntityReferenceFieldItemNormalizerTrait in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizerTrait.php \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizerTrait
  2. 10 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizerTrait.php \Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizerTrait

Converts empty reference values for entity reference items.

Hierarchy

1 file declares its use of EntityReferenceFieldItemNormalizerTrait
EntityReferenceItemNormalizer.php in core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php

File

core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizerTrait.php, line 10

Namespace

Drupal\serialization\Normalizer
View source
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';
  }

}

Members