3

I have some node IDs I'm passing to a view; however the sort order is wrong.

Here's my pseudo code:

//query to grab the nids

if (!empty($nodeids)) {
  $view = 'taxo_listing';
  foreach ($nodeids as $nid) {
    print views_embed_view($view, block_1, $nid);
  }
}

What I need to do is pre-build the view then run implement hook_views_query_alter to sort the view before printing out the view.

Been looking at http://views.doc.logrus.com/ for a while now, but it's all a bit confusing, can anyone offer any clues as how to go about this? I know what I want to do but unsure how to go about it, sound familiar guys?

Thanks in advance.

0

I don't know where $nodeids comes from, but I expect it's an array of nids from a database query? If so, you're effectively running a view with a view and can probably get away without writing any code for hooks.

Therefore, you can either:

  • Sort the $nodeids yourself with uasort() which will mean probably returning more fields to sort with and maybe writing your own comparison function.
  • Let Views handle all of this, thus gaining access to the UI and all it's powers.

The latter 'view within a view' approach is better for sustainability and sanity because it uses the Views UI and theme layers more consistently. A good approach that's worked for me is:

  1. Make a view that can either:
    • return the properly sorted $nodeids, or;
    • get it to take an argument of a comma-separated list of nids you can pass implode(',', $nodeids) to.
  2. use Views PHP to add a field that can contain your views_embed_view($view, block_1, $nid), probably with $nid replaced by $row->nid but will depend on your setup.
  3. Add any sorting or sorting code to the view you want.
| improve this answer | |
  • Many thanks for your advice, The view in question accepts NIDS as arguments, but ignores the sort I have on 'title' using the wonderful view UI. The query is run on a separate table ie not the one that I have in my view. I was hoping that there was a way to pre-build a view, sort it then print it. Plus I would really like to learn more about how views works under the hood. – user4560 Dec 21 '11 at 15:28
  • if you could tell more about what taxo_listing is doing/what you want it to do, that might shed some more light on an answer as well, as, to be blunt, views does not "ignore" sorts on titles – Jimajamma Feb 20 '12 at 3:49
  • If you can see sorting on Live Preview and views_embed_view is not respecting it, it clearly is a bug in Views. views_embed_view is supposed to respect all argument, sorts, filters, etc. – AyeshK Nov 2 '12 at 20:17
0

You can try hook_views_pre_render as well

This hook is called right before the render process. The query has been executed, and the pre_render() phase has already happened for handlers, so all data should be available.

Example

function hook_views_pre_render(&$view) {
  // Scramble the order of the rows shown on this result page.
  // Note that this could be done earlier, but not later in the view execution
  // process.
  shuffle($view->result);
}

Read Views hooks

| improve this answer | |

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.