8

I want to alter the menu link in Drupal 8. In Drupal 7 we can achieve this using hook_translated_menu_link_alter

function module_translated_menu_link_alter(&$item, $map) {
  global $user;
  if (!empty($item['access'])) {
    // Administrator will access all menu's.
    if (strpos(current_path(), 'admin/structure/menu/manage/' . $item['menu_name']) === 0 && user_access('administer menu')) {
      return;
    }
    // If userid is equal to menu user id.
    if ($user->uid == 1) {
      $access = 'UNBLOCK';
      break;
    }
   // Block menu.
    else {
      $access = 'BLOCK';
    }
    // Access FALSE if Menu not for USER.
    if ($access == 'BLOCK') {
      $item['access'] = FALSE;
    }
  }
}

In D7 i am hiding/disabling menu based on some conditions like uid.

Similarly I want to alter the menu link in Drupal 8. How can I do it. Thank You.

  • On this page if you scroll to end before comments, it has stated hook_translated_menu_link_alter() has been replaced by hook_menu_link_load(), I do not know whether it is right or wrong, I just found this. Might be you can make out. If, let me also know. – CodeNext Aug 12 '16 at 22:05
-1

From drupal.org on /node/2122241:

Altering menu links and adding menu links dynamically

Statically defined menu items may be altered using hook_menu_links_discovered_alter() (but note there is no hook_menu_link_discovered(), the menu links are built from the .yml files as explained above). This hook may also be used to add new dynamic menu items.

More often, you may want to use a deriver class to add new menu link plugins. For example Views provided menu links are added in views.links.menu.yml, which specifies the deriver class \Drupal\views\Plugin\Derivative\ViewsMenuLink.

Finally, you can use the plugin manager directly to add, update, and remove definitions.

You could probably use hook_menu_links_discovered_alter for your scenario.


Edit: You want to edit the permission to access that link, so it automatically does not show up in the menu.

Read on drupal.org: /node/2311427

Drupal 8

In Drupal 8, you can support dynamic permissions by referencing a function that will dynamically define those permissions. This callback defines the permissions for core's filter module.

// in filter.permissions.yml


permission_callbacks:
  - Drupal\filter\FilterPermissions::permissions

// in FilterPermissions.php

class FilterPermissions {
  public function permissions() {
    $permissions = [];
    // Generate permissions for each text format. Warn the administrator that any
    // of them are potentially unsafe.
    /** @var \Drupal\filter\FilterFormatInterface[] $formats */
    $formats = $this->entityManager->getStorage('filter_format')->loadByProperties(['status' => TRUE]);
    uasort($formats, 'Drupal\Core\Config\Entity\ConfigEntityBase::sort');
    foreach ($formats as $format) {
      if ($permission = $format->getPermissionName()) {
        $permissions[$permission] = [
          'title' => $this->t('Use the @label text format', ['@url' => $format->url(), '@label' => $format->label()]),
          'description' => String::placeholder($this->t('Warning: This permission may have security implications depending on how the text format is configured.')),
        ];
      }
    }
    return $permissions;
  }
}

You should also read this article https://www.lullabot.com/articles/what-happened-to-hook_menu-in-drupal-8 (scroll down to "Access Control" and "Hook_Menu_Alter").

I hope this helps.

  • 1
    Tried this hook_menu_links_discovered_alter, but this is not useful to hide menu. Kindly check that this hook is only useful for The link definitions to be altered. – Ajay Reddy Aug 8 '16 at 11:04
  • Referring to above article, I tried that but that will give only access denied, it wont hide menu. – Ajay Reddy Aug 8 '16 at 12:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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