3 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;
13 * Base form for vocabulary edit forms.
15 class VocabularyForm extends BundleEntityFormBase {
18 * The vocabulary storage.
20 * @var \Drupal\taxonomy\VocabularyStorageInterface.
22 protected $vocabularyStorage;
25 * Constructs a new vocabulary form.
27 * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
28 * The vocabulary storage.
30 public function __construct(VocabularyStorageInterface $vocabulary_storage) {
31 $this->vocabularyStorage = $vocabulary_storage;
37 public static function create(ContainerInterface $container) {
39 $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
46 public function form(array $form, FormStateInterface $form_state) {
47 $vocabulary = $this->entity;
48 if ($vocabulary->isNew()) {
49 $form['#title'] = $this->t('Add vocabulary');
52 $form['#title'] = $this->t('Edit vocabulary');
56 '#type' => 'textfield',
57 '#title' => $this->t('Name'),
58 '#default_value' => $vocabulary->label(),
63 '#type' => 'machine_name',
64 '#default_value' => $vocabulary->id(),
65 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
67 'exists' => [$this, 'exists'],
71 $form['description'] = [
72 '#type' => 'textfield',
73 '#title' => $this->t('Description'),
74 '#default_value' => $vocabulary->getDescription(),
77 // $form['langcode'] is not wrapped in an
78 // if ($this->moduleHandler->moduleExists('language')) check because the
79 // language_select form element works also without the language module being
80 // installed. https://www.drupal.org/node/1749954 documents the new element.
82 '#type' => 'language_select',
83 '#title' => $this->t('Vocabulary language'),
84 '#languages' => LanguageInterface::STATE_ALL,
85 '#default_value' => $vocabulary->language()->getId(),
87 if ($this->moduleHandler->moduleExists('language')) {
88 $form['default_terms_language'] = [
90 '#title' => $this->t('Terms language'),
93 $form['default_terms_language']['default_language'] = [
94 '#type' => 'language_configuration',
95 '#entity_information' => [
96 'entity_type' => 'taxonomy_term',
97 'bundle' => $vocabulary->id(),
99 '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id()),
102 // Set the hierarchy to "multiple parents" by default. This simplifies the
103 // vocabulary form and standardizes the term form.
104 $form['hierarchy'] = [
109 $form = parent::form($form, $form_state);
110 return $this->protectBundleIdElement($form);
116 public function save(array $form, FormStateInterface $form_state) {
117 $vocabulary = $this->entity;
119 // Prevent leading and trailing spaces in vocabulary names.
120 $vocabulary->set('name', trim($vocabulary->label()));
122 $status = $vocabulary->save();
123 $edit_link = $this->entity->link($this->t('Edit'));
126 drupal_set_message($this->t('Created new vocabulary %name.', ['%name' => $vocabulary->label()]));
127 $this->logger('taxonomy')->notice('Created new vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
128 $form_state->setRedirectUrl($vocabulary->urlInfo('overview-form'));
132 drupal_set_message($this->t('Updated vocabulary %name.', ['%name' => $vocabulary->label()]));
133 $this->logger('taxonomy')->notice('Updated vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
134 $form_state->setRedirectUrl($vocabulary->urlInfo('collection'));
138 $form_state->setValue('vid', $vocabulary->id());
139 $form_state->set('vid', $vocabulary->id());
143 * Determines if the vocabulary already exists.
149 * TRUE if the vocabulary exists, FALSE otherwise.
151 public function exists($vid) {
152 $action = $this->vocabularyStorage->load($vid);
153 return !empty($action);