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