0

difficult to formulate a question

I have made a View, which display title, image, body(trimmed) on nodes pages with Content type - Home, its related nodes to this one(. Home has different categories - taxonomy, for example looks like this:

  1. Homes one
  2. homes two
  3. homes three

So for example if my current node has taxonomy term - homes two i should display only nodes(title, image, body(trimmed) of this term. I did it like this - with contextual filters Content: Nid - i exclude current node to not display it in my view and

Content: Has taxonomy term ID

When filter is not available - i provide my own value, php code:

 $node = node_load(arg(1));
 if($node) {
 foreach($node->field_home_category[LANGUAGE_NONE] as $term) { 
    $terms[] = $term['tid']; 
 }
    return implode('+',$terms);
  } 
  else { 
    return
  }

$node->field_home_category[LANGUAGE_NONE] - is a field which contains taxonomy term id.

And its works fine, but if my node is only single in homes two taxonomy term, the view returns nothing(for obvious reasons) and if this happens i need to display any other Home nodes.

I think i should load my taxonomy vocabulary and get all the terms and pass them to my view in this situation. Like this:

$tree = taxonomy_get_tree(2); // vocabulary id - is 2
    foreach ($tree as $term) {
        $options[] = $term->tid;
      }
      return implode('+', $options);

but i dont know how to add this to my view.

Something like this ->

PSEUDO CODE:

IF THERE IS NO NODES IN CURRENT TERM
   DO 
      LOAD VOCABULARY OF HOMES
    AND 
      DISPLAY ANY AVALIABLE NODES OF THIS VOCABULARY

//////////////////////////////////////////////////////

I will post full solution, maybe helps someone:

//////////////////////////////////////////////////////

No results behavior is great, but there is few situations:

For example:

homes two contains 2 homes, one is excluded, so view will display only 1 home, but we need 3 homes to be displayed all the time.

What i did is created new view, which displays 3 homes randomly(global Random sort criteria) and excluded nid in contextual filter, i allow to pass multiple values to nid. and in THEME_views_pre_render(&$view) i did next thing:

function THEME_views_pre_render(&$view) {
  if ($view->name == 'home') { // check is this my view
      $args = $view->args; //passed arguments
      $results = &$view->result; // this is important , you should pass it by reference
      if (!empty($args) && isset($args[0]) && isset($args[1])) {
        $node = node_load($args[1]);
        if ($node->type == 'home') { // check node type
            foreach($view->result as $row) {
                $restricted[] = $row->nid; //push current nodes to array, to pass them later to our new view, to not display them
            }
            $result_count = count($view->result);
            switch ($result_count) {
                case 1: // if one home
                    //gett the results of my view which display all nodes
                    // and do not display node which already displayed
                    // aka $restricted array
                    $result_array = views_get_view_result('related_homes_and_features', 'block_3', $restricted);
                    //add this results to current view
                    //
                    array_push($results, $result_array[0],$result_array[1]);
                    break;
                case 2:
                    $result_array = views_get_view_result('related_homes_and_features', 'block_3', $restricted);
                    array_push($results, $result_array[0]);
                    break;
                case 3:
                    $result_array = views_get_view_result('related_homes_and_features', 'block_3');
                    array_push($results, $result_array[0], $result_array[1], $result_array[2]);
                    return;
                    break;
            }
        }
    }
  }
}

home that helps someone.

4

Use "No results behavior" of the view ; specify a new view ("Global: View area"), showing all nodes, excluding the taxonomy term "homes two".

  • i resolved it by adding ("Global: PHP") and added views_embed_view - embed my new view which displays all the nodes. – Petro Popelyshko Dec 6 '12 at 16:56
  • added full solution to my question. – Petro Popelyshko Dec 7 '12 at 11:21
  • 1
    nice :) Is there any chance you could gain some performance by using views_pre_build ? – Gregory Kapustin Dec 7 '12 at 11:29

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.