Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\taxonomy;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\ContentEntityForm;
|
Chris@0
|
6 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Base for handler for taxonomy term edit forms.
|
Chris@14
|
10 *
|
Chris@14
|
11 * @internal
|
Chris@0
|
12 */
|
Chris@0
|
13 class TermForm extends ContentEntityForm {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * {@inheritdoc}
|
Chris@0
|
17 */
|
Chris@0
|
18 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
19 $term = $this->entity;
|
Chris@0
|
20 $vocab_storage = $this->entityManager->getStorage('taxonomy_vocabulary');
|
Chris@0
|
21 $taxonomy_storage = $this->entityManager->getStorage('taxonomy_term');
|
Chris@0
|
22 $vocabulary = $vocab_storage->load($term->bundle());
|
Chris@0
|
23
|
Chris@0
|
24 $parent = array_keys($taxonomy_storage->loadParents($term->id()));
|
Chris@0
|
25 $form_state->set(['taxonomy', 'parent'], $parent);
|
Chris@0
|
26 $form_state->set(['taxonomy', 'vocabulary'], $vocabulary);
|
Chris@0
|
27
|
Chris@0
|
28 $form['relations'] = [
|
Chris@0
|
29 '#type' => 'details',
|
Chris@0
|
30 '#title' => $this->t('Relations'),
|
Chris@0
|
31 '#open' => $vocabulary->getHierarchy() == VocabularyInterface::HIERARCHY_MULTIPLE,
|
Chris@0
|
32 '#weight' => 10,
|
Chris@0
|
33 ];
|
Chris@0
|
34
|
Chris@0
|
35 // \Drupal\taxonomy\TermStorageInterface::loadTree() and
|
Chris@0
|
36 // \Drupal\taxonomy\TermStorageInterface::loadParents() may contain large
|
Chris@0
|
37 // numbers of items so we check for taxonomy.settings:override_selector
|
Chris@0
|
38 // before loading the full vocabulary. Contrib modules can then intercept
|
Chris@0
|
39 // before hook_form_alter to provide scalable alternatives.
|
Chris@0
|
40 if (!$this->config('taxonomy.settings')->get('override_selector')) {
|
Chris@14
|
41 $exclude = [];
|
Chris@14
|
42 if (!$term->isNew()) {
|
Chris@14
|
43 $parent = array_keys($taxonomy_storage->loadParents($term->id()));
|
Chris@14
|
44 $children = $taxonomy_storage->loadTree($vocabulary->id(), $term->id());
|
Chris@0
|
45
|
Chris@14
|
46 // A term can't be the child of itself, nor of its children.
|
Chris@14
|
47 foreach ($children as $child) {
|
Chris@14
|
48 $exclude[] = $child->tid;
|
Chris@14
|
49 }
|
Chris@14
|
50 $exclude[] = $term->id();
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 $tree = $taxonomy_storage->loadTree($vocabulary->id());
|
Chris@0
|
54 $options = ['<' . $this->t('root') . '>'];
|
Chris@0
|
55 if (empty($parent)) {
|
Chris@0
|
56 $parent = [0];
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 foreach ($tree as $item) {
|
Chris@0
|
60 if (!in_array($item->tid, $exclude)) {
|
Chris@0
|
61 $options[$item->tid] = str_repeat('-', $item->depth) . $item->name;
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 $form['relations']['parent'] = [
|
Chris@0
|
66 '#type' => 'select',
|
Chris@0
|
67 '#title' => $this->t('Parent terms'),
|
Chris@0
|
68 '#options' => $options,
|
Chris@0
|
69 '#default_value' => $parent,
|
Chris@0
|
70 '#multiple' => TRUE,
|
Chris@0
|
71 ];
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 $form['relations']['weight'] = [
|
Chris@0
|
75 '#type' => 'textfield',
|
Chris@0
|
76 '#title' => $this->t('Weight'),
|
Chris@0
|
77 '#size' => 6,
|
Chris@0
|
78 '#default_value' => $term->getWeight(),
|
Chris@0
|
79 '#description' => $this->t('Terms are displayed in ascending order by weight.'),
|
Chris@0
|
80 '#required' => TRUE,
|
Chris@0
|
81 ];
|
Chris@0
|
82
|
Chris@0
|
83 $form['vid'] = [
|
Chris@0
|
84 '#type' => 'value',
|
Chris@0
|
85 '#value' => $vocabulary->id(),
|
Chris@0
|
86 ];
|
Chris@0
|
87
|
Chris@0
|
88 $form['tid'] = [
|
Chris@0
|
89 '#type' => 'value',
|
Chris@0
|
90 '#value' => $term->id(),
|
Chris@0
|
91 ];
|
Chris@0
|
92
|
Chris@0
|
93 return parent::form($form, $form_state);
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * {@inheritdoc}
|
Chris@0
|
98 */
|
Chris@0
|
99 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
100 parent::validateForm($form, $form_state);
|
Chris@0
|
101
|
Chris@0
|
102 // Ensure numeric values.
|
Chris@0
|
103 if ($form_state->hasValue('weight') && !is_numeric($form_state->getValue('weight'))) {
|
Chris@0
|
104 $form_state->setErrorByName('weight', $this->t('Weight value must be numeric.'));
|
Chris@0
|
105 }
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * {@inheritdoc}
|
Chris@0
|
110 */
|
Chris@0
|
111 public function buildEntity(array $form, FormStateInterface $form_state) {
|
Chris@0
|
112 $term = parent::buildEntity($form, $form_state);
|
Chris@0
|
113
|
Chris@0
|
114 // Prevent leading and trailing spaces in term names.
|
Chris@0
|
115 $term->setName(trim($term->getName()));
|
Chris@0
|
116
|
Chris@0
|
117 // Assign parents with proper delta values starting from 0.
|
Chris@0
|
118 $term->parent = array_keys($form_state->getValue('parent'));
|
Chris@0
|
119
|
Chris@0
|
120 return $term;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * {@inheritdoc}
|
Chris@0
|
125 */
|
Chris@0
|
126 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
127 $term = $this->entity;
|
Chris@0
|
128
|
Chris@0
|
129 $result = $term->save();
|
Chris@0
|
130
|
Chris@0
|
131 $edit_link = $term->link($this->t('Edit'), 'edit-form');
|
Chris@0
|
132 $view_link = $term->link($term->getName());
|
Chris@0
|
133 switch ($result) {
|
Chris@0
|
134 case SAVED_NEW:
|
Chris@17
|
135 $this->messenger()->addStatus($this->t('Created new term %term.', ['%term' => $view_link]));
|
Chris@0
|
136 $this->logger('taxonomy')->notice('Created new term %term.', ['%term' => $term->getName(), 'link' => $edit_link]);
|
Chris@0
|
137 break;
|
Chris@0
|
138 case SAVED_UPDATED:
|
Chris@17
|
139 $this->messenger()->addStatus($this->t('Updated term %term.', ['%term' => $view_link]));
|
Chris@0
|
140 $this->logger('taxonomy')->notice('Updated term %term.', ['%term' => $term->getName(), 'link' => $edit_link]);
|
Chris@0
|
141 break;
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 $current_parent_count = count($form_state->getValue('parent'));
|
Chris@0
|
145 $previous_parent_count = count($form_state->get(['taxonomy', 'parent']));
|
Chris@0
|
146 // Root doesn't count if it's the only parent.
|
Chris@0
|
147 if ($current_parent_count == 1 && $form_state->hasValue(['parent', 0])) {
|
Chris@0
|
148 $current_parent_count = 0;
|
Chris@0
|
149 $form_state->setValue('parent', []);
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 // If the number of parents has been reduced to one or none, do a check on the
|
Chris@0
|
153 // parents of every term in the vocabulary value.
|
Chris@0
|
154 $vocabulary = $form_state->get(['taxonomy', 'vocabulary']);
|
Chris@0
|
155 if ($current_parent_count < $previous_parent_count && $current_parent_count < 2) {
|
Chris@0
|
156 taxonomy_check_vocabulary_hierarchy($vocabulary, $form_state->getValues());
|
Chris@0
|
157 }
|
Chris@0
|
158 // If we've increased the number of parents and this is a single or flat
|
Chris@0
|
159 // hierarchy, update the vocabulary immediately.
|
Chris@0
|
160 elseif ($current_parent_count > $previous_parent_count && $vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE) {
|
Chris@0
|
161 $vocabulary->setHierarchy($current_parent_count == 1 ? VocabularyInterface::HIERARCHY_SINGLE : VocabularyInterface::HIERARCHY_MULTIPLE);
|
Chris@0
|
162 $vocabulary->save();
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 $form_state->setValue('tid', $term->id());
|
Chris@0
|
166 $form_state->set('tid', $term->id());
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 }
|