Circular Sorting in Drupal 6, CCK & Views
I had an interesting request from a client today and I'm not exactly sure the best way to solve it using Drupal Views. They currently have a "Team Member" content type that represents members of their staff. Each staff member page has a link to all other staff member pages. So far that's not a problem.
They want this list to start with the person we're presently looking at and then proceed with who falls right after them in the "sorted" order. Then when it reaches the end of the list it wraps around and starts back at the beginning until they get back to themselves (similar to how a Circular Linked List would work).
So for example, assume I have Team Members A,B,C,D,E,F and G:
- If I'm looking at Team Member A, the l开发者_JS百科ist order is: A, B, C, D, E, F, G.
- If I'm looking at Team Member D, the list order is: D, E, F, G, A, B, C.
- If I'm looking at Team Member G, the list order is: G, A, B, C, D, E, F.
If I was dealing with some other data structure in a regular programming environment, I'm sure I could come up with some sort of algorithm to determine where we presently at and begin sorting from there and fake a "circular list". Views seems a little more tricky.
The only potential solution I can think of would be to create two separate views and then just join the results together after the fact.
- One view would be a sorted list of all team members that have a sort number greater than or equal to the present team member.
- A second view that is a sorted list of all team members that have a sort number less than or equal to the present team member.
I'm just curious if there are any more efficient ways of doing this kind of a sort with views?
As long as all your results can be contained on one page, you're probably best off with the solution you mention: manipulating two views (probably in the template, as Oswald suggests) so one is displayed after the other. However, if you do end up having lots of results, and needing paging ... you will probably need to do some fancy work in calculating the difference between sort numbers, and using this value in ORDER BY ... all that requiring the difficult task of rewriting the main Views query.
I tried this a few years ago and had enormous difficulties ... Merlin of Chaos, the main Views developer, simply stated somewhere that Views really wasn't meant to be for fancy programming, and that the way it handled queries wouldn't allow for much manipulation ... things would break. However, something has changed in the meantime ... I remember seeing a new function in Views which seemed to be made for the purpose of rewriting the main query.
Here is a tutorial on rewriting a views query: http://blog.raisedeyebrow.com/2010/04/rewrite-a-views-query/
Do note: due to the many things that are going on in Views, you should create a lot of dummy entries to make sure nothing is broken - if you re-write the query, it will be much more susceptible to error.
Actually, if you are acquainted with Drupal module building, for something like this it might just make more sense to build a custom module that allows you more control over the query and the display. This might actually be faster than a lot of experimentation and testing.
My choice would be the theming layer. I guess this can be done in a preprocess function. Maybe you have to override a template file. The Theme Developer module can tell you how the view is rendered and which overrides are possible.
精彩评论