Newer
Older
<?php
/**
* @file
* Contains \Drupal\language\Plugin\Condition\Language.
*/
namespace Drupal\language\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Language\Language as Lang;
/**
* Provides a 'Language' condition.
*
* @Plugin(
* id = "language",
* label = @Translation("Language"),
* module = "language",
* context = {
* "language" = {
* "type" = "language"
* }
* }
* )
*/
class Language extends ConditionPluginBase {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form = parent::buildForm($form, $form_state);
if (language_multilingual()) {
// Fetch languages.
$languages = language_list(Lang::STATE_ALL);
39
40
41
42
43
44
45
46
47
48
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
$langcodes_options = array();
foreach ($languages as $language) {
// @todo $language->name is not wrapped with t(), it should be replaced
// by CMI translation implementation.
$langcodes_options[$language->langcode] = $language->name;
}
$form['langcodes'] = array(
'#type' => 'checkboxes',
'#title' => t('Language selection'),
'#default_value' => !empty($this->configuration['langcodes']) ? $this->configuration['langcodes'] : array(),
'#options' => $langcodes_options,
'#description' => t('Select languages to enforce. If none are selected, all languages will be allowed.'),
);
}
else {
$form['language']['langcodes'] = array(
'#type' => 'value',
'#value' => !empty($this->configuration['langcodes']) ? $this->configuration['langcodes'] : array()
);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$this->configuration['langcodes'] = array_filter($form_state['values']['langcodes']);
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function summary() {
$language_list = language_list(Lang::STATE_ALL);
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
$selected = $this->configuration['langcodes'];
// Reduce the language list to an array of language names.
$language_names = array_reduce($language_list, function(&$result, $item) use ($selected) {
// If the current item of the $language_list array is one of the selected
// languages, add it to the $results array.
if (!empty($selected[$item->langcode])) {
$result[$item->langcode] = $item->name;
}
return $result;
}, array());
// If we have more than one language selected, separate them by commas.
if (count($this->configuration['langcodes']) > 1) {
$languages = implode(', ', $language_names);
}
else {
// If we have just one language just grab the only present value.
$languages = array_pop($language_names);
}
if (!empty($this->configuration['negate'])) {
return t('The language is not @languages.', array('@languages' => $languages));
}
return t('The language is @languages.', array('@languages' => $languages));
}
/**
* {@inheritdoc}
*/
public function evaluate() {
$language = $this->getContextValue('language');
// Language visibility settings.
if (!empty($this->configuration['langcodes'])) {
return !empty($this->configuration['langcodes'][$language->langcode]);
}
return TRUE;
}
}