f61aa775a5abd2ee9a42a1307f6e49f44b50b50d
[yaffs-website] / web / core / modules / book / src / Form / BookRemoveForm.php
1 <?php
2
3 namespace Drupal\book\Form;
4
5 use Drupal\book\BookManagerInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\node\NodeInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Remove form for book module.
13  *
14  * @internal
15  */
16 class BookRemoveForm extends ConfirmFormBase {
17
18   /**
19    * The book manager.
20    *
21    * @var \Drupal\book\BookManagerInterface
22    */
23   protected $bookManager;
24
25   /**
26    * The node representing the book.
27    *
28    * @var \Drupal\node\NodeInterface
29    */
30   protected $node;
31
32   /**
33    * Constructs a BookRemoveForm object.
34    *
35    * @param \Drupal\book\BookManagerInterface $book_manager
36    *   The book manager.
37    */
38   public function __construct(BookManagerInterface $book_manager) {
39     $this->bookManager = $book_manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container) {
46     return new static(
47       $container->get('book.manager')
48     );
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function getFormId() {
55     return 'book_remove_form';
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
62     $this->node = $node;
63     return parent::buildForm($form, $form_state);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function getDescription() {
70     $title = ['%title' => $this->node->label()];
71     if ($this->node->book['has_children']) {
72       return $this->t('%title has associated child pages, which will be relocated automatically to maintain their connection to the book. To recreate the hierarchy (as it was before removing this page), %title may be added again using the Outline tab, and each of its former child pages will need to be relocated manually.', $title);
73     }
74     else {
75       return $this->t('%title may be added to hierarchy again using the Outline tab.', $title);
76     }
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function getConfirmText() {
83     return $this->t('Remove');
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function getQuestion() {
90     return $this->t('Are you sure you want to remove %title from the book hierarchy?', ['%title' => $this->node->label()]);
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function getCancelUrl() {
97     return $this->node->urlInfo();
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function submitForm(array &$form, FormStateInterface $form_state) {
104     if ($this->bookManager->checkNodeIsRemovable($this->node)) {
105       $this->bookManager->deleteFromBook($this->node->id());
106       drupal_set_message($this->t('The post has been removed from the book.'));
107     }
108     $form_state->setRedirectUrl($this->getCancelUrl());
109   }
110
111 }