Drupal 8  8.0.2
BookManager Class Reference
Inheritance diagram for BookManager:

Public Member Functions

 __construct (EntityManagerInterface $entity_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, BookOutlineStorageInterface $book_outline_storage, RendererInterface $renderer)
 
 getAllBooks ()
 
 getLinkDefaults ($nid)
 
 getParentDepthLimit (array $book_link)
 
 addFormElements (array $form, FormStateInterface $form_state, NodeInterface $node, AccountInterface $account, $collapsed=TRUE)
 
 checkNodeIsRemovable (NodeInterface $node)
 
 updateOutline (NodeInterface $node)
 
 getBookParents (array $item, array $parent=array())
 
 getTableOfContents ($bid, $depth_limit, array $exclude=array())
 
 deleteFromBook ($nid)
 
 bookTreeAllData ($bid, $link=NULL, $max_depth=NULL)
 
 getActiveTrailIds ($bid, $link)
 
 bookTreeOutput (array $tree)
 
 bookTreeCollectNodeLinks (&$tree, &$node_links)
 
 bookTreeGetFlat (array $book_link)
 
 loadBookLink ($nid, $translate=TRUE)
 
 loadBookLinks ($nids, $translate=TRUE)
 
 saveBookLink (array $link, $new)
 
 bookTreeCheckAccess (&$tree, $node_links=array())
 
 bookLinkTranslate (&$link)
 
 bookSubtreeData ($link)
 

Data Fields

const BOOK_MAX_DEPTH = 9
 

Protected Member Functions

 loadBooks ()
 
 findChildrenRelativeDepth (array $book_link)
 
 addParentSelectFormElements (array $book_link)
 
 recurseTableOfContents (array $tree, $indent, array &$toc, array $exclude, $depth_limit)
 
 buildItems (array $tree)
 
 bookTreeBuild ($bid, array $parameters=array())
 
 doBookTreeBuild ($bid, array $parameters=array())
 
 flatBookTree (array $tree, array &$flat)
 
 moveChildren (array $link, array $original)
 
 updateParent (array $link)
 
 updateOriginalParent (array $original)
 
 setParents (array &$link, array $parent)
 
 doBookTreeCheckAccess (&$tree)
 
 buildBookOutlineData (array $links, array $parents=array(), $depth=1)
 
 buildBookOutlineRecursive (&$links, $parents, $depth)
 

Protected Attributes

 $entityManager
 
 $configFactory
 
 $books
 
 $bookOutlineStorage
 
 $bookTreeFlattened
 
 $renderer
 

Detailed Description

Defines a book manager.

Constructor & Destructor Documentation

__construct ( EntityManagerInterface  $entity_manager,
TranslationInterface  $translation,
ConfigFactoryInterface  $config_factory,
BookOutlineStorageInterface  $book_outline_storage,
RendererInterface  $renderer 
)

Constructs a BookManager object.

References Drupal\configFactory(), and Drupal\entityManager().

Here is the call graph for this function:

Member Function Documentation

addFormElements ( array  $form,
FormStateInterface  $form_state,
NodeInterface  $node,
AccountInterface  $account,
  $collapsed = TRUE 
)

{Builds the common elements of the book form for the node and outline forms.

Parameters
array$formAn associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface$form_stateThe current state of the form.
\Drupal\node\NodeInterface$nodeThe node whose form is being viewed.
\Drupal\Core\Session\AccountInterface$accountThe account viewing the form.
bool$collapsedIf TRUE, the fieldset starts out collapsed.
Returns
array The form structure, with the book elements added.
}

Implements BookManagerInterface.

References BookManager\addParentSelectFormElements(), BookManager\getAllBooks(), FormStateInterface\getValue(), AccountInterface\hasPermission(), FormStateInterface\hasValue(), EntityInterface\id(), EntityInterface\isNew(), EntityInterface\label(), and t().

Here is the call graph for this function:

addParentSelectFormElements ( array  $book_link)
protected

Builds the parent selection form element for the node form or outline tab.

This function is also called when generating a new set of options during the Ajax callback, so an array is returned that can be used to replace an existing form element.

Parameters
array$book_linkA fully loaded book link that is part of the book hierarchy.
Returns
array A parent selection form element.

