use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
-use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
* id = "system_menu_block",
* admin_label = @Translation("Menu"),
* category = @Translation("Menus"),
- * deriver = "Drupal\system\Plugin\Derivative\SystemMenuBlock"
+ * deriver = "Drupal\system\Plugin\Derivative\SystemMenuBlock",
+ * forms = {
+ * "settings_tray" = "\Drupal\system\Form\SystemMenuOffCanvasForm",
+ * },
* )
*/
class SystemMenuBlock extends BlockBase implements ContainerFactoryPluginInterface {
*/
protected $menuTree;
- /**
- * The active menu trail service.
- *
- * @var \Drupal\Core\Menu\MenuActiveTrailInterface
- */
- protected $menuActiveTrail;
-
/**
* Constructs a new SystemMenuBlock.
*
* The plugin implementation definition.
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
* The menu tree service.
- * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
- * The active menu trail service.
*/
- public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail) {
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_tree) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->menuTree = $menu_tree;
- $this->menuActiveTrail = $menu_active_trail;
}
/**
$configuration,
$plugin_id,
$plugin_definition,
- $container->get('menu.link_tree'),
- $container->get('menu.active_trail')
+ $container->get('menu.link_tree')
);
}
$parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth()));
}
+ // For menu blocks with start level greater than 1, only show menu items
+ // from the current active trail. Adjust the root according to the current
+ // position in the menu in order to determine if we can show the subtree.
+ if ($level > 1) {
+ if (count($parameters->activeTrail) >= $level) {
+ // Active trail array is child-first. Reverse it, and pull the new menu
+ // root based on the parent of the configured start level.
+ $menu_trail_ids = array_reverse(array_values($parameters->activeTrail));
+ $menu_root = $menu_trail_ids[$level - 1];
+ $parameters->setRoot($menu_root)->setMinDepth(1);
+ if ($depth > 0) {
+ $parameters->setMaxDepth(min($level - 1 + $depth - 1, $this->menuTree->maxDepth()));
+ }
+ }
+ else {
+ return [];
+ }
+ }
+
$tree = $this->menuTree->load($menu_name, $parameters);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],