You are here

public function ParentCandidate::getCandidateBundles in Entity Reference Hierarchy 8.2

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

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

Parameters

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

Return value

array Bundles that support this entity as parent, keyed by field name.

Overrides ParentCandidateInterface::getCandidateBundles

File

src/Information/ParentCandidate.php, line 71

Class

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

Namespace

Drupal\entity_hierarchy\Information

Code

public function getCandidateBundles(EntityInterface $entity) {
  $fields = $this->entityFieldManager
    ->getFieldMap()[$entity
    ->getEntityTypeId()];
  $bundles = [];
  $bundleInfo = $this->bundleInfo
    ->getBundleInfo($entity
    ->getEntityTypeId());
  foreach ($this
    ->getCandidateFields($entity) as $field_name) {
    $valid_bundles = [];
    foreach ($fields[$field_name]['bundles'] as $bundle) {

      /** @var \Drupal\Core\Field\FieldDefinitionInterface $field */
      $field = $this->entityFieldManager
        ->getFieldDefinitions($entity
        ->getEntityTypeId(), $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.
        $valid_bundles[$bundle] = $bundle;
      }
    }
    $bundles[$field_name] = array_intersect_key($bundleInfo, $valid_bundles);
  }
  return $bundles;
}