1

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?

1 Answer 1

1

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;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.