0

I could use some assistance in figuring out a way to change script tags for a Drupal 7 site to be "text/plain" instead of "text/javascript" And additional attribute(s). I can tell already that script injection is something that is all over the board when it comes to implementation. Facebook 'pixel' for example is injecting script code outside of the drupal_add_js mechanism. And there are plenty of script tags added in templates (which are simple to change as necessary) but I would like to come up with a solution for the actual Drupal injected scripts, and nothing I’ve tried has worked yet.

Reasoning

This change is required by the service provided via https://eucookie.eu/ for handling GDPR related functionality. I technically only need to change scripts that are adding cookies.

What I've tried without success

Implementing hook_js_alter() in a module

This provides access to some script information, but I don't see a way to change the script type attribute at all. I can however add attributes, just not alter the type as necessary.

Implementing phptemplate_preprocess_html_tag

mytheme_preprocess_html_tag(&$variables) {
  if ($variables['element']['#tag'] == 'script') {
    $variables['element']['#attributes']['type'] = 'text/plain';
    $variables['element']['#attributes']['data-cookie-if'] = 'analytical';
  }
}

The additional attribute of data-cookie-if IS being added as expected. But the type is not being changed, in fact, there is no type at all in the resulting markup.

Additional complications

Script Aggregation if enabled will add another twist, and I'm fairly certain I will need to disable this, but still wanted to mention in case there is a preferred workaround.

2

1 Answer 1

1

I solved, mostly by implementing the theme_process_html_tag method in the template. Note the PROCESS not PREprocess.

In my case, the theme was extending the 'zen' theme, which was actually removing all 'type' attributes from scripts in the header (I don't know exactly why). But I was able to overwrite this behavior in the theme hook mentioned above.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.