BookOutlineForm.php in Drupal 9
File
core/modules/book/src/Form/BookOutlineForm.php
View source
<?php
namespace Drupal\book\Form;
use Drupal\book\BookManagerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BookOutlineForm extends ContentEntityForm {
protected $entity;
protected $bookManager;
public function __construct(EntityRepositoryInterface $entity_repository, BookManagerInterface $book_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->bookManager = $book_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('book.manager'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'));
}
public function getBaseFormId() {
return NULL;
}
public function form(array $form, FormStateInterface $form_state) {
$form['#title'] = $this->entity
->label();
if (!isset($this->entity->book)) {
$this->entity->book = $this->bookManager
->getLinkDefaults($this->entity
->id());
}
else {
$this->entity->book['original_bid'] = $this->entity->book['bid'];
}
if (!isset($this->entity->book['parent_depth_limit'])) {
$this->entity->book['parent_depth_limit'] = $this->bookManager
->getParentDepthLimit($this->entity->book);
}
$form = $this->bookManager
->addFormElements($form, $form_state, $this->entity, $this
->currentUser(), FALSE);
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->entity->book['original_bid'] ? $this
->t('Update book outline') : $this
->t('Add to book outline');
$actions['delete']['#title'] = $this
->t('Remove from book outline');
$actions['delete']['#url'] = new Url('entity.node.book_remove_form', [
'node' => $this->entity->book['nid'],
]);
$actions['delete']['#access'] = $this->bookManager
->checkNodeIsRemovable($this->entity);
return $actions;
}
public function save(array $form, FormStateInterface $form_state) {
$form_state
->setRedirect('entity.node.canonical', [
'node' => $this->entity
->id(),
]);
$book_link = $form_state
->getValue('book');
if (!$book_link['bid']) {
$this
->messenger()
->addStatus($this
->t('No changes were made'));
return;
}
$this->entity->book = $book_link;
if ($this->bookManager
->updateOutline($this->entity)) {
if (isset($this->entity->book['parent_mismatch']) && $this->entity->book['parent_mismatch']) {
$this
->messenger()
->addStatus($this
->t('The post has been added to the selected book. You may now position it relative to other pages.'));
$form_state
->setRedirectUrl($this->entity
->toUrl('book-outline-form'));
}
else {
$this
->messenger()
->addStatus($this
->t('The book outline has been updated.'));
}
}
else {
$this
->messenger()
->addError($this
->t('There was an error adding the post to the book.'));
}
}
}