Issue #3345071: Add menu setting type
Merge request reports
Activity
added 1 commit
61 ContainerInterface $container, 62 array $configuration, 63 $plugin_id, 64 $plugin_definition 65 ) { 66 $plugin = new static( 67 $configuration, 68 $plugin_id, 69 $plugin_definition, 70 $container->get('module_handler'), 71 $container->get('entity_type.manager'), 72 $container->get('menu.link_tree'), 73 $container->get('breadcrumb'), 74 ); 75 /** @var \Drupal\Core\StringTranslation\TranslationInterface $translation */ 76 $translation = $container->get('string_translation'); As the parent class already have a create method. You can get rid of the constructor and only add your additional services. Like in https://git.drupalcode.org/project/file_extractor/-/blob/4.1.x/src/Plugin/file_extractor/Extractor/TikaServerExtractor.php#L50 or other modules I maintain if you need other code examples.
151 if (empty($value["menu"])) { 152 // The value comes from the plugin setting, but no menu was selected. 153 return []; 154 } 155 if ("_breadcrumb" === $value["menu"]) { 156 return $this->getBreadcrumbItems(); 157 } 158 159 return $this->getMenuItems($value); 160 } 161 162 /** 163 * Get breadcrumb items. 164 */ 165 private function getBreadcrumbItems(): array { 166 $breadcrumb = $this->breadcrumbManager->build($this->routeMatch); By loading the breacrumb that way, the hook_preprocess_breadcrumb will not be triggered.
Seeing https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/lib/Drupal/Core/Breadcrumb/BreadcrumbManager.php#L94, the breadcrumb builder triggers an alter hook. So I guess module altering the breadcrumb will use it. And themes which alter the breadcrumb like Bootstrap 3 in https://git.drupalcode.org/project/bootstrap/-/blob/8.x-3.x/src/Plugin/Preprocess/Breadcrumb.php#L32, will have to do it in another way (thinking about that for UI Suite Bootstrap ;)). Also it is not the responsability of a theme to add or remove links, only style it. So no problem for me.
180 private function getMenuItems($value): array { 181 $level = (int) \array_key_exists("level", $value) ? $value["level"] : 1; 182 $depth = (int) \array_key_exists("depth", $value) ? $value["depth"] : 0; 183 $parameters = new MenuTreeParameters(); 184 $parameters->setMinDepth($level); 185 186 // When the depth is configured to zero, there is no depth limit. When depth 187 // is non-zero, it indicates the number of levels that must be displayed. 188 // Hence this is a relative depth that we must convert to an actual 189 // (absolute) depth, that may never exceed the maximum depth. 190 if ($depth > 0) { 191 $parameters->setMaxDepth(min($level + $depth - 1, $this->menuLinkTree->maxDepth())); 192 } 193 194 $tree = $this->menuLinkTree->load($value["menu"], $parameters); 195 $manipulators = [ Unfortunately menu API is not easily extensible. As it requires to hardcode the manipulators each time you have to use it.
I foresee compatibility problems with contrib modules like Menu manipulators: https://git.drupalcode.org/project/menu_manipulator/-/blob/3.0.x/menu_manipulator.module#L62
So maybe adding a note in the setting description to warn that for additional contrib or custom code support, an override of the menu settings plugin will be needed. And also think the code of the plugin to be easily extended then.
added 1 commit
added 6 commits
-
b741fa04 - 1 commit from branch
project:8.x-2.x
- 75ca8fe7 - Issue #3345071: first commit, issue not completed
- 4caf5570 - Issue #3345071: add hook invocation for menu link attributes, add breadcrumbs
- 5fb7ac8b - Issue #3345071: URL normalization
- 683935c6 - Issue #3345071: Cache management, URL attributes, presenter templates
- e750c8b5 - Issue #3345071: Better pager management
Toggle commit list-
b741fa04 - 1 commit from branch