Drupal 6 & 7 unset Javascript from header

本小妞迷上赌 提交于 2019-12-22 18:10:06

问题


EDIT: My question applies to Drupal 6 & 7, though my code example is Drupal 6. People have provided answers are useful for both versions of Drupal.

I'm currently working in Drupal creating a mobile theme for a Drupal 6 website and trying to remove all unnecessary core and module JavaScript and css through the preprocess_page function in my template.php file. The css files are successfully removed, but I can't seem to get the JavaScript to be removed. Here's what I've got. In this example, everything is successfully removed except for the the ajax scripts.

Any idea what I'm doing wrong?

<?php
function mytheme_preprocess_page(&$vars) {
    //////// remove unneccesary drupal head files for mobile version
    // CSS
    $css = drupal_add_css();
       // core
    unset($css['all']['module']['modules/user/user.css']);
    unset($css['all']['module']['modules/node/node.css']);
    unset($css['all']['module']['modules/system/defaults.css']);
    unset($css['all']['module']['modules/system/system.css']);
    unset($css['all']['module']['modules/system/system-menus.css']);
       // contributed -- automatically generate the path—— just easier this way
    $rm[] = drupal_get_path('module','filefield').'/filefield.css';
    $rm[] = drupal_get_path('module','flickr').'/flickr.css';
    $rm[] = drupal_get_path('module','logintoboggan').'/logintoboggan.css';
    $rm[] = drupal_get_path('module','logintoboggan').'/logintoboggan.css';
    $rm[] = drupal_get_path('module','fieldgroup').'/fieldgroup.css';
    $rm[] = drupal_get_path('module','views').'/css/views.css';
    $rm[] = drupal_get_path('module','content').'/theme/content-module.css';
       // remove the contribs from the array
    foreach ($rm as $key => $value) {
      unset($css['all']['module'][$value]);
    }
    // JAVASCRIPT
    $scripts = drupal_add_js();

    unset($scripts['module']['sites/all/modules/ajax/ajax.js']);
    unset($scripts['module']['sites/all/modules/ajax/jquery/jquery.a_form.packed.js']);

    // recreate the tempalate variables
    $vars['styles'] = drupal_get_css($css);
    $vars['scripts'] = drupal_get_js('header', $scripts);
}
?>

ETA: Here is the way the scripts print out in the header:

<script type="text/javascript" src="/sites/all/modules/ajax/jquery/jquery.a_form.packed.js?P"></script>
<script type="text/javascript" src="/sites/all/modules/ajax/ajax.js?P"></script>

回答1:


I found a potentially better solution for this sort of problem. I tried Kerri's approach since Matt V says it worked for him, but I really didn't want to edit template.php if I could avoid it and regardless it wasn't working in my environment. My problem was that a couple JS files throw errors on one particular page. All I did was write a very simple new module that only runs on the desired page. So- I created a basic .info file and below is the .module code. Worked like a charm for me. Just replace "mysite_mymodule" with whatever you name your module and replace "my-page-alias" with whatever page alias you're trying to effect.

<?php
/**
* Implements hook_js_alter().
*/

function mysite_mymodule_js_alter(&$javascript) {
  $alias = drupal_get_path_alias($_GET['q']);
  if ($alias == "my-page-alias") {
    /**
     * documentation about what I did and why I did it...
     */
    unset($javascript['sites/all/modules/dhtml_menu/dhtml_menu.js']);
    unset($javascript['sites/all/modules/apachesolr_autocomplete/apachesolr_autocomplete.js']);
  }
}



回答2:


You can directly use the drupal header in drupal 7. you can clean the other module javascript you don't need

function module_preprocess_page(&$vars) {
  // clean unnecesary script from the page
  if (arg(0) != 'admin' || !(arg(1) == 'add' && arg(2) == 'edit') || arg(0) != 'panels' || arg(0) != 'ctools') {
    $javascript = &drupal_static('drupal_add_js', array());
    unset($javascript['misc/jquery.js']);
    unset($javascript['misc/jquery.once.js']);
    drupal_static('drupal_add_js', $javascript);
  }
}



回答3:


The Ajax module uses hook_preprocess_page to add its scripts. The Ajax module may be executing its hook implementation after your module. If that's the case, you could adjust the weight of your module, to fire after Ajax. You could check first to make sure, by outputting the contents of $scripts (using something like the Devel module's dpm() function), just before you do the unsetting.

UPDATE: Since you're making the changes in a template preprocess function, my initial hypothesis about it being an issue of module weights wouldn't apply. I added your template function on a test site and it removed those two scripts, for me. I had to adjust the path because my ajax module was under /sites/all/modules/contrib. That's just where I happen to keep my contrib modules, but you might double check that you're using the right path.

Another thing that threw me off for a few minutes was that I accidentally added the code to the wrong theme; so, that's another thing to check. Also, check that Javascript caching is disabled on the Performance page.




回答4:


You have to do it the other way around. Unsetting files of other contrib modules explicitly couples your module to every other module that outputs js to the header. You won't be able to maintain that for too long before it breaks. What happens when more js files get added by contrib modules?

You're best to create a mask of paths you want to output and unset everything except those.

Drupal 7 has some hooks that will let you do that but I can't remember them. If you're on D6 you're kinda stuck.

I did write a dirty workaround for Drupal 6 for this same problem with css files. Here is the whole module (it was only 3 functions). I'm sure you can adapt it to js files too.

Create your mask (expose it to the variables table)

function destroy_mask_styles() {
  return variable_get('destroy_mask_styles', array('all/modules/mymodule', 'themes/mytheme'));
}

Then here comes the dodgey part. This function looks for the paths of the css files in the header and unsets them if they don't match.

function destroy_styles($var_styles) {
  //this is a bit dodgey. But it's probably the easiest way to implement 
  $masks = destroy_mask_styles();
  $styles = array();
  foreach(explode("\n", $var_styles) as $style) {
    foreach($masks as $mask) {
      if(strpos($style, $mask) > 0) {
        $styles[] = $style;
      }
    }
  }
  return implode("\n", $styles);
}

Then in preprocess_page, (this might be considered where the dodgyness happens)

function destroy_preprocess_page(&$variables) {
  //destroy some css and scripts we don't want
  $styles = destroy_styles($variables['styles']);
  $variables['styles'] = empty($styles) ? $variables['styles'] : $styles;
}

If you want to include everything from one module (for example), just alter the paths that get matched to include the path to the module's root folder. Don't worry about file names.

Remember that this is not the ideal way to do this. There might be a better way in D6 but there are no hooks here so probably not.

Good luck!



来源:https://stackoverflow.com/questions/4902456/drupal-6-7-unset-javascript-from-header

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!