There is no way right now (as far as I can tell) to select content that is part of the same book as the current node. For instance, you have a Views block that appears on book pages, but you want to limit the content in the view to content that is in the book which the block is appearing on.
There is an existing relationship Book:Top Level Book. Using this relationship and an argument one can restrict the contents of the view to a certain book. So one option is to add a default argument plugin that takes a nid from the url and uses that node's book root as the argument.
I'm working on this, but I thought I'd put it here already and see if it makes sense for this to be a Views patch, and if there are objections/suggestions.
Comment | File | Size | Author |
---|---|---|---|
#3 | views-current_book_def_arg-1524600-3.patch | 2 KB | goron |
#1 | views-current_book_def_arg-1524600-1.patch | 2.12 KB | goron |
Comments
Comment #1
goron CreditAttribution: goron commentedHere's a patch. With this in the code, a user can:
- Add 'top level book' relationship
- Add a 'node:nid' argument with that relationship
- Add a default value for that argument: 'book root from current node'
This works to filter the view's nodes to the ones that have the same root as the current node.
Comment #2
dawehnerJust a random idea, what about extending from views_plugin_argument_default_node to reuse the loading of the right nid.
Comment #3
goron CreditAttribution: goron commentedI like the idea. Here's a new patch.
- The plugin now extends argument_default_node and the nid is now loaded using that
- The only downside I see is that we could be doing some extra work if menu_get_object was called before and then we call node_load here
- I also removed the
!empty($node)
check, since I think theisset($node->book['bid'])
part covers it.Comment #4
dawehnerThis patch looks fine, though i didn't had some manual testing yet.
Comment #5
goron CreditAttribution: goron commentedI've tested on an existing D7 site as well as on a fresh D7 install, though of course it would be good to get another person to confirm.
Comment #6
dawehnerJust added it to the bug mentor sprint.
Comment #7
damiankloip CreditAttribution: damiankloip commentedThis works as expected for me too.
Comment #8
dawehnerI'm sorry i totally forgot to commit that patch.
Committed to 7.x-3.x, maybe someone will backport that.
Comment #9
Zippy CreditAttribution: Zippy commentedHi,
it seems that this patch has been integrated into views (im using views 7.x - 3.5dev) . So I tried to use the functionality an it worked - but not in any case.
If I build a view with a contextual filter and the "book root of current node" value as default the view will be displayed correctly on any edit form, but if the view is shown on an add form it will fail, because the "book root of current node" value seems to be empty. Is there any way to fix this? When the add form is shown there is a get value like this "?parent=2511" or so. The problem is, this is not the book root of the current node (bid) . It seems to be another one (mlid or so)...
Comment #10
Zippy CreditAttribution: Zippy commentedThe book parent is avalable inside the add form $form['book']['bid']['#default_value']
How to pass it to the 'Book root from current node'?
Comment #11
MustangGB CreditAttribution: MustangGB commented