Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Language / ContextProvider / CurrentLanguageContext.php
1 <?php
2
3 namespace Drupal\Core\Language\ContextProvider;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\Core\Plugin\Context\Context;
8 use Drupal\Core\Plugin\Context\ContextDefinition;
9 use Drupal\Core\Plugin\Context\ContextProviderInterface;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11
12 /**
13  * Sets the current language as a context.
14  */
15 class CurrentLanguageContext implements ContextProviderInterface {
16
17   use StringTranslationTrait;
18
19   /**
20    * The language manager.
21    *
22    * @var \Drupal\Core\Language\LanguageManagerInterface
23    */
24   protected $languageManager;
25
26   /**
27    * Constructs a new CurrentLanguageContext.
28    *
29    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
30    *   The language manager.
31    */
32   public function __construct(LanguageManagerInterface $language_manager) {
33     $this->languageManager = $language_manager;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function getRuntimeContexts(array $unqualified_context_ids) {
40     // Add a context for each language type.
41     $language_types = $this->languageManager->getLanguageTypes();
42     $info = $this->languageManager->getDefinedLanguageTypesInfo();
43
44     if ($unqualified_context_ids) {
45       foreach ($unqualified_context_ids as $unqualified_context_id) {
46         if (array_search($unqualified_context_id, $language_types) === FALSE) {
47           unset($language_types[$unqualified_context_id]);
48         }
49       }
50     }
51
52     $result = [];
53     foreach ($language_types as $type_key) {
54       if (isset($info[$type_key]['name'])) {
55         $context = new Context(new ContextDefinition('language', $info[$type_key]['name']), $this->languageManager->getCurrentLanguage($type_key));
56
57         $cacheability = new CacheableMetadata();
58         $cacheability->setCacheContexts(['languages:' . $type_key]);
59         $context->addCacheableDependency($cacheability);
60
61         $result[$type_key] = $context;
62       }
63     }
64
65     return $result;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getAvailableContexts() {
72     return $this->getRuntimeContexts([]);
73   }
74
75 }