编辑:我的问题适用于Drupal 6& 7,虽然我的代码示例是Drupal 6.人们提供的答案对两个版本的Drupal都很有用。
我目前正在Drupal为Drupal 6网站创建移动主题,并尝试通过template.php文件中的preprocess_page
函数删除所有不必要的核心和模块JavaScript和css。 css文件已成功删除,但我似乎无法删除JavaScript。这就是我所拥有的。在此示例中,除了ajax脚本之外,所有内容都已成功删除。
知道我做错了吗?
<?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:以下是脚本在标题中打印的方式:
<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>
答案 0 :(得分:2)
我找到了一个潜在更好的解决方案来解决这类问题。我试过Kerri的方法,因为Matt V说它适合他,但我真的不想编辑template.php,如果我可以避免它,不管它在我的环境中不起作用。我的问题是一些JS文件在一个特定页面上抛出错误。我所做的只是编写一个非常简单的新模块,只在所需的页面上运行。所以 - 我创建了一个基本的.info文件,下面是.module代码。对我来说就像一个魅力。只需将“mysite_mymodule”替换为您为模块命名的内容,并将“my-page-alias”替换为您尝试生效的任何页面别名。
<?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']);
}
}
答案 1 :(得分:2)
您可以直接在drupal 7中使用drupal标头。 你可以清理你不需要的其他模块javascript
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);
}
}
答案 2 :(得分:1)
用于添加脚本的Ajax模块uses hook_preprocess_page。 Ajax模块可能在您的模块之后执行其钩子实现。如果是这种情况,你可以在Ajax之后开火adjust the weight of your module。您可以先检查,然后在取消设置之前输出$ scripts的内容(使用类似Devel module的dpm()功能)。
UPDATE :由于您在模板预处理功能中进行了更改,因此我最初关于它是模块权重问题的假设将不适用。我在测试网站上添加了你的模板功能,并为我删除了这两个脚本。我不得不调整路径,因为我的ajax模块位于/ sites / all / modules / contrib下。这就是我碰巧保留我的contrib模块的地方,但你可能会仔细检查你是否正在使用正确的路径。
让我离开几分钟的另一件事是我不小心将代码添加到了错误的主题中;所以,这是另一回事。另外,请检查“性能”页面上是否禁用了Javascript缓存。
答案 3 :(得分:0)
你必须以相反的方式做到这一点。取消设置其他contrib模块的文件会明确地将模块耦合到将js输出到标头的每个其他模块。在打破之前,你将无法长时间保持这种状态。当contrib模块添加更多js文件时会发生什么?
您最好创建一个想要输出的路径掩码,并取消设置除此之外的所有内容。
Drupal 7有一些钩子可以让你这样做,但我记不住了。如果你在D6上,你就会被困住。
我为 Drupal 6 编写了一个脏解决方法,解决了与css文件相同的问题。这是整个模块(它只有3个功能)。我相信你也可以将它改编为js文件。
创建掩码(将其公开给变量表)
function destroy_mask_styles() {
return variable_get('destroy_mask_styles', array('all/modules/mymodule', 'themes/mytheme'));
}
然后这里有一些躲闪的部分。此函数在标题中查找css文件的路径,如果它们不匹配则取消设置。
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);
}
然后在preprocess_page中,(这可能会被视为躲闪发生的地方)
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;
}
如果要包含一个模块中的所有内容(例如),只需更改匹配的路径即可包含模块根文件夹的路径。不要担心文件名。
请记住,这不是理想的做法。在D6中可能有更好的方法,但这里没有钩子,所以可能没有。
祝你好运!