class BookAdminEditForm in Drupal 10
Same name and namespace in other branches
- 8 core/modules/book/src/Form/BookAdminEditForm.php \Drupal\book\Form\BookAdminEditForm
- 9 core/modules/book/src/Form/BookAdminEditForm.php \Drupal\book\Form\BookAdminEditForm
Provides a form for administering a single book's hierarchy.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\book\Form\BookAdminEditForm
Expanded class hierarchy of BookAdminEditForm
1 string reference to 'BookAdminEditForm'
- book.routing.yml in core/
modules/ book/ book.routing.yml - core/modules/book/book.routing.yml
File
- core/
modules/ book/ src/ Form/ BookAdminEditForm.php, line 22
Namespace
Drupal\book\FormView source
class BookAdminEditForm extends FormBase {
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* Constructs a new BookAdminEditForm.
*
* @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
* The custom block storage.
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
*/
public function __construct(EntityStorageInterface $node_storage, BookManagerInterface $book_manager, EntityRepositoryInterface $entity_repository) {
$this->nodeStorage = $node_storage;
$this->bookManager = $book_manager;
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type_manager
->getStorage('node'), $container
->get('book.manager'), $container
->get('entity.repository'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'book_admin_edit';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
$form['#title'] = $node
->label();
$form['#node'] = $node;
$this
->bookAdminTable($node, $form);
$form['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save book pages'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('tree_hash') != $form_state
->getValue('tree_current_hash')) {
$form_state
->setErrorByName('', $this
->t('This book has been modified by another user, the changes could not be saved.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Save elements in the same order as defined in post rather than the form.
// This ensures parents are updated before their children, preventing orphans.
$user_input = $form_state
->getUserInput();
if (isset($user_input['table'])) {
$order = array_flip(array_keys($user_input['table']));
$form['table'] = array_merge($order, $form['table']);
foreach (Element::children($form['table']) as $key) {
if ($form['table'][$key]['#item']) {
$row = $form['table'][$key];
$values = $form_state
->getValue([
'table',
$key,
]);
// Update menu item if moved.
if ($row['parent']['pid']['#default_value'] != $values['pid'] || $row['weight']['#default_value'] != $values['weight']) {
$link = $this->bookManager
->loadBookLink($values['nid'], FALSE);
$link['weight'] = $values['weight'];
$link['pid'] = $values['pid'];
$this->bookManager
->saveBookLink($link, FALSE);
}
// Update the title if changed.
if ($row['title']['#default_value'] != $values['title']) {
$node = $this->nodeStorage
->load($values['nid']);
$node = $this->entityRepository
->getTranslationFromContext($node);
$node->revision_log = $this
->t('Title changed from %original to %current.', [
'%original' => $node
->label(),
'%current' => $values['title'],
]);
$node->title = $values['title'];
$node->book['link_title'] = $values['title'];
$node
->setNewRevision();
$node
->save();
$this
->logger('content')
->notice('book: updated %title.', [
'%title' => $node
->label(),
'link' => $node
->toLink($this
->t('View'))
->toString(),
]);
}
}
}
}
$this
->messenger()
->addStatus($this
->t('Updated book %title.', [
'%title' => $form['#node']
->label(),
]));
}
/**
* Builds the table portion of the form for the book administration page.
*
* @param \Drupal\node\NodeInterface $node
* The node of the top-level page in the book.
* @param array $form
* The form that is being modified, passed by reference.
*
* @see self::buildForm()
*/
protected function bookAdminTable(NodeInterface $node, array &$form) {
$form['table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Title'),
$this
->t('Weight'),
$this
->t('Parent'),
$this
->t('Operations'),
],
'#empty' => $this
->t('No book content available.'),
'#tabledrag' => [
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'book-pid',
'subgroup' => 'book-pid',
'source' => 'book-nid',
'hidden' => TRUE,
'limit' => BookManager::BOOK_MAX_DEPTH - 2,
],
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'book-weight',
],
],
];
$tree = $this->bookManager
->bookSubtreeData($node->book);
// Do not include the book item itself.
$tree = array_shift($tree);
if ($tree['below']) {
$hash = Crypt::hashBase64(serialize($tree['below']));
// Store the hash value as a hidden form element so that we can detect
// if another user changed the book hierarchy.
$form['tree_hash'] = [
'#type' => 'hidden',
'#default_value' => $hash,
];
$form['tree_current_hash'] = [
'#type' => 'value',
'#value' => $hash,
];
$this
->bookAdminTableTree($tree['below'], $form['table']);
}
}
/**
* Helps build the main table in the book administration page form.
*
* @param array $tree
* A subtree of the book menu hierarchy.
* @param array $form
* The form that is being modified, passed by reference.
*
* @see self::buildForm()
*/
protected function bookAdminTableTree(array $tree, array &$form) {
// The delta must be big enough to give each node a distinct value.
$count = count($tree);
$delta = $count < 30 ? 15 : intval($count / 2) + 1;
$access = \Drupal::currentUser()
->hasPermission('administer nodes');
$destination = $this
->getDestinationArray();
foreach ($tree as $data) {
$nid = $data['link']['nid'];
$id = 'book-admin-' . $nid;
$form[$id]['#item'] = $data['link'];
$form[$id]['#nid'] = $nid;
$form[$id]['#attributes']['class'][] = 'draggable';
$form[$id]['#weight'] = $data['link']['weight'];
if (isset($data['link']['depth']) && $data['link']['depth'] > 2) {
$indentation = [
'#theme' => 'indentation',
'#size' => $data['link']['depth'] - 2,
];
}
$form[$id]['title'] = [
'#prefix' => !empty($indentation) ? \Drupal::service('renderer')
->render($indentation) : '',
'#type' => 'textfield',
'#default_value' => $data['link']['title'],
'#maxlength' => 255,
'#size' => 40,
];
$form[$id]['weight'] = [
'#type' => 'weight',
'#default_value' => $data['link']['weight'],
'#delta' => max($delta, abs($data['link']['weight'])),
'#title' => $this
->t('Weight for @title', [
'@title' => $data['link']['title'],
]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => [
'book-weight',
],
],
];
$form[$id]['parent']['nid'] = [
'#parents' => [
'table',
$id,
'nid',
],
'#type' => 'hidden',
'#value' => $nid,
'#attributes' => [
'class' => [
'book-nid',
],
],
];
$form[$id]['parent']['pid'] = [
'#parents' => [
'table',
$id,
'pid',
],
'#type' => 'hidden',
'#default_value' => $data['link']['pid'],
'#attributes' => [
'class' => [
'book-pid',
],
],
];
$form[$id]['parent']['bid'] = [
'#parents' => [
'table',
$id,
'bid',
],
'#type' => 'hidden',
'#default_value' => $data['link']['bid'],
'#attributes' => [
'class' => [
'book-bid',
],
],
];
$form[$id]['operations'] = [
'#type' => 'operations',
];
$form[$id]['operations']['#links']['view'] = [
'title' => $this
->t('View'),
'url' => new Url('entity.node.canonical', [
'node' => $nid,
]),
];
if ($access) {
$form[$id]['operations']['#links']['edit'] = [
'title' => $this
->t('Edit'),
'url' => new Url('entity.node.edit_form', [
'node' => $nid,
]),
'query' => $destination,
];
$form[$id]['operations']['#links']['delete'] = [
'title' => $this
->t('Delete'),
'url' => new Url('entity.node.delete_form', [
'node' => $nid,
]),
'query' => $destination,
];
}
if ($data['below']) {
$this
->bookAdminTableTree($data['below'], $form);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BookAdminEditForm:: |
protected | property | The book manager. | |
BookAdminEditForm:: |
protected | property | The entity repository service. | |
BookAdminEditForm:: |
protected | property | The node storage. | |
BookAdminEditForm:: |
protected | function | Builds the table portion of the form for the book administration page. | |
BookAdminEditForm:: |
protected | function | Helps build the main table in the book administration page form. | |
BookAdminEditForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
BookAdminEditForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
BookAdminEditForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
BookAdminEditForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
BookAdminEditForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
BookAdminEditForm:: |
public | function | Constructs a new BookAdminEditForm. | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 18 |
MessengerTrait:: |
public | function | Gets the messenger. | 18 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |