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