【问题标题】:drupal jQuery 1.4 on specific pages特定页面上的 drupal jQuery 1.4
【发布时间】:2011-02-28 10:55:50
【问题描述】:

我正在寻找一种方法来强制 drupal 在特定页面上使用 1.4。

这和这个老问题一样:drupal jQuery 1.4 on specific pages

我想尝试一下我标记为正确的答案。但是因为我是模块开发的新手,所以我无法根据答案弄清楚。

该答案的代码如下所示:

/**
 * Implementation of hook_theme_registry_alter().
 * Based on the jquery_update module.
 *
 * Make this page preprocess function runs *last*,
 * so that a theme can't call drupal_get_js().
 */
function MYMODULE_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['page'])) {
    // See if our preprocess function is loaded, if so remove it.
    if ($key = array_search('MYMODULE_preprocess_page', 
      $theme_registry['page']['preprocess functions'])) {
      unset($theme_registry['page']['preprocess functions'][$key]);
    }
    // Now add it on at the end of the array so that it runs last.
    $theme_registry['page']['preprocess functions'][] = 'MYMODULE_preprocess_page';
  } 
}

/**
 * Implementation of moduleName_preprocess_hook().
 * Based on the jquery_update module functions.  *
 * Strips out JS and CSS for a path.
 */
function MYMODULE_preprocess_page(&$variables, $arg = 'my_page', $delta=0) {

  // I needed a one hit wonder. Can be altered to use function arguments
  // to increase it's flexibility.
  if(arg($delta) == $arg) {
    $scripts = drupal_add_js();
    $css = drupal_add_css();
    // Only do this for pages that have JavaScript on them.
    if (!empty($variables['scripts'])) {
      $path = drupal_get_path('module', 'admin_menu');
      unset($scripts['module'][$path . '/admin_menu.js']);
      $variables['scripts'] = drupal_get_js('header', $scripts);
    }
    // Similar process for CSS but there are 2 Css realted variables.
    //  $variables['css'] and $variables['styles'] are both used.
    if (!empty($variables['css'])) {
      $path = drupal_get_path('module', 'admin_menu');
      unset($css['all']['module'][$path . '/admin_menu.css']);
      unset($css['all']['module'][$path . '/admin_menu.color.css']);
      $variables['styles'] = drupal_get_css($css);
    }
  }
}

我需要在“博客”和“视频”的节点类型上取消设置 jquery_update 1.3.2。有人可以帮帮我吗?

谢谢。

【问题讨论】:

    标签: jquery drupal drupal-modules


    【解决方案1】:

    MYMODULE_preprocess_page 函数中,$scripts 变量包含一个数组,其中包含有关将添加到页面的所有 JavaScript 的信息。

    如果你想从页面中删除一个 JS 文件,你应该从 $scripts 数组中删除它的条目(然后更新 $variables['scripts'])。这就是unset 函数正在做的事情。

    如果您尝试删除的 jQuery 是 Drupal 附带的,您可以使用以下命令删除它:

    unset($scripts['core']['misc/jquery.js']);
    

    否则,您必须print_r $scripts 变量才能找到您需要删除的条目。

    【讨论】:

    • 如何检查内容类型以便知道在哪里取消设置?现在它通过 arg 工作。谢谢。
    • 不确定这是否是正确的方法,但我会通过根据 nid(来自 $_GET['q'])查询 'node' 表中的 'type' 字段来做到这一点.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    相关资源
    最近更新 更多