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
Proposed resolution
Filter out duplicate javascriptRemaining tasks
Write a patchUser interface changes
N/AAPI changes
N/AData model changes
N/AIssue fork attachinline-3543177
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 #3
hannahdigidev commentedOpened merge request with patch