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