Issue #1921550 by EclipseGc, larowlan: Create a Language check Condition.

8.0.x
Alex Pott 2013-04-14 21:56:35 +01:00
parent b36ca2848e
commit 4e833c9274
2 changed files with 231 additions and 0 deletions

View File

@ -0,0 +1,111 @@
<?php
/**
* @file
* Contains \Drupal\language\Plugin\Core\Condition\Language.
*/
namespace Drupal\language\Plugin\Core\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* 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(LANGUAGE_ALL);
$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(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;
}
}

View File

@ -0,0 +1,120 @@
<?php
/**
* @file
* Contains \Drupal\language\Tests\Condition\LanguageConditionTest.
*/
namespace Drupal\language\Tests\Condition;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Language\Language;
/**
* Tests the language condition.
*/
class LanguageConditionTest extends DrupalUnitTestBase {
/**
* The condition plugin manager.
*
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $manager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('system', 'language');
public static function getInfo() {
return array(
'name' => '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.');
}
}