VocabularyForm.php 4.83 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\taxonomy;

5
use Drupal\Core\Entity\BundleEntityFormBase;
6
use Drupal\Core\Entity\EntityTypeInterface;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Language\LanguageInterface;
9
use Drupal\language\Entity\ContentLanguageSettings;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11 12

/**
13
 * Base form for vocabulary edit forms.
14 15
 *
 * @internal
16
 */
17
class VocabularyForm extends BundleEntityFormBase {
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  /**
   * The vocabulary storage.
   *
   * @var \Drupal\taxonomy\VocabularyStorageInterface.
   */
  protected $vocabularyStorage;

  /**
   * Constructs a new vocabulary form.
   *
   * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
   *   The vocabulary storage.
   */
  public function __construct(VocabularyStorageInterface $vocabulary_storage) {
    $this->vocabularyStorage = $vocabulary_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
    );
  }

45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function form(array $form, FormStateInterface $form_state) {
49
    $vocabulary = $this->entity;
50 51 52 53 54 55
    if ($vocabulary->isNew()) {
      $form['#title'] = $this->t('Add vocabulary');
    }
    else {
      $form['#title'] = $this->t('Edit vocabulary');
    }
56

57
    $form['name'] = [
58
      '#type' => 'textfield',
59
      '#title' => $this->t('Name'),
60
      '#default_value' => $vocabulary->label(),
61 62
      '#maxlength' => 255,
      '#required' => TRUE,
63 64
    ];
    $form['vid'] = [
65
      '#type' => 'machine_name',
66
      '#default_value' => $vocabulary->id(),
67
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
68 69 70 71 72 73
      '#machine_name' => [
        'exists' => [$this, 'exists'],
        'source' => ['name'],
      ],
    ];
    $form['description'] = [
74
      '#type' => 'textfield',
75
      '#title' => $this->t('Description'),
76
      '#default_value' => $vocabulary->getDescription(),
77
    ];
78

79 80 81
    // $form['langcode'] is not wrapped in an
    // if ($this->moduleHandler->moduleExists('language')) check because the
    // language_select form element works also without the language module being
82
    // installed. https://www.drupal.org/node/1749954 documents the new element.
83
    $form['langcode'] = [
84
      '#type' => 'language_select',
85
      '#title' => $this->t('Vocabulary language'),
86
      '#languages' => LanguageInterface::STATE_ALL,
87
      '#default_value' => $vocabulary->language()->getId(),
88
    ];
89
    if ($this->moduleHandler->moduleExists('language')) {
90
      $form['default_terms_language'] = [
91
        '#type' => 'details',
92
        '#title' => $this->t('Term language'),
93
        '#open' => TRUE,
94 95
      ];
      $form['default_terms_language']['default_language'] = [
96
        '#type' => 'language_configuration',
97
        '#entity_information' => [
98
          'entity_type' => 'taxonomy_term',
99
          'bundle' => $vocabulary->id(),
100
        ],
101
        '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id()),
102
      ];
103
    }
104 105
    // Set the hierarchy to "multiple parents" by default. This simplifies the
    // vocabulary form and standardizes the term form.
106
    $form['hierarchy'] = [
107 108
      '#type' => 'value',
      '#value' => '0',
109
    ];
110

111 112
    $form = parent::form($form, $form_state);
    return $this->protectBundleIdElement($form);
113 114 115
  }

  /**
116
   * {@inheritdoc}
117
   */
118
  public function save(array $form, FormStateInterface $form_state) {
119
    $vocabulary = $this->entity;
120 121

    // Prevent leading and trailing spaces in vocabulary names.
122
    $vocabulary->set('name', trim($vocabulary->label()));
123

124
    $status = $vocabulary->save();
125
    $edit_link = $this->entity->link($this->t('Edit'));
126
    switch ($status) {
127
      case SAVED_NEW:
128
        $this->messenger()->addStatus($this->t('Created new vocabulary %name.', ['%name' => $vocabulary->label()]));
129
        $this->logger('taxonomy')->notice('Created new vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
130
        $form_state->setRedirectUrl($vocabulary->urlInfo('overview-form'));
131 132 133
        break;

      case SAVED_UPDATED:
134
        $this->messenger()->addStatus($this->t('Updated vocabulary %name.', ['%name' => $vocabulary->label()]));
135
        $this->logger('taxonomy')->notice('Updated vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
136
        $form_state->setRedirectUrl($vocabulary->urlInfo('collection'));
137 138 139
        break;
    }

140
    $form_state->setValue('vid', $vocabulary->id());
141
    $form_state->set('vid', $vocabulary->id());
142
  }
143

144 145 146
  /**
   * Determines if the vocabulary already exists.
   *
147 148
   * @param string $vid
   *   The vocabulary ID.
149 150 151 152
   *
   * @return bool
   *   TRUE if the vocabulary exists, FALSE otherwise.
   */
153 154
  public function exists($vid) {
    $action = $this->vocabularyStorage->load($vid);
155 156 157
    return !empty($action);
  }

158
}