diff -u b/src/Plugin/Block/MenuBlock.php b/src/Plugin/Block/MenuBlock.php --- b/src/Plugin/Block/MenuBlock.php +++ b/src/Plugin/Block/MenuBlock.php @@ -3,15 +3,15 @@ namespace Drupal\menu_block\Plugin\Block; use Drupal\Core\Access\AccessResult; +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\Core\Session\AccountInterface; use Drupal\system\Entity\Menu; use Drupal\system\Plugin\Block\SystemMenuBlock; use Symfony\Component\DependencyInjection\ContainerInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Menu\MenuActiveTrailInterface; -use Drupal\Core\Menu\MenuLinkTreeInterface; -use Drupal\Core\Menu\MenuTreeParameters; /** * Provides an extended Menu block. @@ -188,7 +188,6 @@ $this->configuration['expand'] = $form_state->getValue('expand'); $this->configuration['parent'] = $form_state->getValue('parent'); $this->configuration['suggestion'] = $form_state->getValue('suggestion'); - $this->configuration['label_type'] = $form_state->getValue('label_type'); } /** @@ -216,6 +215,7 @@ $this->configuration['expand'] = $form_state->getValue('expand'); $this->configuration['parent'] = $form_state->getValue('parent'); $this->configuration['suggestion'] = $form_state->getValue('suggestion'); + $this->configuration['label_type'] = $form_state->getValue('label_type'); } /** @@ -298,7 +298,6 @@ 'expand' => 0, 'parent' => $this->getDerivativeId() . ':', 'suggestion' => strtr($this->getDerivativeId(), '-', '_'), - 'label_type' => self::LABEL_BLOCK, ]; } @@ -364,6 +363,7 @@ 'expand' => 0, 'parent' => $this->getDerivativeId() . ':', 'suggestion' => strtr($this->getDerivativeId(), '-', '_'), + 'label_type' => self::LABEL_BLOCK, ]; } @@ -378,7 +378,7 @@ } /** - * Get the configured block label. + * Gets the configured block label. * * @return string * The configured label. @@ -406,10 +406,10 @@ } /** - * Get the label of the configured menu. + * Gets the label of the configured menu. * * @return string|null - * Menu label or null if no menu exists. + * Menu label or NULL if no menu exists. */ protected function getMenuTitle() { try { @@ -424,7 +424,10 @@ } /** - * @return string + * Gets the title of a fixed parent item. + * + * @return string|null + * Title of the configured (fixed) parent item, or NULL if there is none. */ protected function getFixedParentItemTitle() { $parent = $this->configuration['parent']; @@ -436,12 +439,13 @@ } /** - * Get the active menu item's title. + * Gets the active menu item's title. * - * @return string - * Current menu item title. + * @return string|null + * Currently active menu item title or NULL if there's nothing active. */ protected function getActiveItemTitle() { + /** @var array $active_trail_ids */ $active_trail_ids = $this->getDerivativeActiveTrailIds(); if ($active_trail_ids) { return $this->getLinkTitleFromLink(reset($active_trail_ids)); @@ -449,14 +453,15 @@ } /** - * Get the current menu item's parent menu title. + * Gets the title of the parent of the active menu item. * - * @return string - * The menu item title. + * @return string|null + * The title of the parent of the active menu item, the title of the active + * item if it has no parent, or NULL if there's no active menu item. */ protected function getActiveTrailParentTitle() { + /** @var array $active_trail_ids */ $active_trail_ids = $this->getDerivativeActiveTrailIds(); - if ($active_trail_ids) { if (count($active_trail_ids) === 1) { return $this->getActiveItemTitle(); @@ -466,12 +471,13 @@ } /** - * Get the current menu item's root menu item title. + * Gets the current menu item's root menu item title. * - * @return string - * The menu item title. + * @return string|null + * The root menu item title or NULL if there's no active item. */ protected function getActiveTrailRootTitle() { + /** @var array $active_trail_ids */ $active_trail_ids = $this->getDerivativeActiveTrailIds(); if ($active_trail_ids) { @@ -480,10 +486,10 @@ } /** - * Get an array of the active trail menu link items. + * Gets an array of the active trail menu link items. * * @return array - * The active trail. + * The active trail menu item IDs. */ protected function getDerivativeActiveTrailIds() { $menu_id = $this->getDerivativeId(); @@ -491,32 +497,33 @@ } /** - * Given a menu item ID, get that item's title. + * Gets the title of a given menu item ID. * * @param string $link_id - * Menu Item ID. + * The menu item ID. * - * @return string - * The menu item title. + * @return string|null + * The menu item title or NULL if the given menu item can't be found. */ protected function getLinkTitleFromLink($link_id) { $parameters = new MenuTreeParameters(); $menu = $this->menuTree->load($this->getDerivativeId(), $parameters); - if ($link = $this->findLinkInTree($menu, $link_id)) { + $link = $this->findLinkInTree($menu, $link_id); + if ($link) { return $link->link->getTitle(); } } /** - * Find and return the menu link item from the menu tree. + * Gets the menu link item from the menu tree. * * @param array $menu_tree * Associative array containing the menu link tree data. * @param string $link_id * Menu link id to find. * - * @return \Drupal\Core\Menu\MenuLinkTreeElement - * The link element from the given menu tree. + * @return \Drupal\Core\Menu\MenuLinkTreeElement|null + * The link element from the given menu tree or NULL if it can't be found. */ protected function findLinkInTree(array $menu_tree, $link_id) { if (isset($menu_tree[$link_id])) { @@ -524,7 +531,8 @@ } /** @var \Drupal\Core\Menu\MenuLinkTreeElement $link */ foreach ($menu_tree as $link) { - if ($link = $this->findLinkInTree($link->subtree, $link_id)) { + $link = $this->findLinkInTree($link->subtree, $link_id); + if ($link) { return $link; } }