Is there any way with Views to show 'sibling' book pages? Namely, nodes with the same parent and with the same depth as the current node. I tried adding a 'Book: parent' relationship, but I didn't get the results I wanted.

$view = new view;
$view->name = 'list_sibling_book_pages';
$view->description = 'List sibling book pages';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'nid' => array(
    'label' => 'Book parent',
    'required' => 1,
    'id' => 'nid',
    'table' => 'book_parent',
    'field' => 'nid',
    'relationship' => 'none',
  ),
));
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'p' => array(
    'order' => 'ASC',
    'id' => 'p',
    'table' => 'book_menu_links',
    'field' => 'p',
    'relationship' => 'none',
  ),
  'weight' => array(
    'order' => 'ASC',
    'id' => 'weight',
    'table' => 'book_menu_links',
    'field' => 'weight',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'status' => array(
    'operator' => '=',
    'value' => 1,
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'status',
    'table' => 'node',
    'field' => 'status',
    'relationship' => 'none',
  ),
  'nid' => array(
    'operator' => 'not empty',
    'value' => array(
      'value' => '',
      'min' => '',
      'max' => '',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'relationship' => 'nid',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('items_per_page', 100);
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);

Comments

merlinofchaos’s picture

Status: Active » Fixed

'current node' is one of those nebulous things that Drupal knows lots less about than people wish it did. Usually you get information about the current node using an argument. You can set the argument default to 'node from URL' and that'll usually do a good job of filtering the view to the current node.

What you want to do is take this view you've created, and add an argument: Node: nid. Set the relationship to the book parent relationship you've set. Set the "action to take if argument is not present" to 'provide default argument' and select 'Node from URL'.

I believe that will get you the results you want.

mike_r1977’s picture

Thanks a lot merlinofchaos for your reply: it gave me a great idea on how to proceed. I tried using the 'Node from URL' default argument option, but it didn't work as:

1) the query in live preview showed me it'd use the node from url as 'parent', while I'd need it to use the node's book parent as parent:

WHERE (node.status <> 0) AND (node_book_parent.nid = 124)

(where 124 is the node I need siblings of, and as such it doesn't work as parent for this purpose)

2) I heavily use aliases, so the node id would not be in the url anyhow.

However if I manage to get the node's book parent via php somehow, trying it out as argument in the live preview shows me it'd indeed get me the results I need, so thanks again.

mooffie’s picture

the query in live preview showed me it'd use the node from url as 'parent', while I'd need it to use the node's book parent as parent

In other words, you want to convert the incomming NID argument to its parent NID.

You can use a validator to do this conversion. Add the 'PHP code' validator to your argument. Note that in its description it explains that it can also be used to modify the argument.

How to find out a NID's parent NID? Google for "drupal book parent node" (see varesano's blog).

I heavily use aliases, so [...]

(I don't see why this should be a problem. Aliases are for the "outside" world. Drupal code doesn't really see them.)

merlinofchaos’s picture

As mooffie said, don't worry about the aliases, at least until you know something's wrong. If you're just aliasing standard URLs, then Node from URL will work just fine.

mike_r1977’s picture

moofie & merlinofchaos: thank you! Using your suggestions I was able to create the view I needed. Here it is, in case somebody else needs something like it:

$view = new view;
$view->name = 'list_sibling_book_pages';
$view->description = 'List sibling book pages';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'nid' => array(
    'label' => 'Book parent',
    'required' => 1,
    'id' => 'nid',
    'table' => 'book_parent',
    'field' => 'nid',
    'relationship' => 'none',
  ),
));
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'p' => array(
    'order' => 'ASC',
    'id' => 'p',
    'table' => 'book_menu_links',
    'field' => 'p',
    'relationship' => 'none',
  ),
  'weight' => array(
    'order' => 'ASC',
    'id' => 'weight',
    'table' => 'book_menu_links',
    'field' => 'weight',
    'relationship' => 'none',
  ),
));
$handler->override_option('arguments', array(
  'nid' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'default_argument_type' => 'node',
    'default_argument' => '',
    'validate_type' => 'php',
    'validate_fail' => 'not found',
    'break_phrase' => 0,
    'not' => 0,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'relationship' => 'nid',
    'default_options_div_prefix' => '',
    'default_argument_fixed' => '',
    'default_argument_php' => '',
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_php' => '$node = node_load($argument);
        if(isset($node->book)) { // book page
              $parent = book_link_load($node->book[\'plid\']);
              $handler->argument = $parent[\'nid\'];
              return TRUE;
        }',
  ),
));
$handler->override_option('filters', array(
  'status' => array(
    'operator' => '=',
    'value' => 1,
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'status',
    'table' => 'node',
    'field' => 'status',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('items_per_page', 100);
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
  'grouping' => '',
  'type' => 'ul',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', 'List sibling book pages');
$handler->override_option('block_caching', -1);
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

wolan’s picture

Thanks a bunch Mike.

That view saved me 5 hours of headache!

d.o rocks somehow!

/wolan

lubnax’s picture

subsribe

calte’s picture

Saved me a couple hours!!
Thank you so much, just thought I'd mention this still works with the Drupal 7 version of Views.

kevin.mcnamee@mailbox.org’s picture

subscribing

mpa3b’s picture

good thing!
thanks.

is there any way to display book contents on book page?

spineless’s picture

In views 2 (using Drupal 7) there is a new variable to isolate the child pages under any page.

1. Create a relationship for book: top level book
2. Create a relationship for book: parent
3. Create a contextual filter for content:nid ( link the relationship "book: top level book" this this filter). In this filter select "provide default value" of type " Book root from current node".
4. Create a filter criteria "book: depth" (link the relationship "book:parent" to this filter criteria.

gurtner’s picture

Thanks @spineless, I tried it and it didn't work for me :-(