Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / language / src / Plugin / Condition / Language.php
1 <?php
2
3 namespace Drupal\language\Plugin\Condition;
4
5 use Drupal\Core\Condition\ConditionPluginBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\Core\Language\LanguageManagerInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides a 'Language' condition.
14  *
15  * @Condition(
16  *   id = "language",
17  *   label = @Translation("Language"),
18  *   context = {
19  *     "language" = @ContextDefinition("language", label = @Translation("Language"))
20  *   }
21  * )
22  */
23 class Language extends ConditionPluginBase implements ContainerFactoryPluginInterface {
24
25   /**
26    * The Language manager.
27    *
28    * @var \Drupal\Core\Language\LanguageManagerInterface
29    */
30   protected $languageManager;
31
32   /**
33    * Creates a new Language instance.
34    *
35    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
36    *   The language manager.
37    * @param array $configuration
38    *   The plugin configuration, i.e. an array with configuration values keyed
39    *   by configuration option name. The special key 'context' may be used to
40    *   initialize the defined contexts by setting it to an array of context
41    *   values keyed by context names.
42    * @param string $plugin_id
43    *   The plugin_id for the plugin instance.
44    * @param mixed $plugin_definition
45    *   The plugin implementation definition.
46    */
47   public function __construct(LanguageManagerInterface $language_manager, array $configuration, $plugin_id, $plugin_definition) {
48     parent::__construct($configuration, $plugin_id, $plugin_definition);
49     $this->languageManager = $language_manager;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56     return new static(
57       $container->get('language_manager'),
58       $configuration,
59       $plugin_id,
60       $plugin_definition
61     );
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
68     if ($this->languageManager->isMultilingual()) {
69       // Fetch languages.
70       $languages = $this->languageManager->getLanguages();
71       $langcodes_options = [];
72       foreach ($languages as $language) {
73         $langcodes_options[$language->getId()] = $language->getName();
74       }
75       $form['langcodes'] = [
76         '#type' => 'checkboxes',
77         '#title' => $this->t('Language selection'),
78         '#default_value' => $this->configuration['langcodes'],
79         '#options' => $langcodes_options,
80         '#description' => $this->t('Select languages to enforce. If none are selected, all languages will be allowed.'),
81       ];
82     }
83     else {
84       $form['langcodes'] = [
85         '#type' => 'value',
86         '#default_value' => $this->configuration['langcodes'],
87       ];
88     }
89     return parent::buildConfigurationForm($form, $form_state);
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
96     $this->configuration['langcodes'] = array_filter($form_state->getValue('langcodes'));
97     parent::submitConfigurationForm($form, $form_state);
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function summary() {
104     $language_list = $this->languageManager->getLanguages(LanguageInterface::STATE_ALL);
105     $selected = $this->configuration['langcodes'];
106     // Reduce the language list to an array of language names.
107     $language_names = array_reduce($language_list, function (&$result, $item) use ($selected) {
108       // If the current item of the $language_list array is one of the selected
109       // languages, add it to the $results array.
110       if (!empty($selected[$item->getId()])) {
111         $result[$item->getId()] = $item->getName();
112       }
113       return $result;
114     }, []);
115
116     // If we have more than one language selected, separate them by commas.
117     if (count($this->configuration['langcodes']) > 1) {
118       $languages = implode(', ', $language_names);
119     }
120     else {
121       // If we have just one language just grab the only present value.
122       $languages = array_pop($language_names);
123     }
124     if (!empty($this->configuration['negate'])) {
125       return t('The language is not @languages.', ['@languages' => $languages]);
126     }
127     return t('The language is @languages.', ['@languages' => $languages]);
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function evaluate() {
134     if (empty($this->configuration['langcodes']) && !$this->isNegated()) {
135       return TRUE;
136     }
137
138     $language = $this->getContextValue('language');
139     // Language visibility settings.
140     return !empty($this->configuration['langcodes'][$language->getId()]);
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   public function defaultConfiguration() {
147     return ['langcodes' => []] + parent::defaultConfiguration();
148   }
149
150 }