Pre-note: usually my questions aren't as vague as this one. This time though I am pretty lost in the lack of documentation, so I think I don't even completely understand the context of what I'm asking.

I am looking into code that wasn't written by me and it builds a menu using a Deriver (Drupal\Component\Plugin\Derivative\DeriverBase). It's method getDerivativeDefinitions creates a list of links. The actual menu is then loaded in the page using a block.

Now the menu that's being build is a menu of static links. Some of the links require that the user either has certain permissions, or has certain account checkboxes ticket.

Currently these checks are being performed inside the getDerivativeDefinitions method, but that doesn't seem to work because the method is called once during a cache rebuild and then no more. Adding user as a cache context doesn't seem to change anything.

Example code:

  public function getDerivativeDefinitions($base_plugin_definition)
    $links = [];
    if (!$this->account->isAnonymous() && !$this->account->get('field_something')->isEmpty()) {
      $links['Some link'] = 'https://some-url';
    if (!$this->account->isAnonymous() && $this->account->hasRole('some_role')) {
      $links['Something'] = 'https://some-link';

    $menu = [];
    foreach ($links as $key => $link) {
      $menu['app_links' . $key] = [
          'title'     => $key,
          'menu_name' => 'menu-name',
          'url'       => $link,
          'options'   => ['attributes' => ['target' => '_blank']],
        ] + $base_plugin_definition;

    return $menu;

I would expect the actual link definition to have some kind of access-callback key or something like that, but I can't find that either. Does anyone know how to fix this?

  • 1
    There are two issues: (i) Derivatives are not dynamic, they are for deriving plugins from static configuration. (ii) Access is usually controlled by the target route, not by the menu item itself. See both answers here drupal.stackexchange.com/questions/273062/…
    – 4uk4
    Aug 30, 2020 at 15:45
  • Thank you very much for this very constructive answer. That'll help a lot.
    – vrijdenker
    Aug 30, 2020 at 20:55
  • Are you interested in posting the final solution as answer?
    – 4uk4
    Aug 31, 2020 at 9:58
  • Yes, you can do that and I'll mark it as the answer. In the end I indeed had to ditch the deriver.
    – vrijdenker
    Oct 6, 2020 at 5:52


Browse other questions tagged or ask your own question.