TermForm.php 5.42 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\taxonomy\TermForm.
6 7 8 9
 */

namespace Drupal\taxonomy;

10
use Drupal\Core\Entity\ContentEntityForm;
11
use Drupal\Core\Form\FormStateInterface;
12 13 14 15

/**
 * Base for controller for taxonomy term edit forms.
 */
16
class TermForm extends ContentEntityForm {
17

18 19
  /**
   * {@inheritdoc}
20
   */
21
  public function form(array $form, FormStateInterface $form_state) {
22
    $term = $this->entity;
23
    $vocab_storage = $this->entityManager->getStorage('taxonomy_vocabulary');
24
    $vocabulary = $vocab_storage->load($term->bundle());
25

26
    $parent = array_keys(taxonomy_term_load_parents($term->id()));
27 28
    $form_state->set(['taxonomy', 'parent'], $parent);
    $form_state->set(['taxonomy', 'vocabulary'], $vocabulary);
29 30

    $form['relations'] = array(
31
      '#type' => 'details',
32
      '#title' => $this->t('Relations'),
33
      '#open' => $vocabulary->getHierarchy() == TAXONOMY_HIERARCHY_MULTIPLE,
34 35 36
      '#weight' => 10,
    );

37 38 39 40
    // taxonomy_get_tree and taxonomy_term_load_parents may contain large
    // numbers of items so we check for taxonomy.settings:override_selector
    // before loading the full vocabulary. Contrib modules can then intercept
    // before hook_form_alter to provide scalable alternatives.
41
    if (!$this->config('taxonomy.settings')->get('override_selector')) {
42 43
      $parent = array_keys(taxonomy_term_load_parents($term->id()));
      $children = taxonomy_get_tree($vocabulary->id(), $term->id());
44 45 46 47 48

      // A term can't be the child of itself, nor of its children.
      foreach ($children as $child) {
        $exclude[] = $child->tid;
      }
49
      $exclude[] = $term->id();
50

51
      $tree = taxonomy_get_tree($vocabulary->id());
52
      $options = array('<' . $this->t('root') . '>');
53 54 55
      if (empty($parent)) {
        $parent = array(0);
      }
56

57 58 59 60 61 62 63 64
      foreach ($tree as $item) {
        if (!in_array($item->tid, $exclude)) {
          $options[$item->tid] = str_repeat('-', $item->depth) . $item->name;
        }
      }

      $form['relations']['parent'] = array(
        '#type' => 'select',
65
        '#title' => $this->t('Parent terms'),
66 67 68 69 70 71 72 73
        '#options' => $options,
        '#default_value' => $parent,
        '#multiple' => TRUE,
      );
    }

    $form['relations']['weight'] = array(
      '#type' => 'textfield',
74
      '#title' => $this->t('Weight'),
75
      '#size' => 6,
76
      '#default_value' => $term->getWeight(),
77
      '#description' => $this->t('Terms are displayed in ascending order by weight.'),
78 79 80 81 82
      '#required' => TRUE,
    );

    $form['vid'] = array(
      '#type' => 'value',
83
      '#value' => $vocabulary->id(),
84 85 86 87
    );

    $form['tid'] = array(
      '#type' => 'value',
88
      '#value' => $term->id(),
89 90 91 92 93 94
    );

    return parent::form($form, $form_state, $term);
  }

  /**
95
   * {@inheritdoc}
96
   */
97
  public function validate(array $form, FormStateInterface $form_state) {
98 99 100
    parent::validate($form, $form_state);

    // Ensure numeric values.
101
    if ($form_state->hasValue('weight') && !is_numeric($form_state->getValue('weight'))) {
102
      $form_state->setErrorByName('weight', $this->t('Weight value must be numeric.'));
103 104 105 106
    }
  }

  /**
107
   * {@inheritdoc}
108
   */
109
  public function buildEntity(array $form, FormStateInterface $form_state) {
110
    $term = parent::buildEntity($form, $form_state);
111 112

    // Prevent leading and trailing spaces in term names.
113
    $term->setName(trim($term->getName()));
114 115

    // Assign parents with proper delta values starting from 0.
116
    $term->parent = array_keys($form_state->getValue('parent'));
117 118 119 120 121

    return $term;
  }

  /**
122
   * {@inheritdoc}
123
   */
124
  public function save(array $form, FormStateInterface $form_state) {
125
    $term = $this->entity;
126

127 128
    $result = $term->save();

129
    $link = $term->link($this->t('Edit'), 'edit-form');
130
    switch ($result) {
131
      case SAVED_NEW:
132
        drupal_set_message($this->t('Created new term %term.', array('%term' => $term->getName())));
133
        $this->logger('taxonomy')->notice('Created new term %term.', array('%term' => $term->getName(), 'link' => $link));
134 135
        break;
      case SAVED_UPDATED:
136
        drupal_set_message($this->t('Updated term %term.', array('%term' => $term->getName())));
137
        $this->logger('taxonomy')->notice('Updated term %term.', array('%term' => $term->getName(), 'link' => $link));
138 139 140
        break;
    }

141
    $current_parent_count = count($form_state->getValue('parent'));
142
    $previous_parent_count = count($form_state->get(['taxonomy', 'parent']));
143
    // Root doesn't count if it's the only parent.
144
    if ($current_parent_count == 1 && $form_state->hasValue(array('parent', 0))) {
145
      $current_parent_count = 0;
146
      $form_state->setValue('parent', array());
147 148 149 150
    }

    // If the number of parents has been reduced to one or none, do a check on the
    // parents of every term in the vocabulary value.
151
    $vocabulary = $form_state->get(['taxonomy', 'vocabulary']);
152
    if ($current_parent_count < $previous_parent_count && $current_parent_count < 2) {
153
      taxonomy_check_vocabulary_hierarchy($vocabulary, $form_state->getValues());
154 155 156
    }
    // If we've increased the number of parents and this is a single or flat
    // hierarchy, update the vocabulary immediately.
157 158
    elseif ($current_parent_count > $previous_parent_count && $vocabulary->getHierarchy() != TAXONOMY_HIERARCHY_MULTIPLE) {
      $vocabulary->setHierarchy($current_parent_count == 1 ? TAXONOMY_HIERARCHY_SINGLE : TAXONOMY_HIERARCHY_MULTIPLE);
159
      $vocabulary->save();
160 161
    }

162
    $form_state->setValue('tid', $term->id());
163
    $form_state->set('tid', $term->id());
164 165 166
  }

}