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