fdee13a6e897ff064aa7bdff677061cbd43204da
[yaffs-website] / src / Form / ForumForm.php
1 <?php
2
3 namespace Drupal\forum\Form;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Url;
7 use Drupal\taxonomy\TermForm;
8
9 /**
10  * Base form for forum term edit forms.
11  *
12  * @internal
13  */
14 class ForumForm extends TermForm {
15
16   /**
17    * Reusable type field to use in status messages.
18    *
19    * @var string
20    */
21   protected $forumFormType;
22
23   /**
24    * Reusable url stub to use in watchdog messages.
25    *
26    * @var string
27    */
28   protected $urlStub = 'forum';
29
30   /**
31    * {@inheritdoc}
32    */
33   public function form(array $form, FormStateInterface $form_state) {
34     // Build the bulk of the form from the parent taxonomy term form.
35     $form = parent::form($form, $form_state);
36
37     // Set the title and description of the name field.
38     $form['name']['#title'] = $this->t('Forum name');
39     $form['name']['#description'] = $this->t('Short but meaningful name for this collection of threaded discussions.');
40
41     // Change the description.
42     $form['description']['#description'] = $this->t('Description and guidelines for discussions within this forum.');
43
44     // Re-use the weight field.
45     $form['weight'] = $form['relations']['weight'];
46
47     // Remove the remaining relations fields.
48     unset($form['relations']);
49
50     // Our parent field is different to the taxonomy term.
51     $form['parent']['#tree'] = TRUE;
52     $form['parent'][0] = $this->forumParentSelect($this->entity->id(), $this->t('Parent'));
53
54     $form['#theme_wrappers'] = ['form__forum'];
55     $this->forumFormType = $this->t('forum');
56     return $form;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function buildEntity(array $form, FormStateInterface $form_state) {
63     $term = parent::buildEntity($form, $form_state);
64
65     // Assign parents from forum parent select field.
66     $term->parent = [$form_state->getValue(['parent', 0])];
67
68     return $term;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function save(array $form, FormStateInterface $form_state) {
75     $term = $this->entity;
76     $term_storage = $this->entityManager->getStorage('taxonomy_term');
77     $status = $term_storage->save($term);
78
79     $route_name = $this->urlStub == 'container' ? 'entity.taxonomy_term.forum_edit_container_form' : 'entity.taxonomy_term.forum_edit_form';
80     $route_parameters = ['taxonomy_term' => $term->id()];
81     $link = $this->l($this->t('Edit'), new Url($route_name, $route_parameters));
82     $view_link = $term->link($term->getName());
83     switch ($status) {
84       case SAVED_NEW:
85         $this->messenger()->addStatus($this->t('Created new @type %term.', ['%term' => $view_link, '@type' => $this->forumFormType]));
86         $this->logger('forum')->notice('Created new @type %term.', ['%term' => $term->getName(), '@type' => $this->forumFormType, 'link' => $link]);
87         $form_state->setValue('tid', $term->id());
88         break;
89
90       case SAVED_UPDATED:
91         $this->messenger()->addStatus($this->t('The @type %term has been updated.', ['%term' => $term->getName(), '@type' => $this->forumFormType]));
92         $this->logger('forum')->notice('Updated @type %term.', ['%term' => $term->getName(), '@type' => $this->forumFormType, 'link' => $link]);
93         break;
94     }
95
96     $form_state->setRedirect('forum.overview');
97     return $term;
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   protected function actions(array $form, FormStateInterface $form_state) {
104     $actions = parent::actions($form, $form_state);
105
106     if (!$this->entity->isNew() && $this->entity->hasLinkTemplate('forum-delete-form')) {
107       $actions['delete']['#url'] = $this->entity->urlInfo('forum-delete-form');
108     }
109     else {
110       unset($actions['delete']);
111     }
112
113     return $actions;
114   }
115
116   /**
117    * Returns a select box for available parent terms.
118    *
119    * @param int $tid
120    *   ID of the term that is being added or edited.
121    * @param string $title
122    *   Title for the select box.
123    *
124    * @return array
125    *   A select form element.
126    */
127   protected function forumParentSelect($tid, $title) {
128     $taxonomy_storage = $this->entityManager->getStorage('taxonomy_term');
129     $parents = $taxonomy_storage->loadParents($tid);
130     if ($parents) {
131       $parent = array_shift($parents);
132       $parent = $parent->id();
133     }
134     else {
135       $parent = 0;
136     }
137
138     $vid = $this->config('forum.settings')->get('vocabulary');
139     $children = $taxonomy_storage->loadTree($vid, $tid, NULL, TRUE);
140
141     // A term can't be the child of itself, nor of its children.
142     foreach ($children as $child) {
143       $exclude[] = $child->tid;
144     }
145     $exclude[] = $tid;
146
147     $tree = $taxonomy_storage->loadTree($vid, 0, NULL, TRUE);
148     $options[0] = '<' . $this->t('root') . '>';
149     if ($tree) {
150       foreach ($tree as $term) {
151         if (!in_array($term->id(), $exclude)) {
152           $options[$term->id()] = str_repeat(' -- ', $term->depth) . $term->getName();
153         }
154       }
155     }
156
157     $description = $this->t('Forums may be placed at the top (root) level, or inside another container or forum.');
158
159     return [
160       '#type' => 'select',
161       '#title' => $title,
162       '#default_value' => $parent,
163       '#options' => $options,
164       '#description' => $description,
165       '#required' => TRUE,
166     ];
167   }
168
169 }