I have two type of roles 'base' and 'full'. For the base role I would like to alter some menu links to open a modal dialog instead of the original destination url. I would like to replace the link. I know about route subscriber and how to change the route. But in my case I want to change the link (use an ajax enabled link and open a modal dialog instead). Is there any way I can do this?


I think you can use hook_link_alter

function hook_link_alter(&$variables) {
  $url = $variables['url'];

  if ($url->isRouted() && $url->getRouteName() == 'user.login') {
    $variables['options']['attributes']['data-dialog-type'] = 'modal';
    $variables['options']['attributes']['class'] = 'use-ajax';

See more details here https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Menu%21menu.api.php/function/hook_link_alter/8.5.x

|improve this answer|||||
  • thanks, this is what I was searching for. Is there any possibility to attach a library to the link or do I need to use hook_page_attachments to attach it? – Matthias Apr 17 '18 at 20:53
  • @Matthias np, yes you have to use hook_page_attachments or extend global library in your theme (it depends on accessibility of your assets: js/css). – Alex Kuzava Apr 18 '18 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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