0

I work on a D8 (8.6) I want to hide some item on my menu depending on route param onto this link.

When I try to access the route I get a 403 error and works as expected but the the link inside the menu is still displayed.

I create a custom Accessinterface which return an AccessResult::allowed() or denied() depending on my rules.

Is there a core solution to prevent the link to be displayed on the main menu or I have to handle it in JS ?

My CustomAccessCheck :

class CustomAccessCheck implements AccessInterface
{

  public function access(AccountInterface $account)
  {
    $isAuthenticated = $account->isAuthenticated();

    try {
      $currentGroup = TestEntity::getInstance()->currentGroup;
    } catch (InvalidPluginDefinitionException $e) {
      return AccessResult::forbidden();
    }
    if ( $isAuthenticated &&
      ( !empty($currentGroup["id"]) && isset($currentGroup["IsActive"]) && $currentGroup["IsActive"])
    ) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }
}

I configure services and routing.yml in order to run this AccessChecker. The custom menu contain some menu items which target the route configured.

My problem is even if the user don't have access to the route the menu item is displayed.

Thanks for help Nico

2
  • Welcome to Drupal Answers! What have you tried so far? Can you share any code snippet? Please edit your question for clarification.
    – leymannx
    Jan 17, 2019 at 13:40
  • 1
    The core solution is to add the access check to the route requirements. Can you provide more details about the route?
    – 4uk4
    Jan 17, 2019 at 13:40

1 Answer 1

0

I found by myself my mistake.

The menu items are always shown for super user.

The menu items are hidden/shown correctly for others users.

2
  • 1
    User 1 is not beholden to any permissions.
    – Kevin
    Jan 18, 2019 at 9:47
  • Yes i made a rookie mistane
    – Nico
    Jan 19, 2019 at 11:39

Your Answer

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

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