Problem
When views are added as blocks the contextual link doesn't work as expected. The block outputs two contextual links with the same ID which causes some issues with the way that Backbone binds and creates the event handlers. Reviewing the event handlers in the inspect you can see that the element has 2 click handlers which triggers openToggle twice (once opening and once closing) which causes the contextual button to never show the menu.
![]()
Steps to reproduce
- Create a view as a block
- Add the block to the page (tested with Panels and with the default Block UI)
- View source for the page and you will see the duplicated contextual block
Proposed resolution
- Move adding the contextual links to DisplayPluginBase
- Remove adding the contextual links to the View element and the view block
- Update contextual.js to filter the list of IDs so that it doesn't try to bind to the same element (ids should be unique on the page)
Remaining tasks
Testing and review the proposed fix.
Debugging notes
While debugging this I found
- views adds contextual links in two places; \Drupal\views\Plugin\Block\ViewBlockBase and \Drupal\views\Element\View
- Disabling the contextual link for the view block removes both instances of the link (on the block and the view)
- Tracking a static for a given contextual ID in the views_add_contextual_links function didn't prevent it from duplicating
- Seems to not be related to theme implementations (tried with Cog, Bootstrap, Bartik and all had the same issue)
- Tested with both Block UI and Panels
- Exposed form blocks still display contextual link after the proposed fix is applied
| Comment | File | Size | Author |
|---|---|---|---|
| #30 | Screenshot from 2025-01-12 15-16-28.png | 56.34 KB | mortona2k |
| #26 | 2884801-nr-bot.txt | 144 bytes | needs-review-queue-bot |
| #18 | 2884801-18.patch | 4.89 KB | ranjith_kumar_k_u |
| #12 | core-views_block_duplicated_contextual_links-2884801-12-d8.5.x.patch | 5.1 KB | psf_ |
| #6 | interdiff-3-5.txt | 1.74 KB | manuel garcia |
Issue fork drupal-2884801
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
steveworley commentedI came across the bug while testing views translations as I wanted to translate some of the static elements (footers). Apologies if there is another issue opened where this would be better to track against. There are a few issues that are discussing contextual links and some issues that people are having with those but this seemed a little different.
Comment #3
steveworley commentedComment #4
lendude@steveworley thanks for the extensive report and patch! Next time, if you upload a patch, please select a 'test with' version and put the issue status on 'needs review' to make sure it is tested by the testbot. Really curious to see what this breaks.
Some initial points:
Unrelated change, lets leave it out, for easier reviewing
Deprecating it is fine, but we can't remove the contents of this method, other modules might rely on it.
This can't default to 'block' in the DisplayPluginBase right? That would mess with other displays.
Also, this needs tests. Not sure if there is any javascript test coverage for contextual filters at the moment (actually pretty sure there isn't), but we need some.
Comment #5
manuel garcia commentedWe now need to "compile" es6 js files like this (inside core/ dir):
yarn run build:js -- --file modules/contextual/js/contextual.es6.jsDoing that in this patch.
#4.1 is automatically done by the compiling action.
Fixing #4.2 as well.
What #4.3 talks about brings up the issue of how views_add_contextual_links() is built... that parameter seems like it is very special, it can be on of three hardcoded values , or:
So this isn't as straight forward as it may seem, so I'm not entirely sure about the proposed solution.
How do we know what to pass to views_add_contextual_links from
DisplayPluginBase::buildRenderable()?Comment #6
manuel garcia commentedAppologies, the interdiff came out wrong. Here is the good one.
Comment #7
manuel garcia commentedComment #9
Anonymous (not verified) commentedWe also discovered the problem of binding in #2891603: Contextual links can't handle multiple occurrences of the same contextual links (again). It is a pity that we did not notice each other at once. As a result, there was a duplication. You have a good description of the binding problem, we have a test and a simpler solution. Let's cooperate! 🤝
Comment #11
psf_ commentedComment #12
psf_ commentedI updated the Manuel Garcia's patch to 8.5.x, but it need tests too.
Comment #18
ranjith_kumar_k_u commentedRe-rolled for 9.2
Comment #19
kapilv commentedComment #20
kapilv commentedComment #24
abarrioApplid patch from #18 on Drupal 9.3 and is working perfect.
Comment #26
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #30
mortona2k commentedI created an issue fork and added the patch.
I am testing by placing the recent content block via block UI.
What I'm seeing is duplicate contextual links for the block and for the view itself.
The only duplicated link is edit view, which is the only one on the view links.
I have any issues with opening the contextual links when the block title is visible.
If the block title is hidden, the view links overlap the block links.
Comment #32
mortona2k commentedThe MR is not working on D11.
Need to remove use of backbone code, removed in 11.2