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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

goron’s picture

Title: Filter or default argument for "belonging to current book" » Default argument for "book root of current node"
Status: Active » Needs review
FileSize
2.12 KB

Here'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.

dawehner’s picture

Status: Needs review » Needs work
+++ b/modules/book/views_plugin_argument_default_book_root.incundefined
@@ -0,0 +1,26 @@
+  function get_argument() {
+    foreach (range(1, 3) as $i) {
+      $node = menu_get_object('node', $i);
+      if (!empty($node) && isset($node->book['bid'])) {
+        return $node->book['bid'];
+      }
+    }
+
+    if (arg(0) == 'node' && is_numeric(arg(1))) {
+      $node = node_load(arg(1));
+      if (!empty($node) && isset($node->book['bid'])) {
+        return $node->book['bid'];
+      }
+    }

Just a random idea, what about extending from views_plugin_argument_default_node to reuse the loading of the right nid.

goron’s picture

Status: Needs work » Needs review
FileSize
2 KB

I 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 the isset($node->book['bid']) part covers it.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

This patch looks fine, though i didn't had some manual testing yet.

goron’s picture

I'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.

dawehner’s picture

Just added it to the bug mentor sprint.

damiankloip’s picture

This works as expected for me too.

dawehner’s picture

Version: 7.x-3.x-dev » 6.x-3.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

I'm sorry i totally forgot to commit that patch.

Committed to 7.x-3.x, maybe someone will backport that.

Zippy’s picture

Hi,

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)...

Zippy’s picture

The book parent is avalable inside the add form $form['book']['bid']['#default_value']
How to pass it to the 'Book root from current node'?

MustangGB’s picture

Issue summary: View changes
Status: Patch (to be ported) » Closed (won't fix)