Skip to content
Snippets Groups Projects

Resolve #3321699 "Active trail initial visible level title"

@@ -2,6 +2,7 @@
namespace Drupal\menu_block\Plugin\Block;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
@@ -31,6 +32,7 @@ class MenuBlock extends SystemMenuBlock {
*/
const LABEL_BLOCK = 'block';
const LABEL_MENU = 'menu';
const LABEL_VISIBILITY_LEVEL_PARENT = 'visibility_level_parent';
const LABEL_ACTIVE_ITEM = 'active_item';
const LABEL_PARENT = 'parent';
const LABEL_ROOT = 'root';
@@ -57,6 +59,11 @@ class MenuBlock extends SystemMenuBlock {
*/
protected $buildCache;
/**
* Instance reference to the derived menu root.
*/
protected string $menuRoot;
/**
* {@inheritdoc}
*/
@@ -129,6 +136,7 @@ class MenuBlock extends SystemMenuBlock {
'#options' => [
self::LABEL_BLOCK => $this->t('Block title'),
self::LABEL_MENU => $this->t('Menu title'),
self::LABEL_VISIBILITY_LEVEL_PARENT => $this->t('Parent title at initial visibility level'),
self::LABEL_FIXED => $this->t("Fixed parent item's title"),
self::LABEL_ACTIVE_ITEM => $this->t("Active item's title"),
self::LABEL_PARENT => $this->t("Active trail's parent title"),
@@ -151,6 +159,7 @@ class MenuBlock extends SystemMenuBlock {
':input[name="settings[label_display]"]' => ['checked' => TRUE],
':input[name="settings[label_type]"]' => [
['value' => self::LABEL_ACTIVE_ITEM],
['value' => self::LABEL_VISIBILITY_LEVEL_PARENT],
['value' => self::LABEL_PARENT],
['value' => self::LABEL_ROOT],
['value' => self::LABEL_FIXED],
@@ -325,8 +334,8 @@ class MenuBlock extends SystemMenuBlock {
// root based on the parent of the configured start level.
$menu_trail_ids = array_reverse(array_values($parameters->activeTrail));
$offset = ($following && $follow_parent == 'active') ? 2 : 1;
$menu_root = $menu_trail_ids[$level - $offset];
$parameters->setRoot($menu_root);
$this->menuRoot = $menu_trail_ids[$level - $offset];
$parameters->setRoot($this->menuRoot);
if ($following || !$render_parent) {
$parameters->setMinDepth(1);
}
@@ -487,6 +496,9 @@ class MenuBlock extends SystemMenuBlock {
case self::LABEL_PARENT:
return $this->getActiveTrailParentTitle();
case self::LABEL_VISIBILITY_LEVEL_PARENT:
return $this->getVisibilityLevelParentTitle();
case self::LABEL_ROOT:
return $this->getActiveTrailRootTitle();
@@ -582,6 +594,22 @@ class MenuBlock extends SystemMenuBlock {
return NULL;
}
/**
* Gets the current menu item visibility level parent menu item title.
*/
protected function getVisibilityLevelParentTitle(): MarkupInterface|string|null {
$active_trail_ids = $this->getDerivativeActiveTrailIds();
if ($active_trail_ids) {
if ($this->menuRoot && in_array($this->menuRoot, $active_trail_ids)) {
$active_trail_ids = [$this->menuRoot];
}
return $this->getLinkTitleFromLink(end($active_trail_ids));
}
return NULL;
}
/**
* Gets an array of the active trail menu link items.
*
@@ -599,10 +627,10 @@ class MenuBlock extends SystemMenuBlock {
* @param string $link_id
* The menu item ID.
*
* @return string|null
* The menu item title or NULL if the given menu item can't be found.
* @return string|\Drupal\Component\Render\MarkupInterface|null
* The menu item title, link, or NULL if the given menu item can't be found.
*/
protected function getLinkTitleFromLink(string $link_id) {
protected function getLinkTitleFromLink(string $link_id): MarkupInterface|string|null {
// Get the actual parameters so we have the active trail.
$menu_name = $this->getDerivativeId();
$parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name);
Loading