class BookManager implements BookManagerInterface (View source)

Defines a book manager.

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Constants

BOOK_MAX_DEPTH

Defines the maximum supported depth of the book tree.

Properties

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected EntityTypeManagerInterface $entityTypeManager

Entity type manager.

protected ConfigFactoryInterface $configFactory

Config Factory Service Object.

protected array $books

Books Array.

protected BookOutlineStorageInterface $bookOutlineStorage

Book outline storage.

protected array $bookTreeFlattened

Stores flattened book trees.

protected RendererInterface $renderer

The renderer.

protected EntityRepositoryInterface $entityRepository

The entity repository service.

protected LanguageManagerInterface|mixed|null $languageManager

The language manager.

Methods

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, BookOutlineStorageInterface $book_outline_storage, RendererInterface $renderer, LanguageManagerInterface $language_manager = NULL, EntityRepositoryInterface $entity_repository = NULL)

Constructs a BookManager object.

array
getAllBooks()

Returns an array of all books.

loadBooks()

Loads Books Array.

array
getLinkDefaults(string|int $nid)

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

int
getParentDepthLimit(array $book_link)

Finds the depth limit for items in the parent select.

int
findChildrenRelativeDepth(array $book_link)

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

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

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

bool
checkNodeIsRemovable(NodeInterface $node)

Determines if a node can be removed from the book.

bool
updateOutline(NodeInterface $node)

Handles additions and updates to the book outline.

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

No description

array
addParentSelectFormElements(array $book_link)

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

recurseTableOfContents(array $tree, string $indent, array $toc, array $exclude, int $depth_limit)

Recursively processes and formats book links for getTableOfContents().

array
getTableOfContents(int $bid, int $depth_limit, array $exclude = [])

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

deleteFromBook(int $nid)

Deletes node's entry from book table.

array
bookTreeAllData(int $bid, array|null $link = NULL, int|null $max_depth = NULL)

Gets the data structure representing a named menu tree.

array
getActiveTrailIds(string $bid, array $link)

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

array
bookTreeOutput(array $tree)

Returns a rendered menu tree.

array
buildItems(array $tree)

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

array
bookTreeBuild(int $bid, array $parameters = [])

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

array
doBookTreeBuild(int $bid, array $parameters = [])

Builds a book tree.

bookTreeCollectNodeLinks(array $tree, array $node_links)

Collects node links from a given menu tree recursively.

array
bookTreeGetFlat(array $book_link)

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

flatBookTree(array $tree, array $flat)

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

array
loadBookLink(int $nid, bool $translate = TRUE)

Loads a single book entry.

array[]
loadBookLinks(int[] $nids, bool $translate = TRUE)

Loads multiple book entries.

array
saveBookLink(array $link, bool $new)

Saves a single book entry.

moveChildren(array $link, array $original)

Moves children from the original parent to the updated link.

bool
updateParent(array $link)

Sets the has_children flag of the parent of the node.

bool
updateOriginalParent(array $original)

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

setParents(array $link, array $parent)

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

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

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

doBookTreeCheckAccess(array $tree)

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

bookLinkTranslate(array $link)

Provides book loading, access control and translation.

array
buildBookOutlineData(array $links, array $parents = [], int $depth = 1)

Sorts and returns the built data representing a book tree.

array
buildBookOutlineRecursive(array $links, array $parents, int $depth)

Builds the data representing a book tree.

A
bookSubtreeData(array $link)

Gets the data representing a subtree of the book hierarchy.

Details

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $translation, ConfigFactoryInterface $config_factory, BookOutlineStorageInterface $book_outline_storage, RendererInterface $renderer, LanguageManagerInterface $language_manager = NULL, EntityRepositoryInterface $entity_repository = NULL)

Constructs a BookManager object.

Parameters

EntityTypeManagerInterface $entity_type_manager

The entity type manager.

TranslationInterface $translation

The string translation service.

ConfigFactoryInterface $config_factory

The config factory.

BookOutlineStorageInterface $book_outline_storage

The book outline storage.

RendererInterface $renderer

The renderer.

LanguageManagerInterface $language_manager

The language manager.

EntityRepositoryInterface $entity_repository

The entity repository service.

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

Return Value

array

An array of all books.

protected loadBooks()

Loads Books Array.

array getLinkDefaults(string|int $nid)

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

Parameters

string|int $nid

The ID of the node whose menu link is being created.

Return Value

array

The default values for the menu link.

int getParentDepthLimit(array $book_link)

Finds the depth limit for items in the parent select.

Parameters

array $book_link

A fully loaded menu link that is part of the book hierarchy.

Return Value

int

The depth limit for items in the parent select.

protected int findChildrenRelativeDepth(array $book_link)

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

Parameters

array $book_link

The book link.

Return Value

int

The difference between the max depth in the book tree and the depth of the passed book link.

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

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

Parameters

array $form

An associative array containing the structure of the form.

FormStateInterface $form_state

The current state of the form.

NodeInterface $node

The node whose form is being viewed.

AccountInterface $account

The account viewing the form.

bool $collapsed

If TRUE, the fieldset starts out collapsed.

Return Value

array

The form structure, with the book elements added.

bool 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

NodeInterface $node

The node to remove from the outline.

Return Value

bool

TRUE if a node can be removed from the book, FALSE otherwise.

bool 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

NodeInterface $node

The node that is being saved, added, deleted, or moved.

Return Value

bool

TRUE if the book link was saved; FALSE otherwise.

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

No description

Parameters

array $item
array $parent

