Commit 5baece5c authored by fago's avatar fago

Issue #2089491 by drunken monkey, Bojhan: Add a property containing all book ancestors.

parent db285551
......@@ -1210,18 +1210,21 @@ class EntityMetadataIntegrationTestCase extends EntityWebTestCase {
*/
function testBookModule() {
$title = 'Book 1';
$node = $this->drupalCreateNode(array('title' => $title, 'type' => 'book'));
$node2 = $this->drupalCreateNode(array('type' => 'book', 'book' => array('bid' => $node->nid)));
$node = $this->drupalCreateNode(array('title' => $title, 'type' => 'book', 'book' => array('bid' => 'new')));
$book = array('bid' => $node->nid, 'plid' => $node->book['mlid']);
$node2 = $this->drupalCreateNode(array('type' => 'book', 'book' => $book));
$node3 = $this->drupalCreateNode(array('type' => 'page'));
// Test whether the properties work.
$wrapper = entity_metadata_wrapper('node', $node2);
$this->assertEqual("Book 1", $wrapper->book->title->value(), "Book title returned.");
$this->assertEqual($title, $wrapper->book->title->value(), "Book title returned.");
$this->assertEqual(array($node->nid), $wrapper->book_ancestors->value(array('identifier' => TRUE)), "Book ancestors returned.");
$this->assertEqual($node->nid, $wrapper->book->nid->value(), "Book id returned.");
// Try using book properties for no book nodes.
$wrapper = entity_metadata_wrapper('node', $node3);
$this->assertException($wrapper, 'book');
$this->assertException($wrapper, 'book_ancestors');
}
/**
......@@ -1260,11 +1263,12 @@ class EntityMetadataIntegrationTestCase extends EntityWebTestCase {
$node = $this->drupalCreateNode(array('title' => $title, 'type' => 'page'));
$wrapper = entity_metadata_wrapper('node', $node);
foreach ($wrapper as $key => $value) {
if ($key != 'book' && $key != 'source' && $key != 'last_view') {
if ($key != 'book' && $key != 'book_ancestors' && $key != 'source' && $key != 'last_view') {
$this->assertValue($wrapper, $key);
}
}
$this->assertException($wrapper, 'book');
$this->assertException($wrapper, 'book_ancestors');
$this->assertEmpty($wrapper, 'source');
$this->assertException($wrapper->source, 'title');
$this->assertEmpty($wrapper, 'last_view');
......
......@@ -20,4 +20,11 @@ function entity_metadata_book_entity_property_info_alter(&$info) {
'description' => t("If part of a book, the book to which this book page belongs."),
'getter callback' => 'entity_metadata_book_get_properties',
);
}
\ No newline at end of file
$properties['book_ancestors'] = array(
'label' => t("Book ancestors"),
'type' => 'list<node>',
'computed' => TRUE,
'description' => t("If part of a book, a list of all book pages upwards in the book hierarchy."),
'getter callback' => 'entity_metadata_book_get_properties',
);
}
......@@ -23,7 +23,19 @@ function entity_metadata_book_get_properties($node, array $options, $name, $enti
if (!isset($node->book['bid'])) {
throw new EntityMetadataWrapperException('This node is no book page.');
}
return $node->book['bid'];
switch ($name) {
case 'book':
return $node->book['bid'];
case 'book_ancestors':
$ancestors = array();
while (!empty($node->book['plid'])) {
$link = book_link_load($node->book['plid']);
array_unshift($ancestors, $link['nid']);
$node = node_load($link['nid']);
}
return $ancestors;
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment