Sorry about that rather specific title: hopefully my question will clarify itself with the following:
What data is on the table to work with
I have a View (page display) that lists nodes based on the main categories "Fruit" and "Veggies"
So I have two pages: 1 at www.mysite.com/fruit and the other www.mysite.com/veggies
- arg(0) is being used as a contextual filter to get the right nodes on both those pages.
- I'm grouping my nodes (grouping field dropdown in Style options) based on the taxonomy term they belong to (eg for "Fruit" > sweet and sour) to reflect their hierarchical place in the taxonomy structure (see below)
fruit
-- sweet(taxonomy term)
-- -- banana node
-- -- melon node
-- sour(taxonomy term)
-- -- lemon node
veggies
-- red (taxonomy term)
-- -- paprika node
-- -- onion node
What I would like achieve
I would like to be able to add different styling to a page that has only 1 main category (like the veggies - categorypage -> we only have the "red" subcateg there) as opposed to a page that has 2 or more main categories (like the fruit - categorypage -> we have 2 subcategs there: "sweet" and "sour")
What I have uptil now (it's working but I need to clean up my act/code)
As stated ... this already functions , but I would love to know:
- if I followed Drupal protocols.
- if there are better ways (more performant, cleaner code, easier)
I've got this in my template.php
/* Helper function so I can reach my specific display in the preprocess hook below*/
function <themename>_preprocess_views_view(&$vars) {
if (isset($vars['view']->name)) {
$function = __FUNCTION__ . '__' . $vars['view']->name . '__' . $vars['view']->current_display;
if (function_exists($function)) {
$function($vars);
}
}
// This is where I inject a custom CSS file in the case where I've got only 1 Main category (as explained above)
function <themename>_preprocess_views_view__need__page(&$vars) {
$tidArr = $vars['view']->field['term_node_tid']->view->field['term_node_tid']->items;
$terms = array();
foreach ($tidArr as $key=>$tids) {
$terms[] = current(reset($tids));
}
$uniqueTids = count(array_unique($terms));
if($uniqueTids < 2) {
drupal_add_css(path_to_theme() .'/css/my_custom_css.css');
}
}
Disclaimer: Heavy question , I know ... but hopefully clear enough so you can all understand what is going on , and what I'm trying to achieve.
Understand that I'm happy I got it working , but I would be even more happy if I could do it the Drupal "way" and have my code as lean and mean as possible.
thanx for taking the time.
}