annotate core/modules/taxonomy/src/VocabularyForm.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\taxonomy;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\BundleEntityFormBase;
Chris@0 6 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 7 use Drupal\Core\Form\FormStateInterface;
Chris@0 8 use Drupal\Core\Language\LanguageInterface;
Chris@0 9 use Drupal\language\Entity\ContentLanguageSettings;
Chris@0 10 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Base form for vocabulary edit forms.
Chris@14 14 *
Chris@14 15 * @internal
Chris@0 16 */
Chris@0 17 class VocabularyForm extends BundleEntityFormBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The vocabulary storage.
Chris@0 21 *
Chris@17 22 * @var \Drupal\taxonomy\VocabularyStorageInterface
Chris@0 23 */
Chris@0 24 protected $vocabularyStorage;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Constructs a new vocabulary form.
Chris@0 28 *
Chris@0 29 * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
Chris@0 30 * The vocabulary storage.
Chris@0 31 */
Chris@0 32 public function __construct(VocabularyStorageInterface $vocabulary_storage) {
Chris@0 33 $this->vocabularyStorage = $vocabulary_storage;
Chris@0 34 }
Chris@0 35
Chris@0 36 /**
Chris@0 37 * {@inheritdoc}
Chris@0 38 */
Chris@0 39 public static function create(ContainerInterface $container) {
Chris@0 40 return new static(
Chris@0 41 $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
Chris@0 42 );
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * {@inheritdoc}
Chris@0 47 */
Chris@0 48 public function form(array $form, FormStateInterface $form_state) {
Chris@0 49 $vocabulary = $this->entity;
Chris@0 50 if ($vocabulary->isNew()) {
Chris@0 51 $form['#title'] = $this->t('Add vocabulary');
Chris@0 52 }
Chris@0 53 else {
Chris@0 54 $form['#title'] = $this->t('Edit vocabulary');
Chris@0 55 }
Chris@0 56
Chris@0 57 $form['name'] = [
Chris@0 58 '#type' => 'textfield',
Chris@0 59 '#title' => $this->t('Name'),
Chris@0 60 '#default_value' => $vocabulary->label(),
Chris@0 61 '#maxlength' => 255,
Chris@0 62 '#required' => TRUE,
Chris@0 63 ];
Chris@0 64 $form['vid'] = [
Chris@0 65 '#type' => 'machine_name',
Chris@0 66 '#default_value' => $vocabulary->id(),
Chris@0 67 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
Chris@0 68 '#machine_name' => [
Chris@0 69 'exists' => [$this, 'exists'],
Chris@0 70 'source' => ['name'],
Chris@0 71 ],
Chris@0 72 ];
Chris@0 73 $form['description'] = [
Chris@0 74 '#type' => 'textfield',
Chris@0 75 '#title' => $this->t('Description'),
Chris@0 76 '#default_value' => $vocabulary->getDescription(),
Chris@0 77 ];
Chris@0 78
Chris@0 79 // $form['langcode'] is not wrapped in an
Chris@0 80 // if ($this->moduleHandler->moduleExists('language')) check because the
Chris@0 81 // language_select form element works also without the language module being
Chris@0 82 // installed. https://www.drupal.org/node/1749954 documents the new element.
Chris@0 83 $form['langcode'] = [
Chris@0 84 '#type' => 'language_select',
Chris@0 85 '#title' => $this->t('Vocabulary language'),
Chris@0 86 '#languages' => LanguageInterface::STATE_ALL,
Chris@0 87 '#default_value' => $vocabulary->language()->getId(),
Chris@0 88 ];
Chris@0 89 if ($this->moduleHandler->moduleExists('language')) {
Chris@0 90 $form['default_terms_language'] = [
Chris@0 91 '#type' => 'details',
Chris@17 92 '#title' => $this->t('Term language'),
Chris@0 93 '#open' => TRUE,
Chris@0 94 ];
Chris@0 95 $form['default_terms_language']['default_language'] = [
Chris@0 96 '#type' => 'language_configuration',
Chris@0 97 '#entity_information' => [
Chris@0 98 'entity_type' => 'taxonomy_term',
Chris@0 99 'bundle' => $vocabulary->id(),
Chris@0 100 ],
Chris@0 101 '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id()),
Chris@0 102 ];
Chris@0 103 }
Chris@0 104 // Set the hierarchy to "multiple parents" by default. This simplifies the
Chris@0 105 // vocabulary form and standardizes the term form.
Chris@0 106 $form['hierarchy'] = [
Chris@0 107 '#type' => 'value',
Chris@0 108 '#value' => '0',
Chris@0 109 ];
Chris@0 110
Chris@0 111 $form = parent::form($form, $form_state);
Chris@0 112 return $this->protectBundleIdElement($form);
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * {@inheritdoc}
Chris@0 117 */
Chris@0 118 public function save(array $form, FormStateInterface $form_state) {
Chris@0 119 $vocabulary = $this->entity;
Chris@0 120
Chris@0 121 // Prevent leading and trailing spaces in vocabulary names.
Chris@0 122 $vocabulary->set('name', trim($vocabulary->label()));
Chris@0 123
Chris@0 124 $status = $vocabulary->save();
Chris@18 125 $edit_link = $this->entity->toLink($this->t('Edit'), 'edit-form')->toString();
Chris@0 126 switch ($status) {
Chris@0 127 case SAVED_NEW:
Chris@17 128 $this->messenger()->addStatus($this->t('Created new vocabulary %name.', ['%name' => $vocabulary->label()]));
Chris@0 129 $this->logger('taxonomy')->notice('Created new vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
Chris@18 130 $form_state->setRedirectUrl($vocabulary->toUrl('overview-form'));
Chris@0 131 break;
Chris@0 132
Chris@0 133 case SAVED_UPDATED:
Chris@17 134 $this->messenger()->addStatus($this->t('Updated vocabulary %name.', ['%name' => $vocabulary->label()]));
Chris@0 135 $this->logger('taxonomy')->notice('Updated vocabulary %name.', ['%name' => $vocabulary->label(), 'link' => $edit_link]);
Chris@18 136 $form_state->setRedirectUrl($vocabulary->toUrl('collection'));
Chris@0 137 break;
Chris@0 138 }
Chris@0 139
Chris@0 140 $form_state->setValue('vid', $vocabulary->id());
Chris@0 141 $form_state->set('vid', $vocabulary->id());
Chris@0 142 }
Chris@0 143
Chris@0 144 /**
Chris@0 145 * Determines if the vocabulary already exists.
Chris@0 146 *
Chris@0 147 * @param string $vid
Chris@0 148 * The vocabulary ID.
Chris@0 149 *
Chris@0 150 * @return bool
Chris@0 151 * TRUE if the vocabulary exists, FALSE otherwise.
Chris@0 152 */
Chris@0 153 public function exists($vid) {
Chris@0 154 $action = $this->vocabularyStorage->load($vid);
Chris@0 155 return !empty($action);
Chris@0 156 }
Chris@0 157
Chris@0 158 }