diff --git a/src/Tree/EntityTreeBuilder.php b/src/Tree/EntityTreeBuilder.php index 6baa9c41e7e75f62cdbb5abf11c59b9447cd20c9..f1dab976d841ba9eef3ae06086ee7fa43b240c23 100644 --- a/src/Tree/EntityTreeBuilder.php +++ b/src/Tree/EntityTreeBuilder.php @@ -23,6 +23,13 @@ class EntityTreeBuilder implements TreeBuilderInterface { */ private $accessPermission = 'access content'; + /** + * The Language code. + * + * @var string + */ + protected $langCode; + /** * Load all entities from an entity bundle for the tree. * @@ -35,7 +42,7 @@ class EntityTreeBuilder implements TreeBuilderInterface { * @return array * All entities in the entity bundle. */ - public function loadTree(string $entityType, string $bundleID, int $parent = 0, int $max_depth = NULL) { + 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. diff --git a/src/Tree/TaxonomyTreeBuilder.php b/src/Tree/TaxonomyTreeBuilder.php index 3ef854ed5ccbc0e6797f457b30b555d8ffc07144..044addc827a87028729ba9ecf665b3812b5ac883 100644 --- a/src/Tree/TaxonomyTreeBuilder.php +++ b/src/Tree/TaxonomyTreeBuilder.php @@ -3,6 +3,7 @@ namespace Drupal\entity_reference_tree\Tree; use Drupal\Core\Session\AccountProxyInterface; +use Drupal\taxonomy\Entity\Term; /** * Provides a class for building a tree from taxonomy entity. @@ -14,12 +15,20 @@ use Drupal\Core\Session\AccountProxyInterface; class TaxonomyTreeBuilder implements TreeBuilderInterface { /** + * The permission name to access the tree. * * @var string - * The permission name to access the tree. + * */ private $accessPermission = 'access taxonomy overview'; + /** + * The Language code. + * + * @var string + */ + protected $langCode; + /** * Load all entities from an entity bundle for the tree. * @@ -32,10 +41,20 @@ class TaxonomyTreeBuilder implements TreeBuilderInterface { * @return array * All entities in the entity bundle. */ - public function loadTree(string $entityType, string $bundleID, int $parent = 0, int $max_depth = NULL) { + public function loadTree(string $entityType, string $bundleID, string $langCode = NULL, int $parent = 0, int $max_depth = NULL) { + // Setup the language code for this tree. + // Use current language by default. + if (empty($langCode)) { + $this->langCode = \Drupal::service('language_manager')->getCurrentLanguage()->getId(); + } + else { + $this->langCode = $langCode; + } + if ($this->hasAccess()) { return \Drupal::entityTypeManager()->getStorage($entityType)->loadTree($bundleID, $parent, $max_depth); } + // The user is not allowed to access taxonomy overviews. return NULL; } @@ -43,29 +62,42 @@ class TaxonomyTreeBuilder implements TreeBuilderInterface { /** * Create a tree node. * - * @param $entity + * @param \Drupal\taxonomy\TermInterface $entity * The entity for the tree node. * * @param array $selected - * A anrray for all selected nodes. + * An anrray for all selected nodes. * * @return array * The tree node for the entity. */ public function createTreeNode($entity, array $selected = []) { $parent = $entity->parents[0]; + $text = $entity->name; 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' => $entity->name, + 'text' => $text, 'state' => ['selected' => FALSE], ]; @@ -80,7 +112,7 @@ class TaxonomyTreeBuilder implements TreeBuilderInterface { /** * Get the ID of a tree node. * - * @param $entity + * @param \Drupal\taxonomy\TermInterface $entity * The entity for the tree node. * * @return string|int|null diff --git a/src/Tree/TreeBuilderInterface.php b/src/Tree/TreeBuilderInterface.php index 25d24402ecd3190aba3debb88d996dcaaff21c78..356f9c176f3b1e67120d136a21215cb4e1ab00b0 100644 --- a/src/Tree/TreeBuilderInterface.php +++ b/src/Tree/TreeBuilderInterface.php @@ -21,7 +21,7 @@ interface TreeBuilderInterface { * @return array * All entities in the entity bundle. */ - public function loadTree(string $entityType, string $bundleID, int $parent = 0, int $max_depth = NULL); + public function loadTree(string $entityType, string $bundleID, string $langCode = NULL, int $parent = 0, int $max_depth = NULL); /** * Create a tree node.