function EntityReferenceItemNormalizer::targetEntityIsFieldable

Checks whether the referenced entity is of a fieldable entity type.

Parameters

\Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $item: The reference field item whose target entity needs to be checked.

Return value

bool TRUE when the referenced entity is of a fieldable entity type.

File

core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php, line 120

Class

EntityReferenceItemNormalizer
Converts the Drupal entity reference item object to HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

protected function targetEntityIsFieldable(EntityReferenceItem $item) {
  $target_entity = $item->get('entity')
    ->getValue();
  if ($target_entity !== NULL) {
    return $target_entity instanceof FieldableEntityInterface;
  }
  $referencing_entity = $item->getEntity();
  $target_entity_type_id = $item->getFieldDefinition()
    ->getSetting('target_type');
  // If the entity type is the same as the parent, we can check that. This is
  // just a shortcut to avoid getting the entity type definition and checking
  // the class.
  if ($target_entity_type_id === $referencing_entity->getEntityTypeId()) {
    return $referencing_entity instanceof FieldableEntityInterface;
  }
  // Otherwise, we need to get the class for the type.
  $target_entity_type = $this->entityTypeManager
    ->getDefinition($target_entity_type_id);
  $target_entity_type_class = $target_entity_type->getClass();
  return is_a($target_entity_type_class, FieldableEntityInterface::class, TRUE);
}

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