book_form_node_form_alter

book_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id)

Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.

Adds the book form element to the node form.

See also

book_pick_book_nojs_submit()

File

core/modules/book/book.module, line 136
Allows users to create and organize related content in an outline.

Code

function book_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $node = $form_state->getFormObject()->getEntity();
  $account = \Drupal::currentUser();
  $access = $account->hasPermission('administer book outlines');
  if (!$access) {
    if ($account->hasPermission('add content to books') && ((!empty($node->book['bid']) && !$node->isNew()) || book_type_is_allowed($node->getType()))) {
      // Already in the book hierarchy, or this node type is allowed.
      $access = TRUE;
    }
  }

  if ($access) {
    $collapsed = !($node->isNew() && !empty($node->book['pid']));
    $form = \Drupal::service('book.manager')->addFormElements($form, $form_state, $node, $account, $collapsed);
    // The "js-hide" class hides submit button when Javascript is enabled.
    $form['book']['pick-book'] = array(
      '#type' => 'submit',
      '#value' => t('Change book (update list of parents)'),
      '#submit' => array('book_pick_book_nojs_submit'),
      '#weight' => 20,
      '#attributes' => array(
        'class' => array(
          'js-hide',
        ),
      ),
    );
    $form['#entity_builders'][] = 'book_node_builder';
  }
}
doc_Drupal
2016-10-29 08:48:36
Comments
Leave a Comment

Please login to continue.