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