3 namespace Drupal\book\Form;
5 use Drupal\book\BookManagerInterface;
6 use Drupal\Component\Datetime\TimeInterface;
7 use Drupal\Core\Entity\ContentEntityForm;
8 use Drupal\Core\Entity\EntityRepositoryInterface;
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
10 use Drupal\Core\Form\FormStateInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Displays the book outline form.
19 class BookOutlineForm extends ContentEntityForm {
22 * The book being displayed.
24 * @var \Drupal\node\NodeInterface
29 * BookManager service.
31 * @var \Drupal\book\BookManagerInterface
33 protected $bookManager;
36 * Constructs a BookOutlineForm object.
38 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
39 * The entity repository.
40 * @param \Drupal\book\BookManagerInterface $book_manager
41 * The BookManager service.
42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
43 * The entity type bundle service.
44 * @param \Drupal\Component\Datetime\TimeInterface $time
47 public function __construct(EntityRepositoryInterface $entity_repository, BookManagerInterface $book_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
48 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
49 $this->bookManager = $book_manager;
55 public static function create(ContainerInterface $container) {
57 $container->get('entity.repository'),
58 $container->get('book.manager'),
59 $container->get('entity_type.bundle.info'),
60 $container->get('datetime.time')
67 public function getBaseFormId() {
74 public function form(array $form, FormStateInterface $form_state) {
75 $form['#title'] = $this->entity->label();
77 if (!isset($this->entity->book)) {
78 // The node is not part of any book yet - set default options.
79 $this->entity->book = $this->bookManager->getLinkDefaults($this->entity->id());
82 $this->entity->book['original_bid'] = $this->entity->book['bid'];
85 // Find the depth limit for the parent select.
86 if (!isset($this->entity->book['parent_depth_limit'])) {
87 $this->entity->book['parent_depth_limit'] = $this->bookManager->getParentDepthLimit($this->entity->book);
89 $form = $this->bookManager->addFormElements($form, $form_state, $this->entity, $this->currentUser(), FALSE);
97 protected function actions(array $form, FormStateInterface $form_state) {
98 $actions = parent::actions($form, $form_state);
99 $actions['submit']['#value'] = $this->entity->book['original_bid'] ? $this->t('Update book outline') : $this->t('Add to book outline');
100 $actions['delete']['#title'] = $this->t('Remove from book outline');
101 $actions['delete']['#url'] = new Url('entity.node.book_remove_form', ['node' => $this->entity->book['nid']]);
102 $actions['delete']['#access'] = $this->bookManager->checkNodeIsRemovable($this->entity);
109 public function save(array $form, FormStateInterface $form_state) {
110 $form_state->setRedirect(
111 'entity.node.canonical',
112 ['node' => $this->entity->id()]
114 $book_link = $form_state->getValue('book');
115 if (!$book_link['bid']) {
116 $this->messenger()->addStatus($this->t('No changes were made'));
120 $this->entity->book = $book_link;
121 if ($this->bookManager->updateOutline($this->entity)) {
122 if (isset($this->entity->book['parent_mismatch']) && $this->entity->book['parent_mismatch']) {
123 // This will usually only happen when JS is disabled.
124 $this->messenger()->addStatus($this->t('The post has been added to the selected book. You may now position it relative to other pages.'));
125 $form_state->setRedirectUrl($this->entity->urlInfo('book-outline-form'));
128 $this->messenger()->addStatus($this->t('The book outline has been updated.'));
132 $this->messenger()->addError($this->t('There was an error adding the post to the book.'));