Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\language;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\DraggableListBuilder;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
10 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
11 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@17
|
12 use Drupal\Core\Messenger\MessengerInterface;
|
Chris@0
|
13 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Defines a class to build a listing of language entities.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @see \Drupal\language\Entity\ConfigurableLanguage
|
Chris@0
|
19 */
|
Chris@0
|
20 class LanguageListBuilder extends DraggableListBuilder {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * {@inheritdoc}
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $entitiesKey = 'languages';
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The language manager.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Language\LanguageManagerInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $languageManager;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The configuration factory.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var \Drupal\Core\Config\ConfigFactoryInterface
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $configFactory;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@17
|
42 * The messenger.
|
Chris@17
|
43 *
|
Chris@17
|
44 * @var \Drupal\Core\Messenger\MessengerInterface
|
Chris@17
|
45 */
|
Chris@17
|
46 protected $messenger;
|
Chris@17
|
47
|
Chris@17
|
48 /**
|
Chris@0
|
49 * {@inheritdoc}
|
Chris@0
|
50 */
|
Chris@0
|
51 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
Chris@0
|
52 return new static(
|
Chris@0
|
53 $entity_type,
|
Chris@18
|
54 $container->get('entity_type.manager')->getStorage($entity_type->id()),
|
Chris@0
|
55 $container->get('language_manager'),
|
Chris@17
|
56 $container->get('config.factory'),
|
Chris@17
|
57 $container->get('messenger')
|
Chris@0
|
58 );
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Constructs a new LanguageListBuilder object.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
65 * The entity type definition.
|
Chris@0
|
66 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
|
Chris@0
|
67 * The entity storage handler class.
|
Chris@0
|
68 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
69 * The language manager.
|
Chris@0
|
70 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
71 * The factory for configuration objects.
|
Chris@17
|
72 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
|
Chris@17
|
73 * The messenger.
|
Chris@0
|
74 */
|
Chris@17
|
75 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
|
Chris@0
|
76 parent::__construct($entity_type, $storage);
|
Chris@0
|
77 $this->languageManager = $language_manager;
|
Chris@0
|
78 $this->configFactory = $config_factory;
|
Chris@17
|
79 $this->messenger = $messenger;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * {@inheritdoc}
|
Chris@0
|
84 */
|
Chris@0
|
85 public function load() {
|
Chris@0
|
86 $entities = $this->storage->loadByProperties(['locked' => FALSE]);
|
Chris@0
|
87
|
Chris@0
|
88 // Sort the entities using the entity class's sort() method.
|
Chris@0
|
89 // See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
|
Chris@0
|
90 uasort($entities, [$this->entityType->getClass(), 'sort']);
|
Chris@0
|
91 return $entities;
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function getFormId() {
|
Chris@0
|
98 return 'language_admin_overview_form';
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * {@inheritdoc}
|
Chris@0
|
103 */
|
Chris@0
|
104 public function buildHeader() {
|
Chris@0
|
105 $header = [
|
Chris@0
|
106 'label' => t('Name'),
|
Chris@0
|
107 'default' => t('Default'),
|
Chris@0
|
108 ] + parent::buildHeader();
|
Chris@0
|
109 return $header;
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * {@inheritdoc}
|
Chris@0
|
114 */
|
Chris@0
|
115 public function buildRow(EntityInterface $entity) {
|
Chris@0
|
116 $row['label'] = $entity->label();
|
Chris@0
|
117 $row['default'] = [
|
Chris@0
|
118 '#type' => 'radio',
|
Chris@0
|
119 '#parents' => ['site_default_language'],
|
Chris@0
|
120 '#title' => t('Set @title as default', ['@title' => $entity->label()]),
|
Chris@0
|
121 '#title_display' => 'invisible',
|
Chris@0
|
122 '#return_value' => $entity->id(),
|
Chris@0
|
123 '#id' => 'edit-site-default-language-' . $entity->id(),
|
Chris@0
|
124 ];
|
Chris@0
|
125 // Mark the right language as default in the form.
|
Chris@0
|
126 if ($entity->id() == $this->languageManager->getDefaultLanguage()->getId()) {
|
Chris@0
|
127 $row['default']['#default_value'] = $entity->id();
|
Chris@0
|
128 }
|
Chris@0
|
129 return $row + parent::buildRow($entity);
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * {@inheritdoc}
|
Chris@0
|
134 */
|
Chris@0
|
135 public function buildForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
136 $form = parent::buildForm($form, $form_state);
|
Chris@0
|
137
|
Chris@0
|
138 $form[$this->entitiesKey]['#languages'] = $this->entities;
|
Chris@0
|
139 $form['actions']['submit']['#value'] = t('Save configuration');
|
Chris@0
|
140 return $form;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * {@inheritdoc}
|
Chris@0
|
145 */
|
Chris@0
|
146 public function validateForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
147 if (!isset($this->entities[$form_state->getValue('site_default_language')])) {
|
Chris@0
|
148 $form_state->setErrorByName('site_default_language', $this->t('Selected default language no longer exists.'));
|
Chris@0
|
149 }
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * {@inheritdoc}
|
Chris@0
|
154 */
|
Chris@0
|
155 public function submitForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
156 parent::submitForm($form, $form_state);
|
Chris@0
|
157
|
Chris@0
|
158 // Save the default language if changed.
|
Chris@0
|
159 $new_id = $form_state->getValue('site_default_language');
|
Chris@0
|
160 if ($new_id != $this->languageManager->getDefaultLanguage()->getId()) {
|
Chris@0
|
161 $this->configFactory->getEditable('system.site')->set('default_langcode', $new_id)->save();
|
Chris@0
|
162 $this->languageManager->reset();
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
|
Chris@0
|
166 $this->languageManager->updateLockedLanguageWeights();
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@17
|
169 $this->messenger->addStatus($this->t('Configuration saved.'));
|
Chris@0
|
170 // Force the redirection to the page with the language we have just
|
Chris@0
|
171 // selected as default.
|
Chris@18
|
172 $form_state->setRedirectUrl($this->entities[$new_id]->toUrl('collection', ['language' => $this->entities[$new_id]]));
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 }
|