ParentCandidate.php in Entity Reference Hierarchy 3.x
File
src/Information/ParentCandidate.php
View source
<?php
namespace Drupal\entity_hierarchy\Information;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
class ParentCandidate implements ParentCandidateInterface {
protected $entityFieldManager;
protected $bundleInfo;
public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityTypeBundleInfoInterface $bundleInfo) {
$this->entityFieldManager = $entityFieldManager;
$this->bundleInfo = $bundleInfo;
}
public function getCandidateFields(EntityInterface $entity) {
$fields = $this->entityFieldManager
->getFieldMapByFieldType('entity_reference_hierarchy');
$valid_fields = [];
$entity_type = $entity
->getEntityTypeId();
if (isset($fields[$entity_type])) {
foreach ($fields[$entity_type] as $field_name => $detail) {
foreach ($detail['bundles'] as $bundle) {
$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)) {
$valid_fields[] = $field_name;
continue 2;
}
}
}
}
return $valid_fields;
}
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) {
$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)) {
$valid_bundles[$bundle] = $bundle;
}
}
$bundles[$field_name] = array_intersect_key($bundleInfo, $valid_bundles);
}
return $bundles;
}
}
Classes
Name |
Description |
ParentCandidate |
Defines a class for determining if an entity is a parent candidate. |