comparison core/modules/taxonomy/src/TermForm.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 129ea1e6d783
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php 1 <?php
2 2
3 namespace Drupal\taxonomy; 3 namespace Drupal\taxonomy;
4 4
5 use Drupal\Core\Entity\ContentEntityForm; 5 use Drupal\Core\Entity\ContentEntityForm;
6 use Drupal\Core\Entity\EntityConstraintViolationListInterface;
6 use Drupal\Core\Form\FormStateInterface; 7 use Drupal\Core\Form\FormStateInterface;
7 8
8 /** 9 /**
9 * Base for handler for taxonomy term edit forms. 10 * Base for handler for taxonomy term edit forms.
10 * 11 *
15 /** 16 /**
16 * {@inheritdoc} 17 * {@inheritdoc}
17 */ 18 */
18 public function form(array $form, FormStateInterface $form_state) { 19 public function form(array $form, FormStateInterface $form_state) {
19 $term = $this->entity; 20 $term = $this->entity;
20 $vocab_storage = $this->entityManager->getStorage('taxonomy_vocabulary'); 21 $vocab_storage = $this->entityTypeManager->getStorage('taxonomy_vocabulary');
21 $taxonomy_storage = $this->entityManager->getStorage('taxonomy_term'); 22 /** @var \Drupal\taxonomy\TermStorageInterface $taxonomy_storage */
23 $taxonomy_storage = $this->entityTypeManager->getStorage('taxonomy_term');
22 $vocabulary = $vocab_storage->load($term->bundle()); 24 $vocabulary = $vocab_storage->load($term->bundle());
23 25
24 $parent = array_keys($taxonomy_storage->loadParents($term->id())); 26 $parent = array_keys($taxonomy_storage->loadParents($term->id()));
25 $form_state->set(['taxonomy', 'parent'], $parent); 27 $form_state->set(['taxonomy', 'parent'], $parent);
26 $form_state->set(['taxonomy', 'vocabulary'], $vocabulary); 28 $form_state->set(['taxonomy', 'vocabulary'], $vocabulary);
27 29
28 $form['relations'] = [ 30 $form['relations'] = [
29 '#type' => 'details', 31 '#type' => 'details',
30 '#title' => $this->t('Relations'), 32 '#title' => $this->t('Relations'),
31 '#open' => $vocabulary->getHierarchy() == VocabularyInterface::HIERARCHY_MULTIPLE, 33 '#open' => $taxonomy_storage->getVocabularyHierarchyType($vocabulary->id()) == VocabularyInterface::HIERARCHY_MULTIPLE,
32 '#weight' => 10, 34 '#weight' => 10,
33 ]; 35 ];
34 36
35 // \Drupal\taxonomy\TermStorageInterface::loadTree() and 37 // \Drupal\taxonomy\TermStorageInterface::loadTree() and
36 // \Drupal\taxonomy\TermStorageInterface::loadParents() may contain large 38 // \Drupal\taxonomy\TermStorageInterface::loadParents() may contain large
121 } 123 }
122 124
123 /** 125 /**
124 * {@inheritdoc} 126 * {@inheritdoc}
125 */ 127 */
128 protected function getEditedFieldNames(FormStateInterface $form_state) {
129 return array_merge(['parent', 'weight'], parent::getEditedFieldNames($form_state));
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
136 // Manually flag violations of fields not handled by the form display. This
137 // is necessary as entity form displays only flag violations for fields
138 // contained in the display.
139 // @see ::form()
140 foreach ($violations->getByField('parent') as $violation) {
141 $form_state->setErrorByName('parent', $violation->getMessage());
142 }
143 foreach ($violations->getByField('weight') as $violation) {
144 $form_state->setErrorByName('weight', $violation->getMessage());
145 }
146
147 parent::flagViolations($violations, $form, $form_state);
148 }
149
150 /**
151 * {@inheritdoc}
152 */
126 public function save(array $form, FormStateInterface $form_state) { 153 public function save(array $form, FormStateInterface $form_state) {
127 $term = $this->entity; 154 $term = $this->entity;
128 155
129 $result = $term->save(); 156 $result = $term->save();
130 157
131 $edit_link = $term->link($this->t('Edit'), 'edit-form'); 158 $edit_link = $term->toLink($this->t('Edit'), 'edit-form')->toString();
132 $view_link = $term->link($term->getName()); 159 $view_link = $term->toLink()->toString();
133 switch ($result) { 160 switch ($result) {
134 case SAVED_NEW: 161 case SAVED_NEW:
135 $this->messenger()->addStatus($this->t('Created new term %term.', ['%term' => $view_link])); 162 $this->messenger()->addStatus($this->t('Created new term %term.', ['%term' => $view_link]));
136 $this->logger('taxonomy')->notice('Created new term %term.', ['%term' => $term->getName(), 'link' => $edit_link]); 163 $this->logger('taxonomy')->notice('Created new term %term.', ['%term' => $term->getName(), 'link' => $edit_link]);
137 break; 164 break;
140 $this->logger('taxonomy')->notice('Updated term %term.', ['%term' => $term->getName(), 'link' => $edit_link]); 167 $this->logger('taxonomy')->notice('Updated term %term.', ['%term' => $term->getName(), 'link' => $edit_link]);
141 break; 168 break;
142 } 169 }
143 170
144 $current_parent_count = count($form_state->getValue('parent')); 171 $current_parent_count = count($form_state->getValue('parent'));
145 $previous_parent_count = count($form_state->get(['taxonomy', 'parent']));
146 // Root doesn't count if it's the only parent. 172 // Root doesn't count if it's the only parent.
147 if ($current_parent_count == 1 && $form_state->hasValue(['parent', 0])) { 173 if ($current_parent_count == 1 && $form_state->hasValue(['parent', 0])) {
148 $current_parent_count = 0;
149 $form_state->setValue('parent', []); 174 $form_state->setValue('parent', []);
150 }
151
152 // If the number of parents has been reduced to one or none, do a check on the
153 // parents of every term in the vocabulary value.
154 $vocabulary = $form_state->get(['taxonomy', 'vocabulary']);
155 if ($current_parent_count < $previous_parent_count && $current_parent_count < 2) {
156 taxonomy_check_vocabulary_hierarchy($vocabulary, $form_state->getValues());
157 }
158 // If we've increased the number of parents and this is a single or flat
159 // hierarchy, update the vocabulary immediately.
160 elseif ($current_parent_count > $previous_parent_count && $vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE) {
161 $vocabulary->setHierarchy($current_parent_count == 1 ? VocabularyInterface::HIERARCHY_SINGLE : VocabularyInterface::HIERARCHY_MULTIPLE);
162 $vocabulary->save();
163 } 175 }
164 176
165 $form_state->setValue('tid', $term->id()); 177 $form_state->setValue('tid', $term->id());
166 $form_state->set('tid', $term->id()); 178 $form_state->set('tid', $term->id());
167 } 179 }