a305681d5349622ebf0ddaa14fb7c72a05e1d8ae
[yaffs-website] / web / core / modules / book / src / Form / BookOutlineForm.php
1 <?php
2
3 namespace Drupal\book\Form;
4
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;
11 use Drupal\Core\Url;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Displays the book outline form.
16  *
17  * @internal
18  */
19 class BookOutlineForm extends ContentEntityForm {
20
21   /**
22    * The book being displayed.
23    *
24    * @var \Drupal\node\NodeInterface
25    */
26   protected $entity;
27
28   /**
29    * BookManager service.
30    *
31    * @var \Drupal\book\BookManagerInterface
32    */
33   protected $bookManager;
34
35   /**
36    * Constructs a BookOutlineForm object.
37    *
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
45    *   The time service.
46    */
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;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function create(ContainerInterface $container) {
56     return new static(
57       $container->get('entity.repository'),
58       $container->get('book.manager'),
59       $container->get('entity_type.bundle.info'),
60       $container->get('datetime.time')
61     );
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getBaseFormId() {
68     return NULL;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function form(array $form, FormStateInterface $form_state) {
75     $form['#title'] = $this->entity->label();
76
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());
80     }
81     else {
82       $this->entity->book['original_bid'] = $this->entity->book['bid'];
83     }
84
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);
88     }
89     $form = $this->bookManager->addFormElements($form, $form_state, $this->entity, $this->currentUser(), FALSE);
90
91     return $form;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
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);
103     return $actions;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function save(array $form, FormStateInterface $form_state) {
110     $form_state->setRedirect(
111       'entity.node.canonical',
112       ['node' => $this->entity->id()]
113     );
114     $book_link = $form_state->getValue('book');
115     if (!$book_link['bid']) {
116       $this->messenger()->addStatus($this->t('No changes were made'));
117       return;
118     }
119
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'));
126       }
127       else {
128         $this->messenger()->addStatus($this->t('The book outline has been updated.'));
129       }
130     }
131     else {
132       $this->messenger()->addError($this->t('There was an error adding the post to the book.'));
133     }
134   }
135
136 }