EntityHierarchy.php in Entity Reference Hierarchy 8.2
File
src/Plugin/EntityReferenceSelection/EntityHierarchy.php
View source
<?php
namespace Drupal\entity_hierarchy\Plugin\EntityReferenceSelection;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_hierarchy\Information\AncestryLabelTrait;
use Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface;
use Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory;
use Drupal\entity_hierarchy\Storage\NestedSetStorageFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityHierarchy extends DefaultSelection {
use AncestryLabelTrait;
protected $nestedSetStorageFactory;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->entityTreeNodeMapper = $container
->get('entity_hierarchy.entity_tree_node_mapper');
$instance->keyFactory = $container
->get('entity_hierarchy.nested_set_node_factory');
$instance->nestedSetStorageFactory = $container
->get('entity_hierarchy.nested_set_storage_factory');
return $instance;
}
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$target_type = $this->configuration['target_type'];
$query = $this
->buildEntityQuery($match, $match_operator);
if ($limit > 0) {
$query
->range(0, $limit);
}
$result = $query
->execute();
if (empty($result)) {
return [];
}
$options = [];
$entities = $this->entityTypeManager
->getStorage($target_type)
->loadMultiple($result);
$storage = $this->nestedSetStorageFactory
->get($this->pluginDefinition['field_name'], $target_type);
foreach ($entities as $entity_id => $entity) {
$bundle = $entity
->bundle();
$label = $this
->generateEntityLabelWithAncestry($entity, $storage, $target_type);
$options[$bundle][$entity_id] = Html::escape($label);
}
return $options;
}
}
Classes
Name |
Description |
EntityHierarchy |
Defines a class for entity reference selection that includes lineage. |