CSS Injector Rule

While developing a Drupal site, I was looking for an easy way to change the theme banner image per section/topic. This would need to be done programatically based on Book ID and by Category terms. The CSS Injector module seemed like a good option as it allows you to set rules (like Blocks) for when the CSS is used. I was surprised that it is not more widely used.

 

Set CSS Style by Book Parent ID

By examining the book-navigation.tpl.php template file, I found how to get the parent book id, which we can check to see if the current page is a node child of that book.

The first step was to re-organise the book section into multiple books in order to be able to use the parent Book ID, being a new site, it was not a problem. After installing the Drupal CSS Injector module (link below), I created a new rule to replace the inner_banner image in the first book.

1
2
3
#inner_banner {
background: url("/sites/all/themes/mytheme/images/bg_innerTools.jpg") no-repeat;
}
#inner_banner {
background: url("/sites/all/themes/mytheme/images/bg_innerTools.jpg") no-repeat;
}

Then to add the CSS to specific pages to change the header banner depending on the book parent, we select to add if PHP returns a non-zero value, using the code:

1
2
3
4
5
6
7
$current_bid = 0;
 if ($node = menu_get_object()) {
 $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
 }
if ($current_bid == 14 ){
return TRUE;
}
$current_bid = 0;
 if ($node = menu_get_object()) {
 $current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
 }
if ($current_bid == 14 ){
return TRUE;
}

If there was a Rule duplication/clone button for the module, it would be perfect, as I will need to create more rules that are nearly identical, but copy and paste will certainly do.

 

Leah Davies
Join me

Leah Davies

An American living in England, Leah has extensive experience in website development, content management, community building, SEO and internet marketing since 1997.
Leah Davies
Join me

Latest posts by Leah Davies (see all)

Drupal CSS Styling Per Book
Tagged on:     
Loading Facebook Comments ...
Loading Disqus Comments ...

Leave a Reply

Your email address will not be published. Required fields are marked *