function book_form_node_form_alter in Drupal 9
Same name and namespace in other branches
- 8 core/modules/book/book.module \book_form_node_form_alter()
- 7 modules/book/book.module \book_form_node_form_alter()
Implements hook_form_BASE_FORM_ID_alter() for \Drupal\node\NodeForm.
Adds the book form element to the node form.
See also
File
- core/
modules/ book/ book.module, line 137 - 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'] = [
'#type' => 'submit',
'#value' => t('Change book (update list of parents)'),
'#submit' => [
'book_pick_book_nojs_submit',
],
'#weight' => 20,
'#attributes' => [
'class' => [
'js-hide',
],
],
];
$form['#entity_builders'][] = 'book_node_builder';
}
}