ConfigurableLanguageManager.php 14.8 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\language;

5
use Drupal\Core\Language\LanguageInterface;
6
use Drupal\Core\Config\ConfigFactoryInterface;
7
8
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\Language;
9
use Drupal\Core\Language\LanguageDefault;
10
use Drupal\Core\Language\LanguageManager;
11
use Drupal\Core\StringTranslation\TranslatableMarkup;
12
use Drupal\Core\Url;
13
use Drupal\language\Config\LanguageConfigFactoryOverrideInterface;
14
use Drupal\language\Entity\ConfigurableLanguage;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
17
18
19
20
21
22
23
24

/**
 * Overrides default LanguageManager to provide configured languages.
 */
class ConfigurableLanguageManager extends LanguageManager implements ConfigurableLanguageManagerInterface {

  /**
   * The configuration storage service.
   *
25
   * @var \Drupal\Core\Config\ConfigFactoryInterface
26
27
28
29
30
31
32
33
34
35
   */
  protected $configFactory;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

36
37
38
39
40
41
42
  /**
   * The language configuration override service.
   *
   * @var \Drupal\language\Config\LanguageConfigFactoryOverrideInterface
   */
  protected $configFactoryOverride;

43
44
45
  /**
   * The request object.
   *
46
   * @var \Symfony\Component\HttpFoundation\RequestStack
47
   */
48
  protected $requestStack;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

  /**
   * The language negotiator.
   *
   * @var \Drupal\language\LanguageNegotiatorInterface
   */
  protected $negotiator;

  /**
   * Local cache for language type configuration data.
   *
   * @var array
   */
  protected $languageTypes;

  /**
   * Local cache for language type information.
   *
   * @var array
   */
  protected $languageTypesInfo;

  /**
   * An array of language objects keyed by language type.
   *
74
   * @var \Drupal\Core\Language\LanguageInterface[]
75
76
77
   */
  protected $negotiatedLanguages;

78
79
80
81
82
83
84
  /**
   * An array of language negotiation method IDs keyed by language type.
   *
   * @var array
   */
  protected $negotiatedMethods;

85
86
87
88
89
90
91
92
  /**
   * Whether or not the language manager has been initialized.
   *
   * @var bool
   */
  protected $initialized = FALSE;

  /**
93
   * Whether language types are in the process of language initialization.
94
   *
95
   * @var bool[]
96
   */
97
  protected $initializing = [];
98
99

  /**
100
   * {@inheritdoc}
101
102
   */
  public static function rebuildServices() {
103
    \Drupal::service('kernel')->invalidateContainer();
104
105
106
107
108
  }

  /**
   * Constructs a new ConfigurableLanguageManager object.
   *
109
110
   * @param \Drupal\Core\Language\LanguageDefault $default_language
   *   The default language service.
111
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
112
   *   The configuration factory service.
113
114
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
115
116
   * @param \Drupal\language\Config\LanguageConfigFactoryOverrideInterface $config_override
   *   The language configuration override service.
117
118
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack object.
119
   */
120
  public function __construct(LanguageDefault $default_language, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, LanguageConfigFactoryOverrideInterface $config_override, RequestStack $request_stack) {
121
    $this->defaultLanguage = $default_language;
122
123
    $this->configFactory = $config_factory;
    $this->moduleHandler = $module_handler;
124
    $this->configFactoryOverride = $config_override;
125
    $this->requestStack = $request_stack;
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  }

