Great module!! I noticed that although the node references can be ordered via drag and drop while editing the parent node, this has no effect on the order that the view results are displayed.
I came up with a little hack using the views API that makes this work for specific fields. I don't know if there is any way to safely incorporate this into your module, but it is a nice feature to have. If this can't be added to the module hopefully it'll at least help someone else out if they want this functionality.
function modulename_views_pre_render(&$view) {
if ($view->name == 'field_name') {
$new_result = array();
$nids = explode(',', $view->args[0]);
foreach ($nids as $nid) {
foreach ($view->result as $res) {
if ($res->nid == $nid) {
$new_result[] = $res;
break;
}
}
}
$view->result = $new_result;
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 2009-08-20_1435_nr.png | 12.04 KB | rokr |
| #3 | 2009-08-20_1443_nrv.png | 18.62 KB | rokr |
Comments
Comment #1
joachim commentedThis might be a neater way to do it:
http://www.eggheadcafe.com/conversation.aspx?messageid=31792141&threadid...
could add a sort order that picks up the nid argument.
Comment #2
adamo commentedThat's a cool trick. :) Neither MySQL nor PostgreSQL have a CHARINDEX function though.
Comment #3
rokrI second that request. Views uses standard order to display a list of nodes. Since mutliple items of a node reference field can be reordered with drag and drop it would be nice to represent this order given by author to views display.
Have a look at the screenshots and the order of titles used.
cheers, Ronald
Comment #4
rokrAdamo, thanks fpr this snippet, works great!
cheers, Ronald
Comment #5
tradivarium commentedHello Ronald and all,
I ran over exactly the same problem: so it would be great to get this snippet to work for my folk music website, for user generated tunebooks like http://archive.folx.org/node/849
But how and where do I apply it?
Thanks a lot in advance!
Stephan
Comment #6
rokrHi stephan,
most drupal sites use their own mini module to override functions used elsewhere. Have a look at http://drupal.org/node/70903
Google for custom or mini module, build your own, paste the lines above into the module and enable it.
Hope this helps, Ronald
Comment #7
tradivarium commentedHi Ronald,
needed a while to figure it out, but works perfectly now.
Thanks a lot!
Stephan
Comment #8
nitram079 commentedWorks like a charm, thx guys.
Here some pointers for some other noobs like me:
- make sure you replace modulename in function modulename_views_pre_render(&$view) with the name you have chosen
- make sure to replace the 'field_name' in ($view->name == 'field_name') with the name of your view (which must be equal to the filed name of the noderef CCK field
- leave out the closing ?>
cheers,
Martin
Comment #9
gcassie commentedhandy snippet, adamo.
another vote for getting this included in the module.
Comment #10
joachim commentedHmmm..... I really don't like code that has to run on every view and check the view name.
Also, in the module (as opposed to your site's code), how would we know which view name to check? We'd have to load all CCK fields and ask which ones are noderefs and which of those have their formatter set to show a view. That's a lot of stuff to do on every single view even if it's totally irrelevant -- which feels like too much over an imposition to me.
Any better ideas?
Comment #11
adamo commentedI don't really like it either... I don't think a single conditional is going to make any noticeable performance difference, but having to check all CCK fields (etc, etc) might. Perhaps you could make use of the Cache API and generate the list of view names to check once, then cache it. That would at least be less work than generating the list every time, and it could be cached in RAM depending on what caching engine people are using.
It would seem that the proper way to implement sorting would be to create a Views sort handler. Then you would just add that sort handler to the Views you need it for. But all of the sort handlers I've looked at (the ones that come with Views) are just adding an ORDER BY clause to the query. Unless there's a way to use an ORDER BY clause to specify the exact order of rows given a list of IDs (don't think so), or for a Views sort handler to re-sort the results array post query (no idea), then I don't see how that can work.
Perhaps you could implement a dummy sort handler and not alter the query at all, and then in hook_views_pre_render() check if that sort handler has been added to the view, and if so sort the results as above.
Comment #12
joachim commented> Perhaps you could implement a dummy sort handler and not alter the query at all, and then in hook_views_pre_render() check if that sort handler has been added to the view, and if so sort the results as above.
That's quite possibly a cunning plan!
I don't have time to work on this right now, so feel free to beat me to it :)
Comment #13
Firetracker commentedHi,
Any news regarding this issue?
Cheers
Zap
Comment #14
joachim commentedI'm afraid this is very low down on my list of priorities. I am very busy with paid work, so have little time to work on modules in my own time. I will however review patches posted here, and I will try do so fairly promptly. If you need this feature urgently, you can always contact me about paid work :)
Comment #15
webflo commentedUse this view to order the node reference by delta from parent node. No hook or custom module is needed.
This view is for Node reference views 6.x-1.2
Comment #16
joachim commentedCool!
This seems to depend on a recent version of CCK, as with whatever version I initially had on my test site I got an error.
Furthermore, I can't add this to the default example view, as the sort handler's table is named after the CCK nodeference field. So it's going to have to be an extra note in the documentation.
Comment #17
webflo commentedThis view has a different approach than the current default view. The first argument (all referenced nids are completely ignored). This views is build on the second argument (the source nid). Than we use a relationshop to get all referenced nodes and order these nodes by the fields-delta.
(sry for my bad english)
Comment #18
joachim commentedIf you read the source code of the module, you'll see I tried this approach and discarded it:
// This is an alternative approach: send just the nid and requery.
// this requires a view with:
// - a relationship on the CCK noderef field,
// - an argument of node ID
// - fields for actual display set on the noderef relationship
// It's more complex to set up, seems wasteful as CCK has given us the nids already
// and in testing (admittedly with a tiny data set)
// was about the same speed, marginally slower in fact.
Your sort works on the existing view -- but in both cases, it can't be provided in the default example view because it depends on the name of the user-created field.
Comment #19
webflo commentedYes i know, i read the source code. But i think there is no way to use the referenced nids and order these by field delta ...
Comment #20
joachim commentedOh, you're right. Sorry -- I thought it was working with the current view and it's not.
The really cool and awesome thing would be to write a smart relationship handler that depends on the name of the view... which I don't have time to do.
In the meantime, we can add the details of how to do it with the relationship to the documentation.
Comment #21
docwilmot commented@webflo
fantastic.
the view wouldnt import until i did a find-and-replace from field_node_ref to field_my_field. it kept saying handler for field_node_ref not found.
thanks
Comment #22
ak commentedthe view works very well, thanks
Comment #23
asb commentedI ran into this issue as well, and it's a real showstopper in some use cases; if there's no "logical" sorting order, this is a real problem; if the 'Node reference views' view can be given a sorting order, that's a workaround in other cases since 'Node reference views' honors the view's sorting order.
Confirming that the exported view from #15 can not be imported if not manually altered (#21). Error message (sorry, localized Drupal):
This message complains that 'node_data_field_node_ref.delta' does not exist.
Used versions:
Doing a S&R on 'field_node_ref' with the actual field name replaces 9 strings and produces an export that is valid (#21) and works as advertised.
Does anyone volunteer to write a how to for the documentation (#20)?
Greetings, -asb
Comment #24
luksakhi everyone
i imported your view and worked great until i referenced one node twice from different parent nodes. the results are duplicate results.
any ideas?
Comment #25
davepoon commentedThank you for this excellent solution!
Comment #26
bbcSubscribing. Thanks much to webflo and asb. An excellent solution!
Comment #27
damienmckennaI dug into this. What happens is that Views gets passed the arguments, which runs a query like this:
The difficulty is telling it how to order those results.
Luckily there are two modules worth trying:
Comment #28
damienmckennaFYI, lots of good suggestions, if you want to work on it some more: http://stackoverflow.com/questions/866465/sql-order-by-the-in-value-list
Comment #29
jduhls commentedThanks for the initial snippet! It solved both the order problem AND some strange caching problem. If I made changes to the parent node, they weren't immediately visible when the page was refreshed. This was in dev mode with caching disabled globally. Thanks for the "enhancement"!
Comment #30
joachim commentedClosing, as this module is now deprecated.
See #1257780: Sort Order Based on Delta for the issue on the replacement module.