3 namespace Drupal\forum\Form;
5 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\taxonomy\TermForm;
10 * Base form for forum term edit forms.
12 class ForumForm extends TermForm {
15 * Reusable type field to use in status messages.
19 protected $forumFormType;
22 * Reusable url stub to use in watchdog messages.
26 protected $urlStub = 'forum';
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);
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.');
40 // Change the description.
41 $form['description']['#description'] = $this->t('Description and guidelines for discussions within this forum.');
43 // Re-use the weight field.
44 $form['weight'] = $form['relations']['weight'];
46 // Remove the remaining relations fields.
47 unset($form['relations']);
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'));
53 $form['#theme_wrappers'] = ['form__forum'];
54 $this->forumFormType = $this->t('forum');
61 public function buildEntity(array $form, FormStateInterface $form_state) {
62 $term = parent::buildEntity($form, $form_state);
64 // Assign parents from forum parent select field.
65 $term->parent = [$form_state->getValue(['parent', 0])];
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);
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());
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());
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]);
95 $form_state->setRedirect('forum.overview');
102 protected function actions(array $form, FormStateInterface $form_state) {
103 $actions = parent::actions($form, $form_state);
105 if (!$this->entity->isNew() && $this->entity->hasLinkTemplate('forum-delete-form')) {
106 $actions['delete']['#url'] = $this->entity->urlInfo('forum-delete-form');
109 unset($actions['delete']);
116 * Returns a select box for available parent terms.
119 * ID of the term that is being added or edited.
120 * @param string $title
121 * Title for the select box.
124 * A select form element.
126 protected function forumParentSelect($tid, $title) {
127 $taxonomy_storage = $this->entityManager->getStorage('taxonomy_term');
128 $parents = $taxonomy_storage->loadParents($tid);
130 $parent = array_shift($parents);
131 $parent = $parent->id();
137 $vid = $this->config('forum.settings')->get('vocabulary');
138 $children = $taxonomy_storage->loadTree($vid, $tid, NULL, TRUE);
140 // A term can't be the child of itself, nor of its children.
141 foreach ($children as $child) {
142 $exclude[] = $child->tid;
146 $tree = $taxonomy_storage->loadTree($vid, 0, NULL, TRUE);
147 $options[0] = '<' . $this->t('root') . '>';
149 foreach ($tree as $term) {
150 if (!in_array($term->id(), $exclude)) {
151 $options[$term->id()] = str_repeat(' -- ', $term->depth) . $term->getName();
156 $description = $this->t('Forums may be placed at the top (root) level, or inside another container or forum.');
161 '#default_value' => $parent,
162 '#options' => $options,
163 '#description' => $description,