Add the ability to specify JavaScript settings that should only be added once per page

Created on 29 October 2023, 10 months ago

Problem/Motivation

Adding multiple copies of the same JavaScript settings to a page will result in them being added multiple times (once for each time that they are added). This can cause a variety of issues such as, in the case of adding multiple WYSIWYG editors to one page which all use the same settings, broken toolbar buttons.

There is currently no way to add JavaScript settings to a page in Drupal 7 and guarantee that they are idempotent. This was made possible in Drupal 8+ with Adding JS settings is now idempotent (JS settings merging behavior modified) .

Workaround, such as using a static variable and tracking if the settings have been added before, all have a variety of issues.

Backdrop addressed the issue by added the ability to specify a key when adding JavaScript settings if you want to ensure that a group of settings is only added to a page once.

This blocks the release of the Editor module.

Steps to reproduce

  1. Use drupal_add_js() or #attached to pass WYSIWYG editor JavaScript settings to a filter format.
  2. Add multiple fields to an entity which uses that text format.
  3. Attempt to edit the entity which will result in the editor settings being added multiple times, once for each field that uses the filter format.

You can recreate this with the Editor module by adding a text editor to a text format and then displaying multiple fields with that format on one page. The text editor's toolbar buttons will appear multiple times; once for each of the fields on the page with the format.

Proposed resolution

Add the ability to specify a key when adding JavaScript settings which ensures that a group of settings is only added to a page once.

Feature request
Status

Needs review

Version

7.0 ⚰️

Component
Javascript 

Last updated 1 day ago

Created by

🇨🇦Canada Devin Carlson Sudbury, ON

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024