You are here

public function BookManager::addFormElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/book/src/BookManager.php \Drupal\book\BookManager::addFormElements()

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.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

\Drupal\node\NodeInterface $node: The node whose form is being viewed.

\Drupal\Core\Session\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.

Overrides BookManagerInterface::addFormElements

File

core/modules/book/src/BookManager.php, line 161
Contains \Drupal\book\BookManager.

Class

BookManager
Defines a book manager.

Namespace

Drupal\book

Code

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

  // If the form is being processed during the Ajax callback of our book bid
  // dropdown, then $form_state will hold the value that was selected.
  if ($form_state
    ->hasValue('book')) {
    $node->book = $form_state
      ->getValue('book');
  }
  $form['book'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Book outline'),
    '#weight' => 10,
    '#open' => !$collapsed,
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'book-outline-form',
      ),
    ),
    '#attached' => array(
      'library' => array(
        'book/drupal.book',
      ),
    ),
    '#tree' => TRUE,
  );
  foreach (array(
    'nid',
    'has_children',
    'original_bid',
    'parent_depth_limit',
  ) as $key) {
    $form['book'][$key] = array(
      '#type' => 'value',
      '#value' => $node->book[$key],
    );
  }
  $form['book']['pid'] = $this
    ->addParentSelectFormElements($node->book);

  // @see \Drupal\book\Form\BookAdminEditForm::bookAdminTableTree(). The
  // weight may be larger than 15.
  $form['book']['weight'] = array(
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight'),
    '#default_value' => $node->book['weight'],
    '#delta' => max(15, abs($node->book['weight'])),
    '#weight' => 5,
    '#description' => $this
      ->t('Pages at a given level are ordered first by weight and then by title.'),
  );
  $options = array();
  $nid = !$node
    ->isNew() ? $node
    ->id() : 'new';
  if ($node
    ->id() && $nid == $node->book['original_bid'] && $node->book['parent_depth_limit'] == 0) {

    // This is the top level node in a maximum depth book and thus cannot be
    // moved.
    $options[$node
      ->id()] = $node
      ->label();
  }
  else {
    foreach ($this
      ->getAllBooks() as $book) {
      $options[$book['nid']] = $book['title'];
    }
  }
  if ($account
    ->hasPermission('create new books') && ($nid == 'new' || $nid != $node->book['original_bid'])) {

    // The node can become a new book, if it is not one already.
    $options = array(
      $nid => $this
        ->t('- Create a new book -'),
    ) + $options;
  }
  if (!$node->book['bid']) {

    // The node is not currently in the hierarchy.
    $options = array(
      0 => $this
        ->t('- None -'),
    ) + $options;
  }

  // Add a drop-down to select the destination book.
  $form['book']['bid'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Book'),
    '#default_value' => $node->book['bid'],
    '#options' => $options,
    '#access' => (bool) $options,
    '#description' => $this
      ->t('Your page will be a part of the selected book.'),
    '#weight' => -5,
    '#attributes' => array(
      'class' => array(
        'book-title-select',
      ),
    ),
    '#ajax' => array(
      'callback' => 'book_form_update',
      'wrapper' => 'edit-book-plid-wrapper',
      'effect' => 'fade',
      'speed' => 'fast',
    ),
  );
  return $form;
}