comparison core/modules/system/src/Plugin/Block/SystemMenuBlock.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
3 namespace Drupal\system\Plugin\Block; 3 namespace Drupal\system\Plugin\Block;
4 4
5 use Drupal\Core\Block\BlockBase; 5 use Drupal\Core\Block\BlockBase;
6 use Drupal\Core\Cache\Cache; 6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Form\FormStateInterface; 7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Menu\MenuActiveTrailInterface;
8 use Drupal\Core\Menu\MenuLinkTreeInterface; 9 use Drupal\Core\Menu\MenuLinkTreeInterface;
10 use Drupal\Core\Menu\MenuTreeParameters;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface; 11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface; 12 use Symfony\Component\DependencyInjection\ContainerInterface;
11 13
12 /** 14 /**
13 * Provides a generic Menu block. 15 * Provides a generic Menu block.
30 * @var \Drupal\Core\Menu\MenuLinkTreeInterface 32 * @var \Drupal\Core\Menu\MenuLinkTreeInterface
31 */ 33 */
32 protected $menuTree; 34 protected $menuTree;
33 35
34 /** 36 /**
37 * The active menu trail service.
38 *
39 * @var \Drupal\Core\Menu\MenuActiveTrailInterface
40 */
41 protected $menuActiveTrail;
42
43 /**
35 * Constructs a new SystemMenuBlock. 44 * Constructs a new SystemMenuBlock.
36 * 45 *
37 * @param array $configuration 46 * @param array $configuration
38 * A configuration array containing information about the plugin instance. 47 * A configuration array containing information about the plugin instance.
39 * @param string $plugin_id 48 * @param string $plugin_id
40 * The plugin_id for the plugin instance. 49 * The plugin_id for the plugin instance.
41 * @param array $plugin_definition 50 * @param array $plugin_definition
42 * The plugin implementation definition. 51 * The plugin implementation definition.
43 * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree 52 * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
44 * The menu tree service. 53 * The menu tree service.
45 */ 54 * @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
46 public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_tree) { 55 * The active menu trail service.
56 */
57 public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail = NULL) {
47 parent::__construct($configuration, $plugin_id, $plugin_definition); 58 parent::__construct($configuration, $plugin_id, $plugin_definition);
48 $this->menuTree = $menu_tree; 59 $this->menuTree = $menu_tree;
60 if ($menu_active_trail === NULL) {
61 @trigger_error('The menu.active_trail service must be passed to SystemMenuBlock::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2669550.', E_USER_DEPRECATED);
62 $menu_active_trail = \Drupal::service('menu.active_trail');
63 }
64 $this->menuActiveTrail = $menu_active_trail;
49 } 65 }
50 66
51 /** 67 /**
52 * {@inheritdoc} 68 * {@inheritdoc}
53 */ 69 */
54 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { 70 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55 return new static( 71 return new static(
56 $configuration, 72 $configuration,
57 $plugin_id, 73 $plugin_id,
58 $plugin_definition, 74 $plugin_definition,
59 $container->get('menu.link_tree') 75 $container->get('menu.link_tree'),
76 $container->get('menu.active_trail')
60 ); 77 );
61 } 78 }
62 79
63 /** 80 /**
64 * {@inheritdoc} 81 * {@inheritdoc}
96 '#options' => $options, 113 '#options' => $options,
97 '#description' => $this->t('This maximum number includes the initial level.'), 114 '#description' => $this->t('This maximum number includes the initial level.'),
98 '#required' => TRUE, 115 '#required' => TRUE,
99 ]; 116 ];
100 117
118 $form['menu_levels']['expand_all_items'] = [
119 '#type' => 'checkbox',
120 '#title' => $this->t('Expand all menu items'),
121 '#default_value' => !empty($config['expand_all_items']),
122 '#description' => $this->t('Override the option found on each menu link used for expanding children and instead display the whole menu tree as expanded.'),
123 ];
124
101 return $form; 125 return $form;
102 } 126 }
103 127
104 /** 128 /**
105 * Form API callback: Processes the menu_levels field element. 129 * Form API callback: Processes the menu_levels field element.
115 * {@inheritdoc} 139 * {@inheritdoc}
116 */ 140 */
117 public function blockSubmit($form, FormStateInterface $form_state) { 141 public function blockSubmit($form, FormStateInterface $form_state) {
118 $this->configuration['level'] = $form_state->getValue('level'); 142 $this->configuration['level'] = $form_state->getValue('level');
119 $this->configuration['depth'] = $form_state->getValue('depth'); 143 $this->configuration['depth'] = $form_state->getValue('depth');
144 $this->configuration['expand_all_items'] = $form_state->getValue('expand_all_items');
120 } 145 }
121 146
122 /** 147 /**
123 * {@inheritdoc} 148 * {@inheritdoc}
124 */ 149 */
125 public function build() { 150 public function build() {
126 $menu_name = $this->getDerivativeId(); 151 $menu_name = $this->getDerivativeId();
127 $parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name); 152 if ($this->configuration['expand_all_items']) {
153 $parameters = new MenuTreeParameters();
154 $active_trail = $this->menuActiveTrail->getActiveTrailIds($menu_name);
155 $parameters->setActiveTrail($active_trail);
156 }
157 else {
158 $parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name);
159 }
128 160
129 // Adjust the menu tree parameters based on the block's configuration. 161 // Adjust the menu tree parameters based on the block's configuration.
130 $level = $this->configuration['level']; 162 $level = $this->configuration['level'];
131 $depth = $this->configuration['depth']; 163 $depth = $this->configuration['depth'];
132 $parameters->setMinDepth($level); 164 $parameters->setMinDepth($level);
171 */ 203 */
172 public function defaultConfiguration() { 204 public function defaultConfiguration() {
173 return [ 205 return [
174 'level' => 1, 206 'level' => 1,
175 'depth' => 0, 207 'depth' => 0,
208 'expand_all_items' => FALSE,
176 ]; 209 ];
177 } 210 }
178 211
179 /** 212 /**
180 * {@inheritdoc} 213 * {@inheritdoc}