class Language

Provides a 'Language' condition.

Plugin annotation


@Plugin(
  id = "language",
  label = @Translation("Language"),
  module = "language",
  context = {
    "language" = {
      "type" = "language"
    }
  }
)

Hierarchy

Expanded class hierarchy of Language

48 string references to 'Language'
breakpoint.schema.yml in drupal/core/modules/breakpoint/config/schema/breakpoint.schema.yml
drupal/core/modules/breakpoint/config/schema/breakpoint.schema.yml
comment_views_data in drupal/core/modules/comment/comment.views.inc
Implements hook_views_data().
CustomBlockFormController::form in drupal/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php
Overrides \Drupal\Core\Entity\EntityFormController::form().
editor.schema.yml in drupal/core/modules/editor/config/schema/editor.schema.yml
drupal/core/modules/editor/config/schema/editor.schema.yml
EntityTestFormController::form in drupal/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestFormController.php
Overrides Drupal\Core\Entity\EntityFormController::form().

... See full list

File

drupal/core/modules/language/lib/Drupal/language/Plugin/Condition/Language.php, line 29
Contains \Drupal\language\Plugin\Condition\Language.

Namespace

Drupal\language\Plugin\Condition
View source
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);
      $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);
    $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionPluginBase::execute public function Implements \Drupal\Core\Executable\ExecutablePluginBase::execute(). Overrides ExecutableInterface::execute
ConditionPluginBase::getFormID public function Implements \Drupal\Core\Form\FormInterface::getFormID(). Overrides FormInterface::getFormID
ConditionPluginBase::isNegated public function Implements \Drupal\condition\Plugin\ConditionInterface::isNegated(). Overrides ConditionInterface::isNegated
ConditionPluginBase::validateForm public function Implements \Drupal\Core\Form\FormInterface::validateForm(). Overrides FormInterface::validateForm 1
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::getContext public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContext(). Overrides ContextAwarePluginInterface::getContext
ContextAwarePluginBase::getContextDefinition public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextDefinition(). Overrides ContextAwarePluginInterface::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextDefinitions(). Overrides ContextAwarePluginInterface::getContextDefinitions
ContextAwarePluginBase::getContexts public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContexts(). Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValue(). Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValues(). Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContextValue public function Override of \Drupal\Component\Plugin\ContextAwarePluginBase::setContextValue(). Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::valdidateContexts(). Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__construct public function Override of \Drupal\Component\Plugin\ContextAwarePluginBase::__construct(). Overrides ContextAwarePluginBase::__construct
ExecutablePluginBase::$executableManager protected property The condition manager to proxy execute calls through.
ExecutablePluginBase::getConfig public function Gets all configuration values.
ExecutablePluginBase::getConfigDefinition public function Gets the definition of a configuration option.
ExecutablePluginBase::getConfigDefinitions public function Gets an array of definitions of available configuration options.
ExecutablePluginBase::setConfig public function Sets the value of a particular configuration option.
ExecutablePluginBase::setExecutableManager public function
Language::buildForm public function Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides ConditionPluginBase::buildForm
Language::evaluate public function Evaluates the condition and returns TRUE or FALSE accordingly. Overrides ConditionInterface::evaluate
Language::submitForm public function Implements \Drupal\Core\Form\FormInterface::submitForm(). Overrides ConditionPluginBase::submitForm
Language::summary public function Provides a human readable summary of the condition's configuration. Overrides ConditionInterface::summary
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId