You are here

private function EntityReference::entityReferenceFieldsAvailable in Entity Usage 8

Retrieve the entity_reference fields on a given entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity object.

Return value

array An array of field_names that could reference to other content entities.

3 calls to EntityReference::entityReferenceFieldsAvailable()
EntityReference::trackOnEntityCreation in src/Plugin/EntityUsage/Track/EntityReference.php
Track usage updates on the creation of entities.
EntityReference::trackOnEntityDeletion in src/Plugin/EntityUsage/Track/EntityReference.php
Track usage updates on the deletion of entities.
EntityReference::trackOnEntityUpdate in src/Plugin/EntityUsage/Track/EntityReference.php
Track usage updates on the edition of entities.

File

src/Plugin/EntityUsage/Track/EntityReference.php, line 200

Class

EntityReference
Tracks usage of entities related in entity_reference fields.

Namespace

Drupal\entity_usage\Plugin\EntityUsage\Track

Code

private function entityReferenceFieldsAvailable(ContentEntityInterface $entity) {
  $return_fields = [];
  $fields_on_entity = $this->entityFieldManager
    ->getFieldDefinitions($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  $entityref_fields_on_this_entity_type = [];
  if (!empty($this->entityFieldManager
    ->getFieldMapByFieldType('entity_reference')[$entity
    ->getEntityTypeId()])) {
    $entityref_fields_on_this_entity_type = $this->entityFieldManager
      ->getFieldMapByFieldType('entity_reference')[$entity
      ->getEntityTypeId()];
  }
  $entityref_on_this_bundle = array_intersect_key($fields_on_entity, $entityref_fields_on_this_entity_type);

  // Clean out basefields.
  $basefields = $this->entityFieldManager
    ->getBaseFieldDefinitions($entity
    ->getEntityTypeId());
  $entityref_on_this_bundle = array_diff_key($entityref_on_this_bundle, $basefields);
  if (!empty($entityref_on_this_bundle)) {

    // Make sure we only leave the fields that are referencing content
    // entities.
    foreach ($entityref_on_this_bundle as $key => $entityref) {
      $target_type = $entityref_on_this_bundle[$key]
        ->getItemDefinition()
        ->getSettings()['target_type'];
      $entity_type = $this->entityTypeManager
        ->getStorage($target_type)
        ->getEntityType();
      if ($entity_type instanceof ConfigEntityTypeInterface) {
        unset($entityref_on_this_bundle[$key]);
      }
    }
    $return_fields = array_keys($entityref_on_this_bundle);
  }
  return $return_fields;
}