views:

40

answers:

2

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.

thanks

Update: This is the array $scripts

['module']

...

    [sites/all/modules/imagefield_crop/Jcrop/js/jquery.Jcrop.js] => Array
                (
                    [cache] => 1
                    [defer] => 
                    [preprocess] => 1
                )
+1  A: 
sultan
+1 - this is basically what the OP tries to do from within his themes template.php, but with the additional step of ensuring that the preprocess_page callback used for the manipulation is definitely the last to be called. If the problems with his approach turn out to be related to the execution order, this might be the proper solution.
Henrik Opel
However, I doubt that the execution order is the culprit here - if it where, his own additions to the $scripts variable wouldn't make it to the page.tpl.php, as they would be overwritten by the changes from the later preprocess function.
Henrik Opel
By the way I don't see the reasons to override css/js includes provided by some modules or theme specific files, otherwise it seems to me it is better to provide your own custom extension.
sultan
Also, is it possible to make up with CCK?
sultan
@sultan: I agree that in general it is be better to extend instead of overriding existing js/css files, but depending on the nature and scope of the changes needed, sometimes it is a lot easier to simply replace what is there (if the 'extension' gets to convoluted to be easy to understand, simply replacing one file with another can be easier to maintain)
Henrik Opel
@sultan: I do not understand what you mean by 'make up with CCK' - could you rephrase that?
Henrik Opel
@Henrik Opel I meant by 'make up with CCK' any way to achieve @Patrick's goal using CKK
sultan
+2  A: 

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.

Henrik Opel
@Henrik Opel : indeed my mistake again. It works
Patrick

related questions