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

  1. Drupal 11.4, Gin 5 as admin theme, gin_toolbar 3, a frontend theme with contextual links.
  2. Log in as a user with "access contextual links" and view a frontend page.
  3. 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.

Issue fork toolbar-3642238

Command icon 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

sergio morais created an issue. See original summary.

sergio morais’s picture

Tested 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 hidden class.

After: the Edit button shows in the secondary toolbar. Clicking it turns edit mode on (both toggles get is-active and aria-pressed="true", all contextual triggers appear) and clicking again turns it off.

smustgrave’s picture

Project: Drupal core » Toolbar
Version: 11.x-dev » 2.0.0
Component: toolbar.module » Code

Moving 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.

sergio morais’s picture