annotate core/modules/taxonomy/src/TermForm.php @ 19:fa3358dc1485 tip

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