3

Is there any option available in Drupal views to sort views result using the arguments provided (contextual filters)

I am trying to integrate a search functionality using views. My problem is that contextual filter will list only matching results, I need all the results but it should sort according to the arguments passed using contextual filters. Is that possible?

I.E. I pass the following string to a contextual NID argument: 1099+1100+1102+1098+1101+1097

I expect the order of the nodes in the view to be the same as the order as passed in through the argument.

4
  • can you give any explanation?? Commented Jan 11, 2013 at 7:26
  • Suppose we are listing node tiles using views and if we give node id in contextual filters it will filter the results and show the title corresponding to the node id we passed. Like that I need to sort the result according to the argument i.e. matching results should list first following unmatched results. Commented Jan 11, 2013 at 7:35
  • can't you give the same field in sorting. Commented Jan 14, 2013 at 5:01
  • I am trying to integrate a search functionality using views. My problem is that contextual filter will list only matching results, but i need all the results but it should sort according to the matching results. Is that possible? Commented Jan 14, 2013 at 5:13

1 Answer 1

1

This is what helped for me.

https://stackoverflow.com/a/2474164/28672

Using the second option to generate the view manually then modifying the array in the result property.

And this is the code I used to sort the results to the argument

 // Sort the view based on the argument now we've generated it.
  $results = $view->result;
  $view_argument_array = explode('+', '1099+1100+1102+1098+1101+1097');
  $new_result_order = array();
  foreach ($view_argument_array as $nid) {
    foreach ($results as $result) {
      if ($result->nid == $nid) {
        $new_result_order[] = $result;
      }
    }
  }
  $view->result = $new_result_order;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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