You are here

class Language in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Language/Language.php \Drupal\Core\Language\Language
  2. 10 core/modules/ckeditor/src/Plugin/CKEditorPlugin/Language.php \Drupal\ckeditor\Plugin\CKEditorPlugin\Language
  3. 10 core/modules/language/src/Plugin/Condition/Language.php \Drupal\language\Plugin\Condition\Language
  4. 10 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Language.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Language
  5. 10 core/lib/Drupal/Core/TypedData/Plugin/DataType/Language.php \Drupal\Core\TypedData\Plugin\DataType\Language
  6. 10 core/modules/language/src/Plugin/migrate/source/Language.php \Drupal\language\Plugin\migrate\source\Language
Same name and namespace in other branches
  1. 8 core/modules/language/src/Plugin/Condition/Language.php \Drupal\language\Plugin\Condition\Language
  2. 9 core/modules/language/src/Plugin/Condition/Language.php \Drupal\language\Plugin\Condition\Language

Provides a 'Language' condition.

Plugin annotation


@Condition(
  id = "language",
  label = @Translation("Language"),
  context_definitions = {
    "language" = @ContextDefinition("language", label = @Translation("Language"))
  }
)

Hierarchy

Expanded class hierarchy of Language

45 string references to 'Language'
ckeditor.schema.yml in core/modules/ckeditor/config/schema/ckeditor.schema.yml
core/modules/ckeditor/config/schema/ckeditor.schema.yml
ckeditor5.ckeditor5.yml in core/modules/ckeditor5/ckeditor5.ckeditor5.yml
core/modules/ckeditor5/ckeditor5.ckeditor5.yml
ckeditor5.schema.yml in core/modules/ckeditor5/config/schema/ckeditor5.schema.yml
core/modules/ckeditor5/config/schema/ckeditor5.schema.yml
ConfigTestForm::form in core/modules/config/tests/config_test/src/ConfigTestForm.php
ConfigTranslationController::itemPage in core/modules/config_translation/src/Controller/ConfigTranslationController.php
Language translations overview page for a configuration name.

... See full list

File

core/modules/language/src/Plugin/Condition/Language.php, line 23

Namespace

Drupal\language\Plugin\Condition
View source
class Language extends ConditionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The Language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Creates a new Language instance.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   */
  public function __construct(LanguageManagerInterface $language_manager, array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($container
      ->get('language_manager'), $configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    if ($this->languageManager
      ->isMultilingual()) {

      // Fetch languages.
      $languages = $this->languageManager
        ->getLanguages();
      $langcodes_options = [];
      foreach ($languages as $language) {
        $langcodes_options[$language
          ->getId()] = $language
          ->getName();
      }
      $form['langcodes'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Language selection'),
        '#default_value' => $this->configuration['langcodes'],
        '#options' => $langcodes_options,
        '#description' => $this
          ->t('Select languages to enforce. If none are selected, all languages will be allowed.'),
      ];
    }
    else {
      $form['langcodes'] = [
        '#type' => 'value',
        '#default_value' => $this->configuration['langcodes'],
      ];
    }
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['langcodes'] = array_filter($form_state
      ->getValue('langcodes'));
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function summary() {
    $language_list = $this->languageManager
      ->getLanguages(LanguageInterface::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
        ->getId()])) {
        $result[$item
          ->getId()] = $item
          ->getName();
      }
      return $result;
    }, []);

    // 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.', [
        '@languages' => $languages,
      ]);
    }
    return t('The language is @languages.', [
      '@languages' => $languages,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate() {
    if (empty($this->configuration['langcodes']) && !$this
      ->isNegated()) {
      return TRUE;
    }
    $language = $this
      ->getContextValue('language');

    // Language visibility settings.
    return !empty($this->configuration['langcodes'][$language
      ->getId()]);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'langcodes' => [],
    ] + parent::defaultConfiguration();
  }

}

Members