0

I'm trying to remove few JS files from one page, which will be called in an Iframe (so no TEMPLATE_js_alter possible because of the if condition).

This code below unset the JS but they are still loaded on the page. It's like I can't save the new list of JS.

Any ideas ?

 function theme_preprocess_page(&$variables) {
        $scripts = drupal_add_js();
        dsm($scripts);

        if(arg(0) == "account" && arg(1) == "reset"){
            foreach ($scripts as $key => $value) {
                $uri = strrpos($key, 'sites/all');
                if($uri !== FALSE){
                    unset($scripts[$key]);
                }
            }
            $variables['scripts'] = $scripts;
        }
    }
4
  • Scripts are output in html.tpl.php, not page.tpl.php so you're just implementing the wrong hook. There's no reason not to use hook_js_alter here though, that would be the best solution
    – Clive
    Jan 26, 2015 at 11:51
  • but it's in a iframe which basically don't send back the arg() of the iframe page but from the parent page, and I couldn't find a way to find my page/form (hook_menu so no nid)
    – GwenM
    Jan 26, 2015 at 12:01
  • 1
    Sorry, no idea what you mean by that. Having been requested via an iframe makes no difference to how the server generates a page. Unless you specifically make it so. The conditional you have in the preprocess hook will work identically in hook_js_alter
    – Clive
    Jan 26, 2015 at 12:19
  • Oh actually the condition on hook_js_alter works fine! but it doesn't display (dsm) the good arg(), but the one of the previous page calling the iframe. I though I wouldn't be able to get the real arg() but in fact it's working despite the wrong display. Do you know why ?
    – GwenM
    Jan 26, 2015 at 13:40

2 Answers 2

1

You can try hook_js_alter and remove it from there:

module_hook_js_alter(&$data) {
  dpm($data);
  unset($data['script']);
  dpm($data);
}
1
  • but it's in a iframe which basically don't send back the arg() of the iframe page but from the parent page, and I couldn't find a way to find my page/form (hook_menu so no nid)
    – GwenM
    Jan 26, 2015 at 12:02
1

Assuming that you really need to use that code in a preprocess function, you should do it in hook_preprocess_html() with code similar to the following one.

function theme_preprocess_html(&$variables) {
  $scripts = drupal_add_js();

  if (arg(0) == "account" && arg(1) == "reset") {
    foreach ($scripts as $key => $value) {
      if (strrpos($key, 'sites/all') !== FALSE) {
        unset($scripts[$key]);
      }
    }

    drupal_static_reset('drupal_add_js');
    drupal_add_js($scripts);
    $variables['scripts'] = drupal_get_js();
  }
}

$variables['scripts'] should contain the value returned from drupal_get_js(). See template_preprocess_html().

  $variables['head'] = drupal_get_html_head();
  $variables['css'] = drupal_add_css();
  $variables['styles'] = drupal_get_css();
  $variables['scripts'] = drupal_get_js();

 

I agree with @CLive about first trying to implement hook_js_alter(), which I expect to work since Drupal doesn't make difference between a page requested with <iframe src="http://example.com/your/drupal/page" /> and pages requested trough a link, or directly from the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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