3 namespace Drupal\language\Plugin\Condition;
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;
13 * Provides a 'Language' condition.
17 * label = @Translation("Language"),
19 * "language" = @ContextDefinition("language", label = @Translation("Language"))
23 class Language extends ConditionPluginBase implements ContainerFactoryPluginInterface {
26 * The Language manager.
28 * @var \Drupal\Core\Language\LanguageManagerInterface
30 protected $languageManager;
33 * Creates a new Language instance.
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.
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;
55 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57 $container->get('language_manager'),
67 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
68 if ($this->languageManager->isMultilingual()) {
70 $languages = $this->languageManager->getLanguages();
71 $langcodes_options = [];
72 foreach ($languages as $language) {
73 $langcodes_options[$language->getId()] = $language->getName();
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.'),
84 $form['langcodes'] = [
86 '#default_value' => $this->configuration['langcodes'],
89 return parent::buildConfigurationForm($form, $form_state);
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);
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();
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);
121 // If we have just one language just grab the only present value.
122 $languages = array_pop($language_names);
124 if (!empty($this->configuration['negate'])) {
125 return t('The language is not @languages.', ['@languages' => $languages]);
127 return t('The language is @languages.', ['@languages' => $languages]);
133 public function evaluate() {
134 if (empty($this->configuration['langcodes']) && !$this->isNegated()) {
138 $language = $this->getContextValue('language');
139 // Language visibility settings.
140 return !empty($this->configuration['langcodes'][$language->getId()]);
146 public function defaultConfiguration() {
147 return ['langcodes' => []] + parent::defaultConfiguration();