References Drupal\configFactory(), BookManager\getTableOfContents(), and t().

Referenced by BookManager\addFormElements().

Here is the call graph for this function:

Here is the caller graph for this function:

bookLinkTranslate ( $link)

{Provides book loading, access control and translation.

Parameters
array$linkA book link.

Note: copied from _menu_link_translate() in menu.inc, but reduced to the minimal code that's used.}

Implements BookManagerInterface.

References Drupal\entityManager().

Referenced by BookManager\doBookTreeCheckAccess(), and BookManager\loadBookLinks().

Here is the call graph for this function:

Here is the caller graph for this function:

bookSubtreeData (   $link)

{Gets the data representing a subtree of the book hierarchy.The root of the subtree will be the link passed as a parameter, so the returned tree will contain this item and all its descendants in the menu tree.

Parameters
array$linkA fully loaded book link.
Returns
A subtree of book links in an array, in the order they should be rendered.
}

Implements BookManagerInterface.

References BookManager\bookTreeCheckAccess(), BookManager\bookTreeCollectNodeLinks(), BookManager\buildBookOutlineData(), Drupal\cache(), and Cache\PERMANENT.

Here is the call graph for this function:

bookTreeAllData (   $bid,
  $link = NULL,
  $max_depth = NULL 
)

{Gets the data structure representing a named menu tree.Since this can be the full tree including hidden items, the data returned may be used for generating an an admin interface or a select.

Parameters
int$bidThe Book ID to find links for.
array | null$link(optional) A fully loaded menu link, or NULL. If a link is supplied, only the path to root will be included in the returned tree - as if this link represented the current page in a visible menu.
int | null$max_depth(optional) Maximum depth of links to retrieve. Typically useful if only one or two levels of a sub tree are needed in conjunction with a non-NULL $link, in which case $max_depth should be greater than $link['depth'].
Returns
array An tree of menu links in an array, in the order they should be rendered.

Note: based on menu_tree_all_data().}

Implements BookManagerInterface.

References BookManager\bookTreeBuild(), BookManager\getActiveTrailIds(), and Drupal\languageManager().

Referenced by BookManager\bookTreeGetFlat(), and BookManager\getTableOfContents().

Here is the call graph for this function:

Here is the caller graph for this function:

bookTreeBuild (   $bid,
array  $parameters = array() 
)
protected

Builds a book tree, translates links, and checks access.

Parameters
int$bidThe Book ID to find links for.
array$parameters(optional) An associative array of build parameters. Possible keys:
  • expanded: An array of parent link ids to return only book links that are children of one of the plids in this list. If empty, the whole outline is built, unless 'only_active_trail' is TRUE.
  • active_trail: An array of nids, representing the coordinates of the currently active book link.
  • only_active_trail: Whether to only return links that are in the active trail. This option is ignored, if 'expanded' is non-empty.
  • min_depth: The minimum depth of book links in the resulting tree. Defaults to 1, which is the default to build a whole tree for a book.
  • max_depth: The maximum depth of book links in the resulting tree.
  • conditions: An associative array of custom database select query condition key/value pairs; see _menu_build_tree() for the actual query.
Returns
array A fully built book tree.

References BookManager\bookTreeCheckAccess(), and BookManager\doBookTreeBuild().

Referenced by BookManager\bookTreeAllData().

Here is the call graph for this function:

Here is the caller graph for this function:

bookTreeCheckAccess ( $tree,
  $node_links = array() 
)

{Checks access and performs dynamic operations for each link in the tree.

Parameters
array$treeThe book tree you wish to operate on.
array$node_linksA collection of node link references generated from $tree by menu_tree_collect_node_links().
}

Implements BookManagerInterface.

References BookManager\doBookTreeCheckAccess(), and Drupal\entityQuery().

Referenced by BookManager\bookSubtreeData(), and BookManager\bookTreeBuild().

Here is the call graph for this function:

Here is the caller graph for this function:

bookTreeCollectNodeLinks ( $tree,
$node_links 
)

{Collects node links from a given menu tree recursively.

Parameters
array$treeThe menu tree you wish to collect node links from.
array$node_linksAn array in which to store the collected node links.
}

Implements BookManagerInterface.

Referenced by BookManager\bookSubtreeData(), and BookManager\doBookTreeBuild().

Here is the caller graph for this function:

bookTreeGetFlat ( array  $book_link)

{Gets the book for a page and returns it as a linear array.

Parameters
array$book_linkA fully loaded book link that is part of the book hierarchy.
Returns
array A linear array of book links in the order that the links are shown in the book, so the previous and next pages are the elements before and after the element corresponding to the current node. The children of the current node (if any) will come immediately after it in the array, and links will only be fetched as deep as one level deeper than $book_link.
}

Implements BookManagerInterface.

References BookManager\bookTreeAllData(), and BookManager\flatBookTree().

Here is the call graph for this function:

bookTreeOutput ( array  $tree)

{Returns a rendered menu tree.The menu item's LI element is given one of the following classes:

  • expanded: The menu item is showing its submenu.
  • collapsed: The menu item has a submenu which is not shown.
Parameters
array$treeA data structure representing the tree as returned from buildBookOutlineData.
Returns
array A structured array to be rendered by drupal_render().
See Also
::build
}

Implements BookManagerInterface.

References BookManager\buildItems().

Here is the call graph for this function:

buildBookOutlineData ( array  $links,
array  $parents = array(),
  $depth = 1 
)
protected

Sorts and returns the built data representing a book tree.

Parameters
array$linksA flat array of book links that are part of the book. Each array element is an associative array of information about the book link, containing the fields from the {book} table. This array must be ordered depth-first.
array$parentsAn array of the node ID values that are in the path from the current page to the root of the book tree.
int$depthThe minimum depth to include in the returned book tree.
Returns
array An array of book links in the form of a tree. Each item in the tree is an associative array containing:
  • link: The book link item from $links, with additional element 'in_active_trail' (TRUE if the link ID was in $parents).
  • below: An array containing the sub-tree of this item, where each element is a tree item array with 'link' and 'below' elements. This array will be empty if the book link has no items in its sub-tree having a depth greater than or equal to $depth.

References BookManager\buildBookOutlineRecursive().

Referenced by BookManager\bookSubtreeData(), and BookManager\doBookTreeBuild().

Here is the call graph for this function:

Here is the caller graph for this function:

buildBookOutlineRecursive ( $links,
  $parents,
  $depth 
)
protected

Builds the data representing a book tree.

The function is a bit complex because the rendering of a link depends on the next book link.

Referenced by BookManager\buildBookOutlineData().

Here is the caller graph for this function:

buildItems ( array  $tree)
protected

Builds the #items property for a book tree's renderable array.

Helper function for ::bookTreeOutput().

Parameters
array$treeA data structure representing the tree.
Returns
array The value to use for the #items property of a renderable menu.

References Drupal\entityManager().

Referenced by BookManager\bookTreeOutput().

Here is the call graph for this function:

Here is the caller graph for this function:

checkNodeIsRemovable ( NodeInterface  $node)

{Determines if a node can be removed from the book.A node can be removed from a book if it is actually in a book and it either is not a top-level page or is a top-level page with no children.

Parameters
\Drupal\node\NodeInterface$nodeThe node to remove from the outline.
Returns
bool TRUE if a node can be removed from the book, FALSE otherwise.
}

Implements BookManagerInterface.

References EntityInterface\id().

Here is the call graph for this function:

deleteFromBook (   $nid)

{Deletes node's entry from book table.

Parameters
int$nidThe nid to delete.
}

Implements BookManagerInterface.

References Cache\invalidateTags(), BookManager\loadBookLink(), BookManager\updateOriginalParent(), and BookManager\updateOutline().

Here is the call graph for this function:

doBookTreeBuild (   $bid,
array  $parameters = array() 
)
protected

Builds a book tree.

This function may be used build the data for a menu tree only, for example to further massage the data manually before further processing happens. _menu_tree_check_access() needs to be invoked afterwards.

See Also
menu_build_tree()

References BookManager\bookTreeCollectNodeLinks(), BookManager\buildBookOutlineData(), Drupal\cache(), Drupal\languageManager(), and Cache\PERMANENT.

Referenced by BookManager\bookTreeBuild().

Here is the call graph for this function:

Here is the caller graph for this function:

doBookTreeCheckAccess ( $tree)
protected

Sorts the menu tree and recursively checks access for each item.

References BookManager\bookLinkTranslate().

Referenced by BookManager\bookTreeCheckAccess().

Here is the call graph for this function:

Here is the caller graph for this function:

findChildrenRelativeDepth ( array  $book_link)
protected

Determine the relative depth of the children of a given book link.

Parameters
arrayThe book link.
Returns
int The difference between the max depth in the book tree and the depth of the passed book link.

Referenced by BookManager\getParentDepthLimit().

Here is the caller graph for this function:

flatBookTree ( array  $tree,
array &  $flat 
)
protected

Recursively converts a tree of menu links to a flat array.

Parameters
array$treeA tree of menu links in an array.
array$flatA flat array of the menu links from $tree, passed by reference.
See Also
static::bookTreeGetFlat().

Referenced by BookManager\bookTreeGetFlat().

Here is the caller graph for this function:

getActiveTrailIds (   $bid,
  $link 
)

{Gets the active trail IDs for the specified book at the provided path.

Parameters
string$bidThe Book ID to find links for.
array$linkA fully loaded menu link.
Returns
array An array containing the active trail: a list of mlids.
}

Implements BookManagerInterface.

Referenced by BookManager\bookTreeAllData().

Here is the caller graph for this function:

getAllBooks ( )

{Returns an array of all books.This list may be used for generating a list of all the books, or for building the options for a form select.

Returns
array An array of all books.
}

Implements BookManagerInterface.

References BookManager\loadBooks().

Referenced by BookManager\addFormElements().

Here is the call graph for this function:

Here is the caller graph for this function:

getBookParents ( array  $item,
array  $parent = array() 
)

{}

Implements BookManagerInterface.

Referenced by BookManager\saveBookLink().

Here is the caller graph for this function:

getLinkDefaults (   $nid)

{Returns an array with default values for a book page's menu link.

Parameters
string | int$nidThe ID of the node whose menu link is being created.
Returns
array The default values for the menu link.
}

Implements BookManagerInterface.

Referenced by BookManager\saveBookLink().

Here is the caller graph for this function:

getParentDepthLimit ( array  $book_link)

{Finds the depth limit for items in the parent select.

Parameters
array$book_linkA fully loaded menu link that is part of the book hierarchy.
Returns
int The depth limit for items in the parent select.
}

Implements BookManagerInterface.

References BookManager\findChildrenRelativeDepth().

Here is the call graph for this function:

getTableOfContents (   $bid,
  $depth_limit,
array  $exclude = array() 
)

{Returns an array of book pages in table of contents order.

Parameters
int$bidThe ID of the book whose pages are to be listed.
int$depth_limitAny link deeper than this value will be excluded (along with its children).
array$exclude(optional) An array of menu link ID values. Any link whose menu link ID is in this array will be excluded (along with its children). Defaults to an empty array.
Returns
array An array of (menu link ID, title) pairs for use as options for selecting a book page.
}

Implements BookManagerInterface.

References BookManager\bookTreeAllData(), and BookManager\recurseTableOfContents().

Referenced by BookManager\addParentSelectFormElements().

Here is the call graph for this function:

Here is the caller graph for this function:

loadBookLink (   $nid,
  $translate = TRUE 
)

{Loads a single book entry.The entries of a book entry is documented in ::loadMultiple.If $translate is TRUE, it also checks access ('access' key) and loads the title from the node itself.

Parameters
int$nidThe node ID of the book.
bool$translateIf TRUE, set access, title, and other elements.
Returns
array The book data of that node.
See Also
::loadMultiple
}

Implements BookManagerInterface.

References BookManager\loadBookLinks().

Referenced by BookManager\deleteFromBook(), and BookManager\saveBookLink().

Here is the call graph for this function:

Here is the caller graph for this function:

loadBookLinks (   $nids,
  $translate = TRUE 
)

{Loads multiple book entries.The entries of a book entry is documented in ::loadMultiple.If $translate is TRUE, it also checks access ('access' key) and loads the title from the node itself.

Parameters
int[]$nids An array of nids to load.
bool$translateIf TRUE, set access, title, and other elements.
Returns
array[] The book data of each node keyed by NID.
See Also
::loadMultiple
}

Implements BookManagerInterface.

References BookManager\bookLinkTranslate().

Referenced by BookManager\loadBookLink().

Here is the call graph for this function:

Here is the caller graph for this function:

loadBooks ( )
protected

Loads Books Array.

References Drupal\entityManager().

Referenced by BookManager\getAllBooks().

Here is the call graph for this function:

Here is the caller graph for this function:

moveChildren ( array  $link,
array  $original 
)
protected

Moves children from the original parent to the updated link.

Parameters
array$linkThe link being saved.
array$originalThe original parent of $link.

Referenced by BookManager\saveBookLink().

Here is the caller graph for this function:

recurseTableOfContents ( array  $tree,
  $indent,
array &  $toc,
array  $exclude,
  $depth_limit 
)
protected

Recursively processes and formats book links for getTableOfContents().

This helper function recursively modifies the table of contents array for each item in the book tree, ignoring items in the exclude array or at a depth greater than the limit. Truncates titles over thirty characters and appends an indentation string incremented by depth.

Parameters
array$treeThe data structure of the book's outline tree. Includes hidden links.
string$indentA string appended to each node title. Increments by '–' per depth level.
array$tocReference to the table of contents array. This is modified in place, so the function does not have a return value.
array$excludeOptional array of Node ID values. Any link whose node ID is in this array will be excluded (along with its children).
int$depth_limitAny link deeper than this value will be excluded (along with its children).

References Drupal\entityManager(), and Unicode\truncate().

Referenced by BookManager\getTableOfContents().

Here is the call graph for this function:

Here is the caller graph for this function:

saveBookLink ( array  $link,
  $new 
)

{Saves a single book entry.

Parameters
array$linkThe link data to save.
bool$newIs this a new book.
Returns
array The book data of that node.
}

Implements BookManagerInterface.

References BookManager\getBookParents(), BookManager\getLinkDefaults(), Cache\invalidateTags(), BookManager\loadBookLink(), BookManager\moveChildren(), BookManager\setParents(), BookManager\updateOriginalParent(), and BookManager\updateParent().

Referenced by BookManager\updateOutline().

Here is the call graph for this function:

Here is the caller graph for this function:

setParents ( array &  $link,
array  $parent 
)
protected

Sets the p1 through p9 properties for a book link being saved.

Parameters
array$linkThe book link to update.
array$parentThe parent values to set.

Referenced by BookManager\saveBookLink().

Here is the caller graph for this function:

updateOriginalParent ( array  $original)
protected

Updates the has_children flag of the parent of the original node.

This method is called when a book link is moved or deleted. So we want to update the has_children flag of the parent node.

Parameters
array$originalThe original link whose parent we want to update.
Returns
bool TRUE if the update was successful (either there was no original parent to update, or the original parent was updated successfully), FALSE on failure.

Referenced by BookManager\deleteFromBook(), and BookManager\saveBookLink().

Here is the caller graph for this function:

updateOutline ( NodeInterface  $node)

{Handles additions and updates to the book outline.This common helper function performs all additions and updates to the book outline through node addition, node editing, node deletion, or the outline tab.

Parameters
\Drupal\node\NodeInterface$nodeThe node that is being saved, added, deleted, or moved.
Returns
bool TRUE if the book link was saved; FALSE otherwise.
}

Implements BookManagerInterface.

References EntityInterface\id(), and BookManager\saveBookLink().

Referenced by BookManager\deleteFromBook().

Here is the call graph for this function:

Here is the caller graph for this function:

updateParent ( array  $link)
protected

Sets the has_children flag of the parent of the node.

This method is mostly called when a book link is moved/created etc. So we want to update the has_children flag of the new parent book link.

Parameters
array$linkThe book link, data reflecting its new position, whose new parent we want to update.
Returns
bool TRUE if the update was successful (either there is no parent to update, or the parent was updated successfully), FALSE on failure.

Referenced by BookManager\saveBookLink().

Here is the caller graph for this function:

Field Documentation

const BOOK_MAX_DEPTH = 9

Defines the maximum supported depth of the book tree.

Referenced by BookAdminEditForm\bookAdminTable().


The documentation for this class was generated from the following file: