Change layout of a view 'page display' based on number of grouping field occurences 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 &lt;themename&gt;_preprocess_views_view(&amp;$vars) {
  if (isset($vars['view']-&gt;name)) {
    $function = __FUNCTION__ . '__' . $vars['view']-&gt;name . '__' . $vars['view']-&gt;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 &lt;themename&gt;_preprocess_views_view__need__page(&amp;$vars) {
  $tidArr = $vars['view']-&gt;field['term_node_tid']-&gt;view-&gt;field['term_node_tid']-&gt;items;
  $terms = array();
  foreach ($tidArr as $key=&gt;$tids) {
    $terms[] =  current(reset($tids)); 
  }
  $uniqueTids = count(array_unique($terms));
  if($uniqueTids &lt; 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.
    }
I think what you are trying to drupalize/ise this into is something along the lines of:

function YOURTHEME_preprocess_views_view(&amp;$vars) {

  $view=$vars['view'];

  if (empty($view)) {
    watchdog('YOURTHEME', 'Rut roh Rastro....');
    return;
  }

  switch ($view-&gt;name) {

     case 'need':

        if ($view-&gt;current_display=='page') {

          $tid_arr = $view-&gt;field['term_node_tid']-&gt;view-&gt;field['term_node_tid']-&gt;items;
          $terms = array_keys($tid_arr);
          $number_tids = count($terms);

          if ($number_tids == 1) {
            drupal_add_css(path_to_theme() . '/css/my_custom_css.css');
          }

        }

        break;

     // any other views here

  }

}


with my basic comment being that you switch on the View's name in the preprocess function and do all your work in there instead of going out into another function.