AFAICT, there's no way for a menu link plugin to cause the menu link not to appear.
There is MenuLinkInterface::isEnabled(), but returning FALSE from that causes the menu link itself to become disabled.
I've also tried returning either '' or NULL from getRouteName(), but that causes a routing crash. There's route '<none>' but that creates a link to the current page.
At this point the only thing I can see to do is to create a custom route which always denies access, and return that route name.
Use case is that I am writing a custom menu link plugin to show a user's group. If the user has no group memberships, then the link should not show.
Comment | File | Size | Author |
---|---|---|---|
#15 | 2719609-15.interdiff.txt | 3.33 KB | claudiu.cristea |
#15 | 2719609-15-test-only.patch | 3.38 KB | claudiu.cristea |
#2 | 2719609-2.patch | 1.71 KB | dawehner |
Issue fork drupal-2719609
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dawehnerBeside your proposed solution, to put that access logic into the route, IMHO isEnabled should itself totally work, but we have simply a bug.
This tree cacheability information should be gathered first, before we check whether the link is enabled or not.
Comment #3
joachim CreditAttribution: joachim as a volunteer commentedFixed the summary -- my mention of route '<none>' wasn't showing up.
Comment #5
dawehner@joachim
Did you ever managed to try out the patch? IMHO the patch is all we need, beside some additional test coverage ...
Comment #10
claudiu.cristeaThis test proved that using
MenuLinkInterface::isEnabled()
just works, w/o the patch from #2. Or do we need an edge case in test?Comment #11
claudiu.cristeaThe test, sorry.
Comment #12
dawehnerI'm wondering whether anything else adds the per route cache context automatically.
Comment #13
claudiu.cristea@dawehner, actually I can reproduce the bug in one of my projects but I cannot make this test fail to prove it. Probably it receives the route context from somewhere. Using the patch from #2, in my project is solving the issue. I have no time to investigate why the test passes (when it should not).
Comment #14
claudiu.cristeaIn fact,
\Drupal\system\Plugin\Block\SystemMenuBlock::getCacheContexts()
is adding theroute.menu_active_trails:{menu_name}
context. That makes the test pass.Comment #15
claudiu.cristeaImproved the test but it's still passing w/o #2 :(
Comment #16
idimopoulos CreditAttribution: idimopoulos at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedThis patch does work because we are testing it in our project. However, hidden links are also hidden from the menu edit form where the links are listed if the condition somehow excludes this form. I don't know if this is intended behaviour (e.g. hide the link from users that should not have access to it) but the table row is shown, without a link label next to the check box.
So, we should either not show the entire row (if the intended behaviour is to hide the menu item) or show the item in certain cases (like admin path, or administer permissions? not sure).
Comment #17
idimopoulos CreditAttribution: idimopoulos at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedComment #18
claudiu.cristea@idimopoulos, there's no correlation between a disabled menu link and its clickable label shown in the administrative interface. Probably, you hit a JS/CSS issue that makes the link to be hidden.
Comment #19
idimopoulos CreditAttribution: idimopoulos at Randstad Digital for European Commission and European Union Institutions, Agencies and Bodies commentedActually, yeah, probably. because the patch indeed does not justify such a behaviour.