Commit e3e03023 authored by alexpott's avatar alexpott

Issue #474004 by mdrummond, kim.pepper, Wim Leers, jibran, tim.plunkett,...

Issue #474004 by mdrummond, kim.pepper, Wim Leers, jibran, tim.plunkett, joachim | JohnAlbin: Add options to system menu block so primary and secondary menus can be blocks rather than variables.
parent 4697eb9c
......@@ -107,7 +107,7 @@ public function getCurrentRouteMenuTreeParameters($menu_name) {
ksort($route_parameters);
$cid = 'current-route-parameters:' . $menu_name . ':route:' . $this->routeMatch->getRouteName() . ':route_parameters:' . serialize($route_parameters);
if (!isset($this->cachedCurrentRouteParameters[$menu_name])) {
if (!isset($this->cachedCurrentRouteParameters[$cid])) {
$cache = $this->cache->get($cid);
if ($cache && $cache->data) {
$parameters = $cache->data;
......
......@@ -355,6 +355,17 @@ block.settings.system_branding_block:
type: boolean
label: 'Use site slogan'
block.settings.system_menu_block:*:
type: block_settings
label: 'Menu block'
mapping:
level:
type: integer
label: 'Starting level'
depth:
type: integer
label: 'Maximum number of levels'
condition.plugin.request_path:
type: condition.plugin
mapping:
......
......@@ -9,12 +9,13 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
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;
/**
* Provides a generic Menu block.
*
......@@ -74,12 +75,76 @@ public static function create(ContainerInterface $container, array $configuratio
);
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$config = $this->configuration;
$defaults = $this->defaultConfiguration();
$form['menu_levels'] = array(
'#type' => 'details',
'#title' => t('Menu levels'),
// Open if not set to defaults.
'#open' => $defaults['level'] !== $config['level'] || $defaults['depth'] !== $config['depth'],
'#process' => [[get_class(), 'processMenuLevelParents']],
);
$options = range(0, $this->menuTree->maxDepth());
unset($options[0]);
$form['menu_levels']['level'] = array(
'#type' => 'select',
'#title' => $this->t('Initial menu level'),
'#default_value' => $config['level'],
'#options' => $options,
'#description' => $this->t('The menu will only be visible if the menu item for the current page is at or below the selected starting level. Select level 1 to always keep this menu visible.'),
'#required' => TRUE,
);
$options[0] = $this->t('Unlimited');
$form['menu_levels']['depth'] = array(
'#type' => 'select',
'#title' => $this->t('Maximum number of menu levels to display'),
'#default_value' => $config['depth'],
'#options' => $options,
'#description' => $this->t('The maximum number of menu levels to show, starting from the initial menu level. For example: with an initial level 2 and a maximum number of 3, menu levels 2, 3 and 4 can be displayed.'),
'#required' => TRUE,
);
return $form;
}
/**
* Form API callback: Processes the menu_levels field element.
*
* Adjusts the #parents of menu_levels to save its children at the top level.
*/
public static function processMenuLevelParents(&$element, FormStateInterface $form_state, &$complete_form) {
array_pop($element['#parents']);
return $element;
}
/**
* {@inheritdoc}
*/
public function build() {
$menu_name = $this->getDerivativeId();
$parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name);
// Adjust the menu tree parameters based on the block's configuration.
$level = $this->configuration['level'];
$depth = $this->configuration['depth'];
$parameters->setMinDepth($level);
// When the depth is configured to zero, there is no depth limit. When depth
// is non-zero, it indicates the number of levels that must be displayed.
// Hence this is a relative depth that we must convert to an actual
// (absolute) depth, that may never exceed the maximum depth.
if ($depth > 0) {
$parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth()));
}
$tree = $this->menuTree->load($menu_name, $parameters);
$manipulators = array(
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
......@@ -101,7 +166,11 @@ public function defaultConfiguration() {
// 1) it is possible to set a different max age for individual blocks, since
// this is just the default value.
// 2) modules can modify caching by implementing hook_block_view_alter()
return array('cache' => array('max_age' => \Drupal\Core\Cache\Cache::PERMANENT));
return [
'cache' => array('max_age' => Cache::PERMANENT),
'level' => 1,
'depth' => 0,
];
}
/**
......
......@@ -10,6 +10,11 @@ settings:
label: Administration
provider: system
label_display: visible
cache:
max_age: -1
contexts: { }
level: 1
depth: 0
dependencies:
entity:
- system.menu.admin
......
......@@ -10,6 +10,11 @@ settings:
label: Tools
provider: system
label_display: visible
cache:
max_age: -1
contexts: { }
level: 1
depth: 0
dependencies:
entity:
- system.menu.tools
......
......@@ -11,6 +11,11 @@ settings:
label: 'Footer menu'
provider: system
label_display: visible
cache:
max_age: -1
contexts: { }
level: 1
depth: 0
dependencies:
entity:
- system.menu.footer
......
......@@ -11,6 +11,11 @@ settings:
label: Tools
provider: system
label_display: visible
cache:
max_age: -1
contexts: { }
level: 1
depth: 0
dependencies:
entity:
- system.menu.tools
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment