Jul 1, 2009
Have you ever needed to remove a JS or CSS file in certain conditions such as a custom themed page? In Drupal 6 that has become a whole lot easier. On a recent project there was a need to strip a couple page views down to a extremely basic output that was to be used in an iframe. For various reasons I didn't want any extraneous module included JS or CSS files included and while Drupal is good at only loading what is needed there are a few exceptions. For example one module that is almost always found on a Drupal site is the Administration menu module which makes all administrators lives a little easier. While a boon most of the time in this case I needed to get rid of those helpful little menus as it obstructed the output that would be displayed in that tiny iframe for anyone having permissions to see the Admin Menus. There are a couple of ways to accomplish this but since I also needed to strip some other module added javascript and CSS files out as well I started asking myself if I had seen this done in any other module or not. In this project I had been working with several jQuery related modules and the jQuery Update module instantly came to mind. This module replaces the core jQuery js file with it's own so I started poking around in the module to see how it was being done. jQuery Update removes the core jQuery JS from the $variables['scripts'] array and adds in it's own updated js file. This is a very elegant solution to overriding core or module specific js files if you have a need to. In my case I wanted to simply drop the admin_menu.js file from the $variables['scripts']  array as well as remove the associated CSS files for admin_menu and a couple of other css files plus add in my own to style that stripped down page. To do this I needed to use the new in D6 hook_theme_registry_alter function as well as an accompanying preprocess_page function. Here's my code (wrapped in spots): /** * 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); } }}

Nice article Chris, I've
Aug, 19 2009 - peach - all drupal themes
Nice article Chris, I've used this to prevent theme scripts from loading when they are not needed. p.s. where you write $variables, don't you mean $vars? JR

re: Nice article Chris
Aug, 20 2009 - Chris Hales
@peach when passing the array into a function most people do seem to abbreviate it to $vars but the actual array is named $variables. I try to keep myself in the habit of using the standard but it really doesn't matter.

Similar to my blog post
Jul, 14 2009 - Bevan
I blogged about a similar problem and technique a few weeks ago at http://civicactions.com/blog/2009/jun/11/cant_add_css_js_rss_icon_or_set_title_or_messages_preprocess_page   Cheers!

Adding JS/CSS libraries to d7
Jul, 4 2009 - Aaron Winborn
And <a href="http://drupal.org/node/315100">as of today</a>, this got a whole lot easier in d7...

Also if you embed the view
Jul, 2 2009 - Jay
Also if you embed the view and echo it out it the full page won't load bc drupal stops executing once the page headers have been sent. I do that with xml pages all the time. Not sure if that's the 'drupal' way of doing it, but that's how I've gotten around showing things in the drupal layout.

@Jay, Since you mentioned it
Jul, 8 2009 - Chris Hales
@Jay, Since you mentioned it I've done the same thing. I insert opening html in the Header section of the view and closing tags in the footer then kill the output on a page by using an 'exit' call. Here's a module function example of this:function MYMODULE_render_view($view_name = 'defualt_view'') {  $output = '<div class="killer_output">'."\n";  $output .= views_embed_view($view_name, 'page');  $output .= "</div>\n";  print $output;  exit;}  

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.