0

I have a view with one field that shows rendered nodes, and a contextual filter (multiple values allowed) that provides the nids that should be used in the view.

Each node (with nid from the context filter) contains a node reference field with multiple values (nids of related products).

My problem is that a number of these related products, are in fact unpublished nodes. So how can I exclude these unpublished nodes from showing up in the views result? Just adding the filter Published = Yes does not work since the field in the view always is published. It is some of the rendered nodes, produced by that field, that in their turn are unpublished.

UPDATE: in a custom module I have now added this snippet:

function mymodule_views_post_execute(&$view) {
  if ($view->name === 'my_view' && $view->current_display === 'related_products') {
    foreach ($view->result as $result) {

      foreach($result->field_field_add_products as $key => $product){
        if($product['rendered']['#node']->status == 0){
          unset($result->field_field_add_products[$key]);
        }
      }
    }
  }
}

...and this does actually exclude the unwanted products. BUT they don't just leave gracefully. They leave a gap in the (repsonsive) grid that I use to show these products. How do I exclude them AND the space they occupy in my views grid?

UPDATE: this is my PHP snippet in the contextual filter:

$node = menu_get_object();
$value = null;
if(empty($node->field_lagg_till_givargrupp['und'][0]['nid'])){
    $node->field_lagg_till_givargrupp['und'][0]['nid'] = NULL;
    return FALSE;
} else {
    $groups = $node->field_lagg_till_givargrupp['und'];
    foreach($groups as $group){
        $group_nid = $group['nid']; // hämta node-id
                $value .= $group_nid.",";
    }
    return rtrim($value, ",");
}

It works well and supplies the nids wanted (the node ids for the nodes with multiple products grouped in node reference fields).

And this is a partial screenshot of the view: enter image description here

I use the field Innehåll: Titel (Content: Title) to group the result by the title of each product group. The field Innehåll: Koppla givare (Content: related products) produces all the nodes present in the node reference fields. If there are 20 products entered in that multiple value field, I will have 20 rendered nodes as a result, of which some are not publised. See the resulting view below. The gaps are unpublished nodes that I succeeded in excluding with my snippet above.

enter image description here

So how do I rebuild the view result so that there are no gaps?

  • Hello! can you also post a screenshot of your views setup? – Japo Domingo Mar 14 '17 at 21:04
  • Hello, thanks for your comment. I have updated my question above. – TBJ Mar 15 '17 at 7:59
0

Ok, so this was in fact very simple... though it took me a great deal of time to find the solution. I removed the snippet in the custom module above and instead I added the code below in template.php. Hope it helps somebody. I found the solution here: http://agaric.com/blogs/altering-views-results

function mytheme_views_pre_render(&$view) {      
if ($view->name === 'myview' && $view->current_display === 'mydisplay') {
        foreach($view->result as $index=>$val){
          $product_node = node_load($val->field_data_field_related_product_nid);
          if($product_node->status === '0'){
             unset($view->result[$index]);
          }
        }
    }
}

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.