You are here

public function ParentCandidate::getCandidateFields in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Information/ParentCandidate.php \Drupal\entity_hierarchy\Information\ParentCandidate::getCandidateFields()

Gets all fields that allow referencing this entity as a parent.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: Entity to get parent candidate fields for.

Return value

array Field names that would allow referencing this entity as a parent.

Overrides ParentCandidateInterface::getCandidateFields

1 call to ParentCandidate::getCandidateFields()
ParentCandidate::getCandidateBundles in src/Information/ParentCandidate.php
Gets all bundles that allow referencing this entity as a parent.

File

src/Information/ParentCandidate.php, line 44

Class

ParentCandidate
Defines a class for determining if an entity is a parent candidate.

Namespace

Drupal\entity_hierarchy\Information

Code

public function getCandidateFields(EntityInterface $entity) {
  $fields = $this->entityFieldManager
    ->getFieldMapByFieldType('entity_reference_hierarchy');
  $valid_fields = [];
  $entity_type = $entity
    ->getEntityTypeId();
  if (isset($fields[$entity_type])) {

    // See if any bundles point to this entity.
    // We only consider this entity type, there is no point in a hierarchy
    // that spans entity-types as you cannot have more than a single level.
    foreach ($fields[$entity_type] as $field_name => $detail) {
      foreach ($detail['bundles'] as $bundle) {

        /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
        $field = $this->entityFieldManager
          ->getFieldDefinitions($entity_type, $bundle)[$field_name];
        $settings = $field
          ->getSetting('handler_settings');
        if (!isset($settings['target_bundles']) || in_array($entity
          ->bundle(), $settings['target_bundles'], TRUE)) {

          // No target bundles means any can be referenced, return early.
          $valid_fields[] = $field_name;
          continue 2;
        }
      }
    }
  }
  return $valid_fields;
}