21st September
2008
Recently I found myself wanting to add and remove stylesheets via template.php
I thought there must be a way to do it via the _phptemplate_variables function.
Sure enough, there is:
PHP:
-
/**
-
* Override or insert PHPTemplate variables into the templates.
-
*/
-
-
function _phptemplate_variables($hook, $vars) {
-
-
if($hook == 'page'){
-
-
/* add css
-
* this is where css layout make themselves useful
-
* basically we get the current css array, add to it, and set that to var $styles
-
*/
-
$new_css = drupal_add_css(); //get current css
-
//full page layout for node add edit and admin pages
-
if(arg(0) == 'admin' || arg(1) == 'add' || arg(1) == 'edit' || arg(2) == 'edit'){
-
// this is how you add a stylesheet
-
$new_css['all']['theme']['sites/all/themes/peoples_times/stylesheets/1c-b.css'] = TRUE;
-
$vars['hide_sidebars'] = TRUE;
-
}else{
-
//default 2 column width
-
$new_css['all']['theme']['sites/all/themes/peoples_times/stylesheets/2c-r.css'] = TRUE;
-
}
-
// rebuild CSS and assign it to styles var
-
$vars['styles'] = drupal_get_css($new_css);
-
}
-
}
The example above assigns a stylesheet based on url path.
If it's a node add or edit page or admin page, it uses a 1 column layout stylesheet.
It also sets a 'hide sidebars' variable which is available in page.tpl.php
This is a blog to record tidbits of information. It documents thoughts and ideas that may be useful for other people, like yourself.