Problem/Motivation

Attached custom javascript can end up being attached multiple times leading to duplication when its rendering the page

Steps to reproduce

Here is the sample code that may be in a custom module

function attachinline_test_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $form['#attached']['js'][] = [
    'type' => 'inline',
    'data' => _attachinline_test_get_inline_js(),
    'scope' => 'footer',
    'weight' => 5,
  ];
}

function _attachinline_test_get_inline_js() {
  $inline_js = <<<JS
  (function (Drupal) {
    Drupal.behaviors.attachInlineTest = {
      attach: function (context, settings) {
        console.log('hello from custom js!');
      }
    };
  })(Drupal);
JS;

  return $inline_js;
}

Place this on a drupal site that has multiple forms
Load up a page with those forms
View the page source
You will see in source multiple

tags with the same code The code above is an example to demo but I have seen this on more targetted forms with the same behaviours

Proposed resolution

Filter out duplicate javascript

Remaining tasks

Write a patch

User interface changes

N/A

API changes

N/A

Data model changes

N/A
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

hannahdigidev created an issue. See original summary.

hannahdigidev’s picture

Assigned: Unassigned » hannahdigidev
Status: Active » Needs review

Opened merge request with patch

andybroomfield made their first commit to this issue’s fork.