public function TaxonomyTreeBuilder::createTreeNode in Entity Reference Tree Widget 8
Same name and namespace in other branches
- 2.x src/Tree/TaxonomyTreeBuilder.php \Drupal\entity_reference_tree\Tree\TaxonomyTreeBuilder::createTreeNode()
Create a tree node.
Parameters
\Drupal\taxonomy\TermInterface $entity: The entity for the tree node.
array $selected: An anrray for all selected nodes.
Return value
array The tree node for the entity.
Overrides TreeBuilderInterface::createTreeNode
File
- src/
Tree/ TaxonomyTreeBuilder.php, line 74
Class
- TaxonomyTreeBuilder
- Provides a class for building a tree from taxonomy entity.
Namespace
Drupal\entity_reference_tree\TreeCode
public function createTreeNode($entity, array $selected = []) {
$parent = $entity->parents[0];
$text = $entity->name;
\Drupal::moduleHandler()
->alter('entity_reference_tree_create_term_node', $text, $entity);
if ($parent === '0') {
$parent = '#';
}
// Compare the current language with the term's language.
// If the language is different,
// It need to be translated.
if ($entity->langcode !== $this->langCode) {
$term = Term::load($entity->tid);
// Get the translated content.
if ($term
->hasTranslation($this->langCode)) {
$trans = $term
->getTranslation($this->langCode);
$text = $trans
->getName();
}
}
$node = [
// Required.
'id' => $entity->tid,
// Required.
'parent' => $parent,
// Node text.
'text' => $text,
'state' => [
'selected' => FALSE,
],
];
if (in_array($entity->tid, $selected)) {
// Initially selected node.
$node['state']['selected'] = TRUE;
}
return $node;
}