I'm having an issue with adding a data attribute to my preprocessed / aggregated javascript within my theme.

When I have preprocessing disabled, I am able to add a data attribute to individual files within the .libraries.yml file as suggested within the Drupal docs. {attributes: {data-example-attribute: true}}

However once I enable ['js']['preprocess'] = true; to the settings.php file instead of my theme JS being aggregated into a single file they do not get aggregated at all.

Is there a hook that I'm missing which I could use to just prepend a data attribute onto the preprocessed JS file.

Using Drupal 9

  • What should happen to the attributes on a script tag that aggregates multiple files if two libraries specify different values for the same data- attribute?
    – cilefen
    Oct 24, 2022 at 15:42
  • @cilefen Thanks for your reply. In this instance all the files within the library require the same data attribute.
    – Ian
    Oct 24, 2022 at 16:37
  • This issue seems like it is related, although if I am reading it right it could cause issues with data- attributes if merged, because it does not account for attribute values: drupal.org/project/drupal/issues/1587536
    – cilefen
    Oct 24, 2022 at 17:26


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.