Problem/Motivation
ContextualToolbarModelView::render() only updates the first element it was given:
this.$el[0].classList.toggle('hidden', this.isVisible); const button = this.$el[0].querySelector('button');
The view is created with $('.toolbar .toolbar-bar .contextual-toolbar-tab'), which can match more than one element. Gin (with gin_toolbar) renders a second copy of the toolbar on the frontend (the secondary toolbar), so there are two edit toggles. Only the first one is shown; the copy in the secondary toolbar keeps the hidden class and the Edit button is missing there.
Before #3203920 (Backbone to vanilla JS) the Backbone view used jQuery on $el, so every matched element was updated. This is a regression from that change.
Click handlers are bound with this.$el.on(), so they already apply to every element. Only render() is limited to the first one.
The code is in core/modules/toolbar/js/views/ContextualToolbarModelView.js on 11.x (moved by #3618746) and in core/modules/contextual/js/toolbar/contextualToolbarModelView.js on 11.4.x.
Steps to reproduce
- Drupal 11.4, Gin 5 as admin theme, gin_toolbar 3, a frontend theme with contextual links.
- Log in as a user with "access contextual links" and view a frontend page.
- The Edit (pencil) button shows in the first toolbar but not in the Gin secondary toolbar. Its wrapper still has the class
hidden.
Proposed resolution
Update every element in render():
this.$el.each((index, element) => { element.classList.toggle('hidden', this.isVisible); const button = element.querySelector('button'); button.classList.toggle('is-active', !this.isViewing); button.setAttribute('aria-pressed', !this.isViewing); });
Patch attached for 11.x.
Remaining tasks
- Review.
- Convert to a merge request.
Related, possibly a separate issue: updateVisibility() calls this.get('contextualCount'), which no longer exists after the Backbone removal. isVisible therefore stays false and the toggle is shown even on pages without contextual links.
User interface changes
The Edit button appears in every rendered copy of the toolbar again.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|
Issue fork toolbar-3642238
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
sergio morais commentedTested the patch on Drupal 11.4.12 with Gin 5.0.16 and gin_toolbar 3.0.3, frontend theme with contextual links, PHP 8.3.
Before: only the toggle in the first toolbar was shown; the copy in the Gin secondary toolbar kept the
hiddenclass.After: the Edit button shows in the secondary toolbar. Clicking it turns edit mode on (both toggles get
is-activeandaria-pressed="true", all contextual triggers appear) and clicking again turns it off.Comment #3
smustgrave commentedMoving this to the toolbar module since it's been removed from core. Unless it's a critical break we usually don't commit removed module fixes to older versions.
Comment #5
sergio morais commented