  /**
   * {@inheritdoc}
   */
  public function init() {
    if (!$this->initialized) {
      foreach ($this->getDefinedLanguageTypes() as $type) {
        $this->getCurrentLanguage($type);
      }
      $this->initialized = TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isMultilingual() {
144
    return count($this->getLanguages(LanguageInterface::STATE_CONFIGURABLE)) > 1;
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  }

  /**
   * {@inheritdoc}
   */
  public function getLanguageTypes() {
    $this->loadLanguageTypesConfiguration();
    return $this->languageTypes['configurable'];
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinedLanguageTypes() {
    $this->loadLanguageTypesConfiguration();
    return $this->languageTypes['all'];
  }

  /**
   * Retrieves language types from the configuration storage.
   *
   * @return array
   *   An array of language type names.
   */
  protected function loadLanguageTypesConfiguration() {
    if (!$this->languageTypes) {
171
      $this->languageTypes = $this->configFactory->get('language.types')->get() ?: ['configurable' => [], 'all' => parent::getLanguageTypes()];
172
173
174
175
176
177
178
179
180
    }
    return $this->languageTypes;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinedLanguageTypesInfo() {
    if (!isset($this->languageTypesInfo)) {
181
182
      $defaults = parent::getDefinedLanguageTypesInfo();

183
      $info = $this->moduleHandler->invokeAll('language_types_info');
184
185
      $language_info = $info + $defaults;

186
      // Let other modules alter the list of language types.
187
188
      $this->moduleHandler->alter('language_types_info', $language_info);
      $this->languageTypesInfo = $language_info;
189
190
191
192
193
    }
    return $this->languageTypesInfo;
  }

  /**
194
   * {@inheritdoc}
195
   */
196
  public function saveLanguageTypesConfiguration(array $values) {
197
    $config = $this->configFactory->getEditable('language.types');
198
199
200
201
202
203
    if (isset($values['configurable'])) {
      $config->set('configurable', $values['configurable']);
    }
    if (isset($values['all'])) {
      $config->set('all', $values['all']);
    }
204
    $config->save(TRUE);
205
206
207
208
209
  }

  /**
   * {@inheritdoc}
   */
210
  public function getCurrentLanguage($type = LanguageInterface::TYPE_INTERFACE) {
211
212
213
214
215
    if (!isset($this->negotiatedLanguages[$type])) {
      // Ensure we have a valid value for this language type.
      $this->negotiatedLanguages[$type] = $this->getDefaultLanguage();

      if ($this->negotiator && $this->isMultilingual()) {
216
217
        if (!isset($this->initializing[$type])) {
          $this->initializing[$type] = TRUE;
218
219
220
          $negotiation = $this->negotiator->initializeType($type);
          $this->negotiatedLanguages[$type] = reset($negotiation);
          $this->negotiatedMethods[$type] = key($negotiation);
221
          unset($this->initializing[$type]);
222
223
224
225
226
227
228
        }
        // If the current interface language needs to be retrieved during
        // initialization we return the system language. This way string
        // translation calls happening during initialization will return the
        // original strings which can be translated by calling them again
        // afterwards. This can happen for instance while parsing negotiation
        // method definitions.
229
        elseif ($type == LanguageInterface::TYPE_INTERFACE) {
230
          return new Language(['id' => LanguageInterface::LANGCODE_SYSTEM]);
231
232
233
234
235
236
237
238
239
240
241
242
243
        }
      }
    }

    return $this->negotiatedLanguages[$type];
  }

  /**
   * {@inheritdoc}
   */
  public function reset($type = NULL) {
    if (!isset($type)) {
      $this->initialized = FALSE;
244
245
      $this->negotiatedLanguages = [];
      $this->negotiatedMethods = [];
246
247
      $this->languageTypes = NULL;
      $this->languageTypesInfo = NULL;
248
      $this->languages = [];
249
250
251
252
253
254
      if ($this->negotiator) {
        $this->negotiator->reset();
      }
    }
    elseif (isset($this->negotiatedLanguages[$type])) {
      unset($this->negotiatedLanguages[$type]);
255
      unset($this->negotiatedMethods[$type]);
256
    }
257
    return $this;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  }

  /**
   * {@inheritdoc}
   */
  public function getNegotiator() {
    return $this->negotiator;
  }

  /**
   * {@inheritdoc}
   */
  public function setNegotiator(LanguageNegotiatorInterface $negotiator) {
    $this->negotiator = $negotiator;
    $this->initialized = FALSE;
273
    $this->negotiatedLanguages = [];
274
275
276
277
278
  }

  /**
   * {@inheritdoc}
   */
279
  public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) {
280
281
282
283
284
285
286
    // If a config override is set, cache using that language's ID.
    if ($override_language = $this->getConfigOverrideLanguage()) {
      $static_cache_id = $override_language->getId();
    }
    else {
      $static_cache_id = $this->getCurrentLanguage()->getId();
    }
287

288
289
290
291
292
293
294
    if (!isset($this->languages[$static_cache_id][$flags])) {
      // Initialize the language list with the default language and default
      // locked languages. These cannot be removed. This serves as a fallback
      // list if this method is invoked while the language module is installed
      // and the configuration entities for languages are not yet fully
      // imported.
      $default = $this->getDefaultLanguage();
295
      $languages = [$default->getId() => $default];
296
297
298
299
300
301
302
303
304
305
306
307
308
      $languages += $this->getDefaultLockedLanguages($default->getWeight());

      // Load configurable languages on top of the defaults. Ideally this could
      // use the entity API to load and instantiate ConfigurableLanguage
      // objects. However the entity API depends on the language system, so that
      // would result in infinite loops. We use the configuration system
      // directly and instantiate runtime Language objects. When language
      // entities are imported those cover the default and locked languages, so
      // site-specific configuration will prevail over the fallback values.
      // Having them in the array already ensures if this is invoked in the
      // middle of importing language configuration entities, the defaults are
      // always present.
      $config_ids = $this->configFactory->listAll('language.entity.');
309
310
311
      foreach ($this->configFactory->loadMultiple($config_ids) as $config) {
        $data = $config->get();
        $data['name'] = $data['label'];
312
        $languages[$data['id']] = new Language($data);
313
      }
314
      Language::sort($languages);
315

316
317
      // Filter the full list of languages based on the value of $flags.
      $this->languages[$static_cache_id][$flags] = $this->filterLanguages($languages, $flags);
318
319
    }

320
    return $this->languages[$static_cache_id][$flags];
321
322
  }

323
324
325
326
  /**
   * {@inheritdoc}
   */
  public function getNativeLanguages() {
327
    $languages = $this->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
328
    $natives = [];
329
330
331
332
333
334
335
336
337
338
339
340

    $original_language = $this->getConfigOverrideLanguage();

    foreach ($languages as $langcode => $language) {
      $this->setConfigOverrideLanguage($language);
      $natives[$langcode] = ConfigurableLanguage::load($langcode);
    }
    $this->setConfigOverrideLanguage($original_language);
    Language::sort($natives);
    return $natives;
  }

341
342
343
344
  /**
   * {@inheritdoc}
   */
  public function updateLockedLanguageWeights() {
345
346
347
    // Get the weight of the last configurable language.
    $configurable_languages = $this->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
    $max_weight = end($configurable_languages)->getWeight();
348

349
    $locked_languages = $this->getLanguages(LanguageInterface::STATE_LOCKED);
350
351
352
353
354
355
356
357
358
359
    // Update locked language weights to maintain the existing order, if
    // necessary.
    if (reset($locked_languages)->getWeight() <= $max_weight) {
      foreach ($locked_languages as $language) {
        // Update system languages weight.
        $max_weight++;
        ConfigurableLanguage::load($language->getId())
          ->setWeight($max_weight)
          ->save();
      }
360
361
362
363
364
365
    }
  }

  /**
   * {@inheritdoc}
   */
366
  public function getFallbackCandidates(array $context = []) {
367
    if ($this->isMultilingual()) {
368
      $candidates = [];
369
370
371
      if (empty($context['operation']) || $context['operation'] != 'locale_lookup') {
        // If the fallback context is not locale_lookup, initialize the
        // candidates with languages ordered by weight and add
372
        // LanguageInterface::LANGCODE_NOT_SPECIFIED at the end. Interface
373
374
375
        // translation fallback should only be based on explicit configuration
        // gathered via the alter hooks below.
        $candidates = array_keys($this->getLanguages());
376
        $candidates[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
377
378
379
380
381
        $candidates = array_combine($candidates, $candidates);

        // The first candidate should always be the desired language if
        // specified.
        if (!empty($context['langcode'])) {
382
          $candidates = [$context['langcode'] => $context['langcode']] + $candidates;
383
        }
384
385
386
387
      }

      // Let other modules hook in and add/change candidates.
      $type = 'language_fallback_candidates';
388
      $types = [];
389
      if (!empty($context['operation'])) {
390
        $types[] = $type . '_' . $context['operation'];
391
392
393
394
395
      }
      $types[] = $type;
      $this->moduleHandler->alter($types, $candidates, $context);
    }
    else {
396
      $candidates = parent::getFallbackCandidates($context);
397
398
399
400
401
402
403
404
    }

    return $candidates;
  }

  /**
   * {@inheritdoc}
   */
405
  public function getLanguageSwitchLinks($type, Url $url) {
406
407
408
409
410
    if ($this->negotiator) {
      foreach ($this->negotiator->getNegotiationMethods($type) as $method_id => $method) {
        $reflector = new \ReflectionClass($method['class']);

        if ($reflector->implementsInterface('\Drupal\language\LanguageSwitcherInterface')) {
411
          $result = $this->negotiator->getNegotiationMethodInstance($method_id)->getLanguageSwitchLinks($this->requestStack->getCurrentRequest(), $type, $url);
412
413
414

          if (!empty($result)) {
            // Allow modules to provide translations for specific links.
415
            $this->moduleHandler->alter('language_switch_links', $result, $type, $url);
416
            $links = (object) ['links' => $result, 'method_id' => $method_id];
417
418
419
420
421
422
            break;
          }
        }
      }
    }

423
    return $links ?? NULL;
424
425
  }

426
  /**
427
428
429
430
431
432
   * Sets the configuration override language.
   *
   * @param \Drupal\Core\Language\LanguageInterface $language
   *   The language to override configuration with.
   *
   * @return $this
433
   */
434
  public function setConfigOverrideLanguage(LanguageInterface $language = NULL) {
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
    $this->configFactoryOverride->setLanguage($language);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigOverrideLanguage() {
    return $this->configFactoryOverride->getLanguage();
  }

  /**
   * {@inheritdoc}
   */
  public function getLanguageConfigOverride($langcode, $name) {
    return $this->configFactoryOverride->getOverride($langcode, $name);
  }

453
454
455
456
457
458
459
  /**
   * {@inheritdoc}
   */
  public function getLanguageConfigOverrideStorage($langcode) {
    return $this->configFactoryOverride->getStorage($langcode);
  }

460
461
462
  /**
   * {@inheritdoc}
   */
463
  public function getStandardLanguageListWithoutConfigured() {
464
465
466
467
468
469
470
    $languages = $this->getLanguages();
    $predefined = $this->getStandardLanguageList();
    foreach ($predefined as $key => $value) {
      if (isset($languages[$key])) {
        unset($predefined[$key]);
        continue;
      }
471
      $predefined[$key] = new TranslatableMarkup($value[0]);
472
    }
473
    natcasesort($predefined);
474
475
476
    return $predefined;
  }

477
478
479
480
481
482
483
484
485
  /**
   * {@inheritdoc}
   */
  public function getNegotiatedLanguageMethod($type = LanguageInterface::TYPE_INTERFACE) {
    if (isset($this->negotiatedLanguages[$type]) && isset($this->negotiatedMethods[$type])) {
      return $this->negotiatedMethods[$type];
    }
  }

486
}