Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / src / VocabularyForm.php
1 <?php
2
3 namespace Drupal\taxonomy;
4
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 class VocabularyForm extends BundleEntityFormBase {
16
17   /**
18    * The vocabulary storage.
19    *
20    * @var \Drupal\taxonomy\VocabularyStorageInterface.
21    */
22   protected $vocabularyStorage;
23
24   /**
25    * Constructs a new vocabulary form.
26    *
27    * @param \Drupal\taxonomy\VocabularyStorageInterface $vocabulary_storage
28    *   The vocabulary storage.
29    */
30   public function __construct(VocabularyStorageInterface $vocabulary_storage) {
31     $this->vocabularyStorage = $vocabulary_storage;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static(
39       $container->get('entity.manager')->getStorage('taxonomy_vocabulary')
40     );
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function form(array $form, FormStateInterface $form_state) {
47     $vocabulary = $this->entity;
48     if ($vocabulary->isNew()) {
49       $form['#title'] = $this->t('Add vocabulary');
50     }
51     else {
52       $form['#title'] = $this->t('Edit vocabulary');
53     }
54
55     $form['name'] = [
56       '#type' => 'textfield',
57       '#title' => $this->t('Name'),
58       '#default_value' => $vocabulary->label(),
59       '#maxlength' => 255,
60       '#required' => TRUE,
61     ];
62     $form['vid'] = [
63       '#type' => 'machine_name',
64       '#default_value' => $vocabulary->id(),
65       '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
66       '#machine_name' => [
67         'exists' => [$this, 'exists'],
68         'source' => ['name'],
69       ],
70     ];
71     $form['description'] = [
72       '#type' => 'textfield',
73       '#title' => $this->t('Description'),
74       '#default_value' => $vocabulary->getDescription(),
75     ];
76
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.
81     $form['langcode'] = [
82       '#type' => 'language_select',
83       '#title' => $this->t('Vocabulary language'),
84       '#languages' => LanguageInterface::STATE_ALL,
85       '#default_value' => $vocabulary->language()->getId(),
86     ];
87     if ($this->moduleHandler->moduleExists('language')) {
88       $form['default_terms_language'] = [
89         '#type' => 'details',
90         '#title' => $this->t('Terms language'),
91         '#open' => TRUE,
92       ];
93       $form['default_terms_language']['default_language'] = [
94         '#type' => 'language_configuration',
95         '#entity_information' => [
96           'entity_type' => 'taxonomy_term',
97           'bundle' => $vocabulary->id(),
98         ],
99         '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary->id()),
100       ];
101     }
102     // Set the hierarchy to "multiple parents" by default. This simplifies the
103     // vocabulary form and standardizes the term form.
104     $form['hierarchy'] = [
105       '#type' => 'value',
106       '#value' => '0',
107     ];
108
109     $form = parent::form($form, $form_state);
110     return $this->protectBundleIdElement($form);
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function save(array $form, FormStateInterface $form_state) {
117     $vocabulary = $this->entity;
118
119     // Prevent leading and trailing spaces in vocabulary names.
120     $vocabulary->set('name', trim($vocabulary->label()));
121
122     $status = $vocabulary->save();
123     $edit_link = $this->entity->link($this->t('Edit'));
124     switch ($status) {
125       case SAVED_NEW:
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'));
129         break;
130
131       case SAVED_UPDATED:
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'));
135         break;
136     }
137
138     $form_state->setValue('vid', $vocabulary->id());
139     $form_state->set('vid', $vocabulary->id());
140   }
141
142   /**
143    * Determines if the vocabulary already exists.
144    *
145    * @param string $vid
146    *   The vocabulary ID.
147    *
148    * @return bool
149    *   TRUE if the vocabulary exists, FALSE otherwise.
150    */
151   public function exists($vid) {
152     $action = $this->vocabularyStorage->load($vid);
153     return !empty($action);
154   }
155
156 }