diff --git a/core/modules/language/lib/Drupal/language/Plugin/Core/Condition/Language.php b/core/modules/language/lib/Drupal/language/Plugin/Core/Condition/Language.php new file mode 100644 index 00000000000..70ec6ef8b5c --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Plugin/Core/Condition/Language.php @@ -0,0 +1,111 @@ +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(LANGUAGE_ALL); + $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; + } + +} diff --git a/core/modules/language/lib/Drupal/language/Tests/Condition/LanguageConditionTest.php b/core/modules/language/lib/Drupal/language/Tests/Condition/LanguageConditionTest.php new file mode 100644 index 00000000000..e98a8d375cb --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Tests/Condition/LanguageConditionTest.php @@ -0,0 +1,120 @@ + 'Language Condition Plugin', + 'description' => 'Tests that the language condition, provided by the language module, is working properly.', + 'group' => 'Condition API', + ); + } + + protected function setUp() { + parent::setUp(); + + $this->installSchema('language', 'language'); + // This is needed for language_default(). + // @todo remove this when language_default() no longer needs variable_get(). + $this->installSchema('system', 'variable'); + + // Setup English. + language_save(language_default()); + + // Setup Italian. + $language = new Language(array( + 'langcode' => 'it', + 'name' => 'Italian', + 'direction' => '0', + )); + language_save($language); + + $this->manager = $this->container->get('plugin.manager.condition'); + } + + /** + * Test the language condition. + */ + public function testConditions() { + // Grab the language condition and configure it to check the content + // language. + $language = language_load('en'); + $condition = $this->manager->createInstance('language') + ->setConfig('langcodes', array('en' => 'en', 'it' => 'it')) + ->setContextValue('language', $language); + $this->assertTrue($condition->execute(), 'Language condition passes as expected.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The language is English, Italian.'); + + // Change to Italian only. + $condition->setConfig('langcodes', array('it' => 'it')); + $this->assertFalse($condition->execute(), 'Language condition fails as expected.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The language is Italian.'); + + // Negate the condition + $condition->setConfig('negate', TRUE); + $this->assertTrue($condition->execute(), 'Language condition passes as expected.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The language is not Italian.'); + + // Change the default language to Italian. + $language = language_load('it'); + + $condition = $this->manager->createInstance('language') + ->setConfig('langcodes', array('en' => 'en', 'it' => 'it')) + ->setContextValue('language', $language); + + $this->assertTrue($condition->execute(), 'Language condition passes as expected.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The language is English, Italian.'); + + // Change to Italian only. + $condition->setConfig('langcodes', array('it' => 'it')); + $this->assertTrue($condition->execute(), 'Language condition passes as expected.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The language is Italian.'); + + // Negate the condition + $condition->setConfig('negate', TRUE); + $this->assertFalse($condition->execute(), 'Language condition fails as expected.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The language is not Italian.'); + } + +}