0

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.
}

0

I think what you are trying to drupalize/ise this into is something along the lines of:

function YOURTHEME_preprocess_views_view(&$vars) {

  $view=$vars['view'];

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

  switch ($view->name) {

     case 'need':

        if ($view->current_display=='page') {

          $tid_arr = $view->field['term_node_tid']->view->field['term_node_tid']->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.

  • 1) Thank you for helping me out so thoroughly, can't even vote your answer "up" cause I don't have enough xp. 2) that preprocess looks deliciously clean and exactly what me and my drupal bootstrap were looking for. Small thing I had to change, was due to me misleading you: $tidArr didn't hold the tid's as such yet ... had to go one level deeper, but the array keys of that array are not sequential (and don't start at 0) ... the keys used on that level are the Node Id's, so have to create a new array and then do an array_unique like I did above :) – belgianwolfie Aug 19 '12 at 18:15
  • I wondered what was going on in there :) but glad I could help out! – Jimajamma Aug 19 '12 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.