3 namespace Drupal\forum\Form;
5 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\taxonomy\TermForm;
10 * Base form for forum term edit forms.
14 class ForumForm extends TermForm {
17 * Reusable type field to use in status messages.
21 protected $forumFormType;
24 * Reusable url stub to use in watchdog messages.
28 protected $urlStub = 'forum';
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);
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.');
41 // Change the description.
42 $form['description']['#description'] = $this->t('Description and guidelines for discussions within this forum.');
44 // Re-use the weight field.
45 $form['weight'] = $form['relations']['weight'];
47 // Remove the remaining relations fields.
48 unset($form['relations']);
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'));
54 $form['#theme_wrappers'] = ['form__forum'];
55 $this->forumFormType = $this->t('forum');
62 public function buildEntity(array $form, FormStateInterface $form_state) {
63 $term = parent::buildEntity($form, $form_state);
65 // Assign parents from forum parent select field.
66 $term->parent = [$form_state->getValue(['parent', 0])];
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);
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());
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());
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]);
96 $form_state->setRedirect('forum.overview');
103 protected function actions(array $form, FormStateInterface $form_state) {
104 $actions = parent::actions($form, $form_state);
106 if (!$this->entity->isNew() && $this->entity->hasLinkTemplate('forum-delete-form')) {
107 $actions['delete']['#url'] = $this->entity->urlInfo('forum-delete-form');
110 unset($actions['delete']);
117 * Returns a select box for available parent terms.
120 * ID of the term that is being added or edited.
121 * @param string $title
122 * Title for the select box.
125 * A select form element.
127 protected function forumParentSelect($tid, $title) {
128 $taxonomy_storage = $this->entityManager->getStorage('taxonomy_term');
129 $parents = $taxonomy_storage->loadParents($tid);
131 $parent = array_shift($parents);
132 $parent = $parent->id();
138 $vid = $this->config('forum.settings')->get('vocabulary');
139 $children = $taxonomy_storage->loadTree($vid, $tid, NULL, TRUE);
141 // A term can't be the child of itself, nor of its children.
142 foreach ($children as $child) {
143 $exclude[] = $child->tid;
147 $tree = $taxonomy_storage->loadTree($vid, 0, NULL, TRUE);
148 $options[0] = '<' . $this->t('root') . '>';
150 foreach ($tree as $term) {
151 if (!in_array($term->id(), $exclude)) {
152 $options[$term->id()] = str_repeat(' -- ', $term->depth) . $term->getName();
157 $description = $this->t('Forums may be placed at the top (root) level, or inside another container or forum.');
162 '#default_value' => $parent,
163 '#options' => $options,
164 '#description' => $description,