class EntityTreeBuilder in Entity Reference Tree Widget 8
Same name and namespace in other branches
- 2.x src/Tree/EntityTreeBuilder.php \Drupal\entity_reference_tree\Tree\EntityTreeBuilder
Provides a class for building a tree from general entity.
Hierarchy
- class \Drupal\entity_reference_tree\Tree\EntityTreeBuilder implements TreeBuilderInterface
Expanded class hierarchy of EntityTreeBuilder
See also
\Drupal\entity_reference_tree\Tree\TreeBuilderInterface
1 string reference to 'EntityTreeBuilder'
1 service uses EntityTreeBuilder
File
- src/
Tree/ EntityTreeBuilder.php, line 14
Namespace
Drupal\entity_reference_tree\TreeView source
class EntityTreeBuilder implements TreeBuilderInterface {
/**
*
* @var string
* The permission name to access the entity tree.
* The entity storage load function is actually responsible for
* the permission checking for each individual entity.
* So here just use a very weak permission.
*/
private $accessPermission = 'access content';
/**
* The Language code.
*
* @var string
*/
protected $langCode;
/**
* Load all entities from an entity bundle for the tree.
*
* @param string $entityType
* The type of the entity.
*
* @param string $bundleID
* The bundle ID.
*
* @return array
* All entities in the entity bundle.
*/
public function loadTree(string $entityType, string $bundleID, string $langCode = NULL, int $parent = 0, int $max_depth = NULL) {
if ($this
->hasAccess()) {
if ($bundleID === '*') {
// Load all entities regardless bundles.
$entities = \Drupal::entityTypeManager()
->getStorage($entityType)
->loadMultiple();
$hasBundle = FALSE;
}
else {
$hasBundle = TRUE;
$entityStorage = \Drupal::entityTypeManager()
->getStorage($entityType);
// Build the tree node for the bundle.
$tree = [
(object) [
'id' => $bundleID,
// Required.
'parent' => '#',
// Node text.
'text' => $bundleID,
],
];
// Entity query properties.
$properties = [
// Bundle key field.
$entityStorage
->getEntityType()
->getKey('bundle') => $bundleID,
];
// Load all entities matched the conditions.
$entities = $entityStorage
->loadByProperties($properties);
}
// Build the tree.
foreach ($entities as $entity) {
$tree[] = (object) [
'id' => $entity
->id(),
// Required.
'parent' => $hasBundle ? $entity
->bundle() : '#',
// Node text.
'text' => $entity
->label(),
];
}
return $tree;
}
// The user is not allowed to access taxonomy overviews.
return NULL;
}
/**
* Create a tree node.
*
* @param $entity
* The entity for the tree node.
*
* @param array $selected
* A anrray for all selected nodes.
*
* @return array
* The tree node for the entity.
*/
public function createTreeNode($entity, array $selected = []) {
$node = [
// Required.
'id' => $entity->id,
// Required.
'parent' => $entity->parent,
// Node text.
'text' => $entity->text,
'state' => [
'selected' => FALSE,
],
];
if (in_array($entity->id, $selected)) {
// Initially selected node.
$node['state']['selected'] = TRUE;
}
return $node;
}
/**
* Get the ID of a tree node.
*
* @param $entity
* The entity for the tree node.
*
* @return string|int|null
* The id of the tree node for the entity.
*/
public function getNodeId($entity) {
return $entity->id;
}
/**
* Check if a user has the access to the tree.
*
* @param \Drupal\Core\Session\AccountProxyInterface $user
* The user object to check.
*
* @return bool
* If the user has the access to the tree return TRUE,
* otherwise return FALSE.
*/
private function hasAccess(AccountProxyInterface $user = NULL) {
// Check current user as default.
if (empty($user)) {
$user = \Drupal::currentUser();
}
return $user
->hasPermission($this->accessPermission);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityTreeBuilder:: |
private | property | The permission name to access the entity tree. The entity storage load function is actually responsible for the permission checking for each individual entity. So here just use a very weak permission. | |
EntityTreeBuilder:: |
protected | property | The Language code. | |
EntityTreeBuilder:: |
public | function |
Create a tree node. Overrides TreeBuilderInterface:: |
|
EntityTreeBuilder:: |
public | function |
Get the ID of a tree node. Overrides TreeBuilderInterface:: |
|
EntityTreeBuilder:: |
private | function | Check if a user has the access to the tree. | |
EntityTreeBuilder:: |
public | function |
Load all entities from an entity bundle for the tree. Overrides TreeBuilderInterface:: |