views:

55

answers:

1

hi,

I've a question about how to customize drupal modules avoiding hacks.

Let's say I've downloaded Lightbox2 module and I want to change the javascript file to display differently my lightbox.

At the moment I'm modifying the Lightbox2 module, so I cannot update it anymore, so I know it is not the best practice.

I was wondering if I can customize the javascript file of this module with a hook, and how.

Thanks

+3  A: 

To alter just the JS, you shouldn't need to hack the module at all. Instead, you can use the theme system to alter what scripts are sent to the page.

In your template.php preprocess page:

 $scripts = drupal_add_js();
 unset($scripts['module']['whatever/the/path/is/lightbox.js']);
 $scripts['module']['new/js/path/lightbox.js'] = array('preprocess' => 1, 'cache' => 1);
 $variables['scripts'] = drupal_get_js('header', $scripts);

I suggest you then copy across the lightbox js to new path and make your alterations there, rather than starting from scratch.

Some modules provide their own hooks and theme functions which you can override directly, so it might be worth poking around a bit in the lightbox module for those too.

cam8001
hi, I'm trying to use your code, and it perfectly replace the content of the array $scripts with the new path. However still the old js file is loaded (and not the new one)... thanks
Patrick
Make sure you are doing it in themename_preprocess_page(), and that you are replacing the $scripts variable that is printed in your page.tpl.php.
cam8001

related questions