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.
17 class VocabularyForm extends BundleEntityFormBase {
20 * The vocabulary storage.
22 * @var \Drupal\taxonomy\VocabularyStorageInterface
24 protected $vocabularyStorage;
27 * Constructs a new vocabulary form.
29 * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
30 * The vocabulary storage.
32 public function __construct(VocabularyStorageInterface $vocabulary_storage) {
33 $this->vocabularyStorage = $vocabulary_storage;
39 public static function create(ContainerInterface $container) {
41 $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
48 public function form(array $form, FormStateInterface $form_state) {
49 $vocabulary = $this->entity;
50 if ($vocabulary->isNew()) {
51 $form['#title'] = $this->t('Add vocabulary');
54 $form['#title'] = $this->t('Edit vocabulary');
58 '#type' => 'textfield',
59 '#title' => $this->t('Name'),
60 '#default_value' => $vocabulary->label(),
65 '#type' => 'machine_name',
66 '#default_value' => $vocabulary->id(),
67 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
69 'exists' => [$this, 'exists'],
73 $form['description'] = [
74 '#type' => 'textfield',
75 '#title' => $this->t('Description'),
76 '#default_value' => $vocabulary->getDescription(),
79 // $form['langcode'] is not wrapped in an
80 // if ($this->moduleHandler->moduleExists('language')) check because the
81 // language_select form element works also without the language module being
82 // installed. https://www.drupal.org/node/1749954 documents the new element.
84 '#type' => 'language_select',
85 '#title' => $this->t('Vocabulary language'),
86 '#languages' => LanguageInterface::STATE_ALL,
87 '#default_value' => $vocabulary->language()->getId(),
89 if ($this->moduleHandler->moduleExists('language')) {
90 $form['default_terms_language'] = [
92 '#title' => $this->t('Term language'),
95 $form['default_terms_language']['default_language'] = [
96 '#type' => 'language_configuration',
97 '#entity_information' => [
98 'entity_type' => 'taxonomy_term',
99 'bundle' => $vocabulary->id(),
101 '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id()),
104 // Set the hierarchy to "multiple parents" by default. This simplifies the
105 // vocabulary form and standardizes the term form.
106 $form['hierarchy'] = [
111 $form = parent::form($form, $form_state);
112 return $this->protectBundleIdElement($form);
118 public function save(array $form, FormStateInterface $form_state) {
119 $vocabulary = $this->entity;
121 // Prevent leading and trailing spaces in vocabulary names.
122 $vocabulary->set('name', trim($vocabulary->label()));
124 $status = $vocabulary->save();
125 $edit_link = $this->entity->link($this->t('Edit'));
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'));
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'));
140 $form_state->setValue('vid', $vocabulary->id());
141 $form_state->set('vid', $vocabulary->id());
145 * Determines if the vocabulary already exists.
151 * TRUE if the vocabulary exists, FALSE otherwise.
153 public function exists($vid) {
154 $action = $this->vocabularyStorage->load($vid);
155 return !empty($action);