Is there a way to get the top-most ancestor to use as an argument for a view?
I have a hierarchy set up like this:
Section (page) 1
- Page 1
- Event 1
- Page 2
- Page 2a
- Page 3
- Event 2
Section (page) 2
- Page 4
- Page 5
- Event 3
- Page 6
- Page 6a
- Page 6b
- Page 7
- Event 4
etc.
There is a block on every page listing upcoming events. I'd like this to be context-sensitive, i.e. for all the pages in Section 1, it would list Event 1 and Event 2, and in Section 2, it would list Event 3 and Event 4.
I can get it to work where on the page for sections 1 and 2, they display the proper events (1 & 2 for s1, and 3 & 4 for s2). However, on Page 1, it only shows Event 1 (not unexpectedly, since it's looking for all its descendants) and on P2 and P3, no events are shown.
Similarly, for S2, E3 and E4 show on S2's main page, but not on any of its sub-pages (p4, p5, p6) and only E4 shows on P7 (not E3).
So I guess what I'm trying to figure out is, is there a way for P1 (and, importantly, P2a, etc.) to know that S1 is its ancestor, so that information can be passed as an argument for a view?
Thanks--I know that's complicated, but if there's any more information that would be helpful, let me know.
Isaac
Comments
Comment #1
ronan commentedHi,
Unfortunately that is not possible with the current code, and due to the complexity of the request it is not likely to happen at least until version 2. The way the module currently works, such an argument would be extremely inefficient.
If you don't have a lot of sections you may be able to fake this by creating a separate view for each section and simply hardcode the id of the top layer in the argument code of each view. It's a hack but for a small handful of sections it;s probably the simplest way of accomplishing your needs.
Hope this helps
ronan
Comment #2
isaac.niebeling commentedThat's what I was thinking might be the case. At this point, there are only 7 sections plus the main home page where this would be an issue, so I guess creating individual views isn't a huge deal. I was hoping for something more automated, but if that's not in the cards, then we'll skip it. When you need testers for v2, let me know.
Comment #3
ronan commentedsorry about that. a more automated way would be to write a custom module which creates a block that crawls up the hierarchy of the current node to find the top ancestor and then passes that along to the view, but that's a lot of effort and probably not that much cleaner in the end.
good luck
r
Comment #4
isaac.niebeling commentedOK, so what I did is created a theme function in my template.php:
which I call in the view argument handling area like:
So--what am I missing? It's kind of hacked-together code, but it works. Is there something that would work better? Is this going to be super resource-intensive? (I'm still fairly new to Drupal, so any advice on that front in particular would be great.)
Thanks
Comment #5
ronan commentedThat's pretty much how I would have handled it. You may want to code a little more defensively by adding an another
ifstatement to check that node_load returned a valid node or even put it in theifyou already have:(code not tested)
This is not strictly necessary, but it will protect you from unexpected return values (always a good thing with recursive functions).
It will not be too resource intensive per-se, but the longer each branch of the hierarchy, the more nodes you'll have to load on every node view, so you may have scalability issues if the site gets very large or very popular.
Thanks for sharing your solution.