/** * Fetches the book link for the previous page of the book. * * @param array $book_link * A fully loaded book link that is part of the book hierarchy. * * @return array * A fully loaded book link for the page before the one represented in * $book_link. */ public function prevLink(array $book_link) { // If the parent is zero, we are at the start of a book. if ($book_link['pid'] == 0) { return NULL; } // Assigning the array to $flat resets the array pointer for use with each(). $flat = $this->bookManager->bookTreeGetFlat($book_link); $curr = NULL; do { $prev = $curr; list($key, $curr) = each($flat); } while ($key && $key != $book_link['nid']); if ($key == $book_link['nid']) { // The previous page in the book may be a child of the previous visible link. if ($prev['depth'] == $book_link['depth']) { // The subtree will have only one link at the top level - get its data. $tree = $this->bookManager->bookSubtreeData($prev); $data = array_shift($tree); // The link of interest is the last child - iterate to find the deepest one. while ($data['below']) { $data = end($data['below']); } $this->bookManager->bookLinkTranslate($data['link']); return $data['link']; } else { $this->bookManager->bookLinkTranslate($prev); return $prev; } } }
/** * Generates HTML for export when invoked by book_export(). * * The given node is embedded to its absolute depth in a top level section. For * example, a child node with depth 2 in the hierarchy is contained in * (otherwise empty) <div> elements corresponding to depth 0 and depth 1. * This is intended to support WYSIWYG output - e.g., level 3 sections always * look like level 3 sections, no matter their depth relative to the node * selected to be exported as printer-friendly HTML. * * @param \Drupal\node\NodeInterface $node * The node to export. * * @throws \Exception * Thrown when the node was not attached to a book. * * @return array * A render array representing the HTML for a node and its children in the * book hierarchy. */ public function bookExportHtml(NodeInterface $node) { if (!isset($node->book)) { throw new \Exception(); } $tree = $this->bookManager->bookSubtreeData($node->book); $contents = $this->exportTraverse($tree, array($this, 'bookNodeExport')); return array('#theme' => 'book_export_html', '#title' => $node->label(), '#contents' => $contents, '#depth' => $node->book['depth'], '#cache' => ['tags' => $node->getEntityType()->getListCacheTags()]); }
/** * Builds the table portion of the form for the book administration page. * * @param \Drupal\node\NodeInterface $node * The node of the top-level page in the book. * @param array $form * The form that is being modified, passed by reference. * * @see self::buildForm() */ protected function bookAdminTable(NodeInterface $node, array &$form) { $form['table'] = array('#theme' => 'book_admin_table', '#tree' => TRUE); $tree = $this->bookManager->bookSubtreeData($node->book); // Do not include the book item itself. $tree = array_shift($tree); if ($tree['below']) { $hash = Crypt::hashBase64(serialize($tree['below'])); // Store the hash value as a hidden form element so that we can detect // if another user changed the book hierarchy. $form['tree_hash'] = array('#type' => 'hidden', '#default_value' => $hash); $form['tree_current_hash'] = array('#type' => 'value', '#value' => $hash); $this->bookAdminTableTree($tree['below'], $form['table']); } }
/** * Builds the table portion of the form for the book administration page. * * @param \Drupal\node\NodeInterface $node * The node of the top-level page in the book. * @param array $form * The form that is being modified, passed by reference. * * @see self::buildForm() */ protected function bookAdminTable(NodeInterface $node, array &$form) { $form['table'] = array('#type' => 'table', '#header' => [$this->t('Title'), $this->t('Weight'), $this->t('Parent'), $this->t('Operations')], '#empty' => $this->t('No book content available.'), '#tabledrag' => [['action' => 'match', 'relationship' => 'parent', 'group' => 'book-pid', 'subgroup' => 'book-pid', 'source' => 'book-nid', 'hidden' => TRUE, 'limit' => BookManager::BOOK_MAX_DEPTH - 2], ['action' => 'order', 'relationship' => 'sibling', 'group' => 'book-weight']]); $tree = $this->bookManager->bookSubtreeData($node->book); // Do not include the book item itself. $tree = array_shift($tree); if ($tree['below']) { $hash = Crypt::hashBase64(serialize($tree['below'])); // Store the hash value as a hidden form element so that we can detect // if another user changed the book hierarchy. $form['tree_hash'] = array('#type' => 'hidden', '#default_value' => $hash); $form['tree_current_hash'] = array('#type' => 'value', '#value' => $hash); $this->bookAdminTableTree($tree['below'], $form['table']); } }