You are here

class ParentCandidate 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

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

Hierarchy

Expanded class hierarchy of ParentCandidate

1 string reference to 'ParentCandidate'
entity_hierarchy.services.yml in ./entity_hierarchy.services.yml
entity_hierarchy.services.yml
1 service uses ParentCandidate
entity_hierarchy.information.parent_candidate in ./entity_hierarchy.services.yml
Drupal\entity_hierarchy\Information\ParentCandidate

File

src/Information/ParentCandidate.php, line 12

Namespace

Drupal\entity_hierarchy\Information
View source
class ParentCandidate implements ParentCandidateInterface {

  /**
   * Entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $bundleInfo;

  /**
   * Constructs a new ReorderChildrenAccess object.
   *
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
   *   Entity field manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo
   *   Bundle Info.
   */
  public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityTypeBundleInfoInterface $bundleInfo) {
    $this->entityFieldManager = $entityFieldManager;
    $this->bundleInfo = $bundleInfo;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParentCandidate::$bundleInfo protected property Bundle info.
ParentCandidate::$entityFieldManager protected property Entity field manager.
ParentCandidate::getCandidateBundles public function Gets all bundles that allow referencing this entity as a parent. Overrides ParentCandidateInterface::getCandidateBundles
ParentCandidate::getCandidateFields public function Gets all fields that allow referencing this entity as a parent. Overrides ParentCandidateInterface::getCandidateFields
ParentCandidate::__construct public function Constructs a new ReorderChildrenAccess object.