Score:1

Dynamically load the menu items created from the links.task.yml file

cn flag

I need to programmatically load the links that are generated from my_module.links.task.yml on another page.

My exact use case is that I have a set of links that are shown when viewing my node page. The links will be shown and hidden based on the current user's access and the access control is handled through the options in the my_module.routing.yml file.

I need to create a separate page that displays node data in a table and I need to show these links in a drop-down option (similar to how the edit button functions when viewing content as an admin).

Is there a way to dynamically get the links so I can then display them in that drop-down?

Score:1
in flag

You can load all module defined menu links (i.e. links defined in *.menu.links.yml with the MenuLinkManager service's getDefinitions() method and then parse the list for only links defined by your module.

$all_module_link_definitions = \Drupal::service('plugin.manager.menu.link')
  ->getDefinitions();

// Next parse the list for only links defined by my_module.
$my_module_link_definitions = [];
foreach ($all_module_link_definitions as $plugin_id => $plugin_definition) {
  if ($plugin_definition['provider'] === 'MY_MODULE') {
    $my_module_link_definitions[$plugin_id] => $plugin_definition;
  }
}

Similarly, you can load all module defined local tasks with the LocalTaskManager service's getDefintions() method and then parse the list for only tasks defined by your module.

$all_module_task_definitions = \Drupal::service('plugin.manager.menu.local_task')
  ->getDefinitions();

// Next parse the list for only tasks defined by my_module.
$my_module_task_definitions = [];
foreach ($all_module_task_definitions as $plugin_id => $plugin_definition) {
  if ($plugin_definition['provider'] === 'MY_MODULE') {
    $my_module_task_definitions[$plugin_id] => $plugin_definition;
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.