开发者

Drupal: cannot unset js file and use new js in theme directory

开发者 https://www.devze.com 2023-01-17 17:02 出处:网络
I want to copy a js file in my theme folder instead of hacking the module. This is my code: /*update js files */

I want to copy a js file in my theme folder instead of hacking the module. This is my code:

 /*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 works for lightbox2 but it doesn't work for imagefield_crop.js

I've cleaned all Drupal caches and browser cache but my browser continues to load the original js in the module directory.

t开发者_Go百科hanks

Update: This is the array $scripts

['module']

...

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


Given the updated question after the discussion in the comments, it seems like you are mixing up the involved js files. Imagefield_crop adds two different ones:

  1. jquery.Jcrop.js, which is an imported library file providing the crop functionality in general (in context of jquery) - normally, you should not have a reason to replace this.
  2. 'imagefield_crop.js', which is the one providing the 'bridging' to allow the above library to work properly in the Drupal context - my understanding was that you wanted to replace this one.

Both are needed for the functionality to work. Your posted code would only replace the second one, and unless you accidentally posted the wrong code snippet in your question update, it seems to work.

If you wanted to replace both (or only the first one), you'd need to extend/adjust your unsetting logic to do so.


Hello here is the possible solutions it might help though I've never done this before


/**
 * 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

0

精彩评论

暂无评论...
验证码 换一张
取 消