View source
<?php
namespace Drupal\taxonomy;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ContentLanguageSettings;
use Symfony\Component\DependencyInjection\ContainerInterface;
class VocabularyForm extends BundleEntityFormBase {
protected $vocabularyStorage;
public function __construct(VocabularyStorageInterface $vocabulary_storage) {
$this->vocabularyStorage = $vocabulary_storage;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary'));
}
public function buildEntity(array $form, FormStateInterface $form_state) {
$entity = parent::buildEntity($form, $form_state);
if (trim($form_state
->getValue('description')) === '') {
$entity
->set('description', NULL);
}
return $entity;
}
public function form(array $form, FormStateInterface $form_state) {
$vocabulary = $this->entity;
if ($vocabulary
->isNew()) {
$form['#title'] = $this
->t('Add vocabulary');
}
else {
$form['#title'] = $this
->t('Edit vocabulary');
}
$form['name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#default_value' => $vocabulary
->label(),
'#maxlength' => 255,
'#required' => TRUE,
];
$form['vid'] = [
'#type' => 'machine_name',
'#default_value' => $vocabulary
->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#machine_name' => [
'exists' => [
$this,
'exists',
],
'source' => [
'name',
],
],
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $vocabulary
->getDescription(),
];
$form['revision'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Create new revision'),
'#default_value' => $vocabulary
->shouldCreateNewRevision(),
'#description' => $this
->t('Create a new revision by default for this vocabulary.'),
];
$form['langcode'] = [
'#type' => 'language_select',
'#title' => $this
->t('Vocabulary language'),
'#languages' => LanguageInterface::STATE_ALL,
'#default_value' => $vocabulary
->language()
->getId(),
];
if ($this->moduleHandler
->moduleExists('language')) {
$form['default_terms_language'] = [
'#type' => 'details',
'#title' => $this
->t('Term language'),
'#open' => TRUE,
];
$form['default_terms_language']['default_language'] = [
'#type' => 'language_configuration',
'#entity_information' => [
'entity_type' => 'taxonomy_term',
'bundle' => $vocabulary
->id(),
],
'#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary
->id()),
];
}
$form['hierarchy'] = [
'#type' => 'value',
'#value' => '0',
];
$form = parent::form($form, $form_state);
return $this
->protectBundleIdElement($form);
}
public function save(array $form, FormStateInterface $form_state) {
$vocabulary = $this->entity;
$vocabulary
->setNewRevision($form_state
->getValue([
'revision',
]));
$vocabulary
->set('name', trim($vocabulary
->label()));
$status = $vocabulary
->save();
$edit_link = $this->entity
->toLink($this
->t('Edit'), 'edit-form')
->toString();
switch ($status) {
case SAVED_NEW:
$this
->messenger()
->addStatus($this
->t('Created new vocabulary %name.', [
'%name' => $vocabulary
->label(),
]));
$this
->logger('taxonomy')
->notice('Created new vocabulary %name.', [
'%name' => $vocabulary
->label(),
'link' => $edit_link,
]);
$form_state
->setRedirectUrl($vocabulary
->toUrl('overview-form'));
break;
case SAVED_UPDATED:
$this
->messenger()
->addStatus($this
->t('Updated vocabulary %name.', [
'%name' => $vocabulary
->label(),
]));
$this
->logger('taxonomy')
->notice('Updated vocabulary %name.', [
'%name' => $vocabulary
->label(),
'link' => $edit_link,
]);
$form_state
->setRedirectUrl($vocabulary
->toUrl('collection'));
break;
}
$form_state
->setValue('vid', $vocabulary
->id());
$form_state
->set('vid', $vocabulary
->id());
}
public function exists($vid) {
$action = $this->vocabularyStorage
->load($vid);
return !empty($action);
}
}