0

I have a view that shows related products by means of a contextual filter (PHP). If the returned nid values from the contextual filter is for example "20697, 20735, 20768, 20903, 20695, 21863" I want the nodes to be rendered in that order.

But they don't, I have tried all kinds of settings with no luck. The order given is in fact the delta of a certain field, but when I try to use the delta version of that field as a sort criteria, it is simply ignored, even if I add a relationship under "Advanced".

Anyone who recognizes this behaviour?

EDITED: This is my contextual PHP filter. It fetches all the nid's entered in a certain node reference field as I explained above. And I want them to show in that same order, so that the admin user can sort the order by dragging the rows in the Node reference field:

$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;
    $value = FALSE;
         return $value;
} else {
        $limit = count($node->field_lagg_till_givargrupp['und']);
        for($i = 0; $limit > $i; ++$i){
            $group_nid = $node->field_lagg_till_givargrupp['und'][$i]['nid'];
            $group_node = node_load($group_nid);
            $limit2 = count($group_node->field_koppla_givare['und']);
            for($x = 0; $limit2 > $x; ++$x){
                $value .= $group_node->field_koppla_givare['und'][$x]['nid'].",";
            }          
        }
        return rtrim($value, ",");
}

EDITED: this is the SQL query (I have added a few nids as an example);

SELECT node.nid AS nid, field_data_field_koppla_givare.delta AS field_data_field_koppla_givare_delta, field_data_field_koppla_givare.field_koppla_givare_nid AS field_data_field_koppla_givare_field_koppla_givare_nid, 'node' AS field_data_field_givargrupper_node_entity_type, 'node' AS field_data_field_huvudbild_givare_node_entity_type, 'node' AS field_data_field_tumnagel_givare_node_entity_type, 'node' AS field_data_field_underrubrik_givare_node_entity_type, 'node' AS field_data_body_node_entity_type, 'node' AS field_data_field_artikelnr_produkt_node_entity_type, 'node' AS field_data_field_pris_givare_node_entity_type
FROM 
{node} node
LEFT JOIN {field_data_field_koppla_givare} field_data_field_koppla_givare ON node.nid = field_data_field_koppla_givare.entity_id AND (field_data_field_koppla_givare.entity_type = 'node' AND field_data_field_koppla_givare.deleted = '0')
WHERE (( (node.nid IN('20697', '20735', '20768', '20903', '20695') ) )AND(( (node.status = '1') )))
ORDER BY field_data_field_koppla_givare_field_koppla_givare_nid ASC

And this is the simple query without the delta field and without the order by criteria trying to sort in hook_views_pre_render() instead (see this question about that Sorting logic with anonymous function does not work):

SELECT node.nid AS nid, 'node' AS field_data_field_givargrupper_node_entity_type, 'node' AS field_data_field_huvudbild_givare_node_entity_type, 'node' AS field_data_field_tumnagel_givare_node_entity_type, 'node' AS field_data_field_underrubrik_givare_node_entity_type, 'node' AS field_data_body_node_entity_type, 'node' AS field_data_field_artikelnr_produkt_node_entity_type, 'node' AS field_data_field_pris_givare_node_entity_type
FROM 
{node} node
WHERE (( (node.nid IN('20697', '20735', '20768', '20903', '20695') ) )AND(( (node.status = '1') )))

As you can see I am really stuck in all ways about this ...

  • Could you please add screenshot of contextual filter configuration? – Nikhil Banait Apr 19 '16 at 15:33
  • If you don't use paging and the result will always be one page, you could manually sort the results in one of the View hooks that run after the query has run, ie. hook_views_pre_render. This can be done in your theme's template.php file. – J. Reynolds Apr 20 '16 at 7:37
  • Thanks for your answer, I will try that. I want them sorted in the same order as they appear in the concerned node reference field "field_koppla_givare" above. – TBJ Apr 20 '16 at 7:55
0

You can use a custom sort in your module like

/** 
* Implementation of hook_views_query_alter(). 
 */ 
function yourmodule_views_query_alter(&$view, &$query) { 
  if ($view->name == 'your_machine_name_view') { 
        $query->add_orderby('node', 'created', 'DESC'); 
    }
  } 
}
  • Hi and thanks, I have tried this too and the criteria needed is still ignored. I added '$query->add_orderby('field_data_field_koppla_givare', 'delta', 'DESC');' But there is obviously something I don't understand in the views query itself. – TBJ Apr 20 '16 at 7:58
  • Can you provide us your query? Go here: **admin/structure/views/settings **, and activate the option "Show SQL" , so we can see what order it is taking – DarkteK Apr 22 '16 at 1:08
0

PROBLEM SOLVED. Just in case anyone would have the same problem... there was in fact nothing wrong with my contextual filter. With help (thanks J. Reynolds) I solved the sorting issue with hook_views_pre_render(). See this question for the answer: Sorting logic with anonymous function does not work

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.