diff --git a/src/Plugin/Block/MenuBlock.php b/src/Plugin/Block/MenuBlock.php index fab522a..15edf4f 100644 --- a/src/Plugin/Block/MenuBlock.php +++ b/src/Plugin/Block/MenuBlock.php @@ -47,7 +47,20 @@ class MenuBlock extends SystemMenuBlock { /** @var \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector */ $menu_parent_selector = \Drupal::service('menu.parent_form_selector'); - $form['advanced']['parent'] = $menu_parent_selector->parentSelectElement($config['parent'], '', $menus); + if (strpos($config['parent'], 'active_trail') === false) { + $form['advanced']['parent'] = $menu_parent_selector->parentSelectElement($config['parent'], '', $menus); + } + else { + $form['advanced']['parent'] = [ + '#type' => 'select', + '#options' => $menu_parent_selector->getParentSelectOptions('', $menus), + '#default_value' => $config['parent'], + ]; + } + $form['advanced']['parent']['#options'] += [ + $menu_name . ':active_trail' => $this->t('')->render(), + $menu_name . ':active_trail_parent' => $this->t('')->render(), + ]; $form['advanced']['parent'] += [ '#title' => $this->t('Fixed parent item'), @@ -133,7 +146,22 @@ class MenuBlock extends SystemMenuBlock { } // When a fixed parent item is set, root the menu tree at the given ID. if ($menuLinkID = str_replace($menu_name . ':', '', $parent)) { - $parameters->setRoot($menuLinkID); + // Active trail or Active trail parent option. + if (strpos($menuLinkID, 'active_trail') !== false) { + $trailIds = $this->menuActiveTrail->getActiveTrailIds($menu_name); + $trailIds = array_reverse(array_filter($trailIds)); + if ($menuLinkID == 'active_trail') { + $menuLinkID = end($trailIds); + } + // Active trail parent. + else { + array_pop($trailIds); + $menuLinkID = end($trailIds); + } + } + if ($menuLinkID) { + $parameters->setRoot($menuLinkID); + } // If the starting level is 1, we always want the child links to appear, // but the requested tree may be empty if the tree does not contain the