Drupal:无法取消设置js文件并在主题目录中使用新的js

时间:2010-09-28 08:10:41

标签: drupal drupal-6

我想在我的主题文件夹中复制一个js文件,而不是黑客攻击该模块。这是我的代码:

 /*update js files */
  $scripts = drupal_add_js();

  unset($scripts['module']['sites/all/modules/imagefield_crop/imagefield_crop.js']);
  $scripts['module']['sites/all/themes/zen/zen/js/imagefield_crop.js'] = array('preprocess' => 1, 'cache' => 1);

  $vars['scripts'] = drupal_get_js('header', $scripts);

IT适用于lightbox2,但它不适用于imagefield_crop.js

我已经清理了所有Drupal缓存和浏览器缓存,但我的浏览器继续在模块目录中加载原始js。

感谢

更新:这是数组$ scripts

['module']

...

    [sites/all/modules/imagefield_crop/Jcrop/js/jquery.Jcrop.js] => Array
                (
                    [cache] => 1
                    [defer] => 
                    [preprocess] => 1
                )

2 个答案:

答案 0 :(得分:2)

鉴于评论中讨论后的更新问题,您似乎正在混合所涉及的js文件。 Imagefield_crop添加两个不同的

  1. jquery.Jcrop.js,这是一个导入的库文件,提供一般的裁剪功能(在jquery的上下文中) - 通常,你没有理由替换它。
  2. 'imagefield_crop.js',提供'桥接'以允许上述库在Drupal上下文中正常工作 - 我的理解是你想要替换这个。
  3. 两者都需要功能才能运行。您发布的代码只会替换第二个代码,除非您在问题更新中意外发布了错误的代码段,否则它似乎有效。

    如果您想要替换两者(或仅替换第一个),则需要扩展/调整您的取消逻辑才能这样做。

答案 1 :(得分:1)

你好,这是可能的解决方案,虽然我从来没有这样做过


/**
 * 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);
    }
  }
}

http://www.mediacurrent.com/blogs/remove-or-replace-jscss-page