function BookManager::addParentSelectFormElements

Same name in other branches
  1. 9 core/modules/book/src/BookManager.php \Drupal\book\BookManager::addParentSelectFormElements()
  2. 10 core/modules/book/src/BookManager.php \Drupal\book\BookManager::addParentSelectFormElements()
  3. 11.x core/modules/book/src/BookManager.php \Drupal\book\BookManager::addParentSelectFormElements()

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.

1 call to BookManager::addParentSelectFormElements()
BookManager::addFormElements in core/modules/book/src/BookManager.php
Builds the common elements of the book form for the node and outline forms.

File

core/modules/book/src/BookManager.php, line 357

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

protected function addParentSelectFormElements(array $book_link) {
    $config = $this->configFactory
        ->get('book.settings');
    if ($config->get('override_parent_selector')) {
        return [];
    }
    // Offer a message or a drop-down to choose a different parent page.
    $form = [
        '#type' => 'hidden',
        '#value' => -1,
        '#prefix' => '<div id="edit-book-plid-wrapper">',
        '#suffix' => '</div>',
    ];
    if ($book_link['nid'] === $book_link['bid']) {
        // This is a book - at the top level.
        if ($book_link['original_bid'] === $book_link['bid']) {
            $form['#prefix'] .= '<em>' . $this->t('This is the top-level page in this book.') . '</em>';
        }
        else {
            $form['#prefix'] .= '<em>' . $this->t('This will be the top-level page in this book.') . '</em>';
        }
    }
    elseif (!$book_link['bid']) {
        $form['#prefix'] .= '<em>' . $this->t('No book selected.') . '</em>';
    }
    else {
        $form = [
            '#type' => 'select',
            '#title' => $this->t('Parent item'),
            '#default_value' => $book_link['pid'],
            '#description' => $this->t('The parent page in the book. The maximum depth for a book and all child pages is @maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', [
                '@maxdepth' => static::BOOK_MAX_DEPTH,
            ]),
            '#options' => $this->getTableOfContents($book_link['bid'], $book_link['parent_depth_limit'], [
                $book_link['nid'],
            ]),
            '#attributes' => [
                'class' => [
                    'book-title-select',
                ],
            ],
            '#prefix' => '<div id="edit-book-plid-wrapper">',
            '#suffix' => '</div>',
        ];
    }
    $this->renderer
        ->addCacheableDependency($form, $config);
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.