protected array addParentSelectFormElements(array $book_link)

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_link

A fully loaded book link that is part of the book hierarchy.

Return Value

array

A parent selection form element.

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

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 $tree

The data structure of the book's outline tree. Includes hidden links.

string $indent

A string appended to each node title. Increments by '--' per depth level.

array $toc

Reference to the table of contents array. This is modified in place, so the function does not have a return value.

array $exclude

Optional array of Node ID values. Any link whose node ID is in this array will be excluded (along with its children).

int $depth_limit

Any link deeper than this value will be excluded (along with its children).

array getTableOfContents(int $bid, int $depth_limit, array $exclude = [])

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

Parameters

int $bid

The ID of the book whose pages are to be listed.

int $depth_limit

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

Return Value

array

An array of (menu link ID, title) pairs for use as options for selecting a book page.

deleteFromBook(int $nid)

Deletes node's entry from book table.

Parameters

int $nid

The nid to delete.

array bookTreeAllData(int $bid, array|null $link = NULL, int|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 admin interface or a select.

Note: based on menu_tree_all_data().

Parameters

int $bid

The 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'].

Return Value

array

A tree of menu links in an array, in the order they should be rendered.

array getActiveTrailIds(string $bid, array $link)

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

Parameters

string $bid

The Book ID to find links for.

array $link

A fully loaded menu link.

Return Value

array

An array containing the active trail: a list of mlids.

array 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 $tree

A data structure representing the tree as returned from buildBookOutlineData.

Return Value

array

A structured array to be rendered by \Drupal\Core\Render\RendererInterface::render().

protected array buildItems(array $tree)

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

Helper function for ::bookTreeOutput().

Parameters

array $tree

A data structure representing the tree.

Return Value

array

The value to use for the #items property of a renderable menu.

protected array bookTreeBuild(int $bid, array $parameters = [])

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

Parameters

int $bid

The 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 parent link IDs in this list. If empty, the whole outline is built, unless 'only_active_trail' is TRUE.
  • active_trail: An array of node IDs, representing 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 to build the whole tree for the 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 \Drupal\book\BookOutlineStorage::getBookMenuTree() for the actual query.

Return Value

array

A fully built book tree.

protected array doBookTreeBuild(int $bid, array $parameters = [])

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.

Parameters

int $bid

The 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 parent link IDs in this list. If empty, the whole outline is built, unless 'only_active_trail' is TRUE.
  • active_trail: An array of node IDs, representing 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 to build the whole tree for the 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 \Drupal\book\BookOutlineStorage::getBookMenuTree() for the actual query.

Return Value

array

An array with links representing the tree structure of the book.

See also

BookOutlineStorageInterface::getBookMenuTree

Collects node links from a given menu tree recursively.

Parameters

array $tree

The menu tree you wish to collect node links from.

array $node_links

An array in which to store the collected node links.

array bookTreeGetFlat(array $book_link)

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

Parameters

array $book_link

A fully loaded book link that is part of the book hierarchy.

Return Value

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.

protected flatBookTree(array $tree, array $flat)

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

Parameters

array $tree

A tree of menu links in an array.

array $flat

A flat array of the menu links from $tree, passed by reference.

See also

\Drupal\book\static::bookTreeGetFlat()

Loads a single book entry.

The entries of a book entry is documented in \Drupal\book\BookOutlineStorageInterface::loadMultiple.

If $translate is TRUE, it also checks access ('access' key) and loads the title from the node itself.

Parameters

int $nid

The node ID of the book.

bool $translate

If TRUE, set access, title, and other elements.

Return Value

array

The book data of that node.

Loads multiple book entries.

The entries of a book entry is documented in \Drupal\book\BookOutlineStorageInterface::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 $translate

If TRUE, set access, title, and other elements.

Return Value

array[]

The book data of each node keyed by NID.

Saves a single book entry.

Parameters

array $link

The link data to save.

bool $new

Is this a new book.

Return Value

array

The book data of that node.

protected moveChildren(array $link, array $original)

Moves children from the original parent to the updated link.

Parameters

array $link

The link being saved.

array $original

The original parent of $link.

protected bool updateParent(array $link)

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 $link

The book link, data reflecting its new position, whose new parent we want to update.

Return Value

bool

TRUE if the update was successful (either there is no parent to update, or the parent was updated successfully), FALSE on failure.

protected bool updateOriginalParent(array $original)

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 $original

The original link whose parent we want to update.

Return Value

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.

protected setParents(array $link, array $parent)

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

Parameters

array $link

The book link to update, passed by reference.

array $parent

The parent values to set.

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

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

Parameters

array $tree

The book tree you wish to operate on.

array $node_links

A collection of node link references generated from $tree by menu_tree_collect_node_links().

protected doBookTreeCheckAccess(array $tree)

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

Parameters

array $tree

The book tree to operate on.

bookLinkTranslate(array $link)

Provides book loading, access control and translation.

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

Parameters

array $link

A book link.

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

Sorts and returns the built data representing a book tree.

Parameters

array $links

A 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 $parents

An array of the node ID values that are in the path from the current page to the root of the book tree.

int $depth

The minimum depth to include in the returned book tree.

Return Value

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.

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

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.

Parameters

array $links

A 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 $parents

An array of the node ID values that are in the path from the current page to the root of the book tree.

int $depth

The minimum depth to include in the returned book tree.

Return Value

array

Book tree.

A bookSubtreeData(array $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 $link

A fully loaded book link.

Return Value

A

subtree of book links in an array, in the order they should be rendered.