diff --git a/src/Plugin/Block/MenuBlock.php b/src/Plugin/Block/MenuBlock.php index 5c58b37..12758d5 100644 --- a/src/Plugin/Block/MenuBlock.php +++ b/src/Plugin/Block/MenuBlock.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Menu\MenuActiveTrailInterface; use Drupal\Core\Menu\MenuLinkTreeInterface; +use Drupal\Core\Menu\MenuTreeParameters; use Drupal\system\Entity\Menu; use Drupal\system\Plugin\Block\SystemMenuBlock; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -68,7 +69,8 @@ class MenuBlock extends SystemMenuBlock { * {@inheritdoc} */ public function getConfiguration() { - $this->setConfigurationValue('label', $this->blockLabel()); + $label = $this->blockLabel() ?: $this->label(); + $this->setConfigurationValue('label', $label); return $this->configuration; } @@ -352,10 +354,9 @@ class MenuBlock extends SystemMenuBlock { */ protected function getActiveItemTitle() { $active_trail_ids = $this->getDerivativeActiveTrailIds(); - if ($active_trail_ids) { return $this->getLinkTitleFromLink(reset($active_trail_ids)); - } + }; return ''; } @@ -401,7 +402,7 @@ class MenuBlock extends SystemMenuBlock { * @return array * The active trail. */ - private function getDerivativeActiveTrailIds() { + protected function getDerivativeActiveTrailIds() { $menu_id = $this->getDerivativeId(); return array_filter($this->menuActiveTrail->getActiveTrailIds($menu_id)); } @@ -415,34 +416,35 @@ class MenuBlock extends SystemMenuBlock { * @return string * The given item title or empty string. */ - private function getLinkTitleFromLink($link_id) { - list($entity_type, $entity_uuid) = explode(':', $link_id); - - try { - $entity_query = $this->entityTypeManager->getStorage($entity_type) - ->getQuery('AND'); - } - catch (\Exception $e) { - return ''; + protected function getLinkTitleFromLink($link_id) { + $parameters = new MenuTreeParameters(); + $menu = $this->menuTree->load($this->getDerivativeId(), $parameters); + if($link = $this->findLinkInTree($menu, $link_id)){ + return $link->link->getTitle(); } + } - $entity_query->condition('uuid', $entity_uuid); - $parent_ids = $entity_query->execute(); - - if ($parent_ids) { - $parent_id = reset($parent_ids); - try { - $parent = $this->entityTypeManager->getStorage('menu_link_content') - ->load($parent_id); - } - catch (\Exception $e) { - return ''; + /** + * Find and return the menu link item from the menu tree. + * + * @param array $menu_tree + * Array of menu link id's and its subtree. + * @param string $link_id + * Menu link id. + * + * @return \Drupal\Core\Menu\MenuLinkTreeElement|bool + */ + protected function findLinkInTree(array $menu_tree, $link_id){ + if(isset($menu_tree[$link_id])){ + return $menu_tree[$link_id]; + } + /** @var \Drupal\Core\Menu\MenuLinkTreeElement $link */ + foreach ($menu_tree as $uuid => $link) { + if ($link = $this->findLinkInTree($link->subtree, $link_id)) { + return $link; } - - return $parent->getTitle(); } - - return ''; + return FALSE; } }