class Language

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

Defines the 'language' data type.

The plain value of a language is the language object, i.e. an instance of \Drupal\Core\Language\Language. For setting the value the language object or the language code as string may be passed.

Plugin annotation


@DataType(
  id = "language",
  label = @Translation("Language"),
  description = @Translation("A language object.")
)

Hierarchy

Expanded class hierarchy of Language

393 string references to 'Language'
AccountSettingsForm::buildForm in core/modules/user/src/AccountSettingsForm.php
block.block.umami_languageswitcher.yml in core/profiles/demo_umami/config/install/block.block.umami_languageswitcher.yml
core/profiles/demo_umami/config/install/block.block.umami_languageswitcher.yml
BlockContentTypeForm::form in core/modules/block_content/src/BlockContentTypeForm.php
BlockForm::buildVisibilityInterface in core/modules/block/src/BlockForm.php
Helper function for building the visibility UI form.
BlockLanguageTest::testLanguageBlockVisibilityLanguageDelete in core/modules/block/tests/src/Functional/BlockLanguageTest.php
Tests if the visibility settings are removed if the language is deleted.

... See full list

File

core/lib/Drupal/Core/TypedData/Plugin/DataType/Language.php, line 20

Namespace

Drupal\Core\TypedData\Plugin\DataType
View source
class Language extends TypedData {
  
  /**
   * The id of the language.
   *
   * @var string
   */
  protected $id;
  
  /**
   * @var \Drupal\Core\Language\Language
   */
  protected $language;
  
  /**
   * Overrides TypedData::getValue().
   *
   * @return \Drupal\Core\Language\LanguageInterface|null
   */
  public function getValue() {
    if (!isset($this->language) && $this->id) {
      $this->language = \Drupal::languageManager()->getLanguage($this->id);
    }
    return $this->language;
  }
  
  /**
   * Overrides TypedData::setValue().
   *
   * Both the langcode and the language object may be passed as value.
   */
  public function setValue($value, $notify = TRUE) {
    // Support passing language objects.
    if (is_object($value)) {
      $this->id = $value->getId();
      $this->language = $value;
    }
    elseif (isset($value) && !is_scalar($value)) {
      throw new \InvalidArgumentException('Value is no valid langcode or language object.');
    }
    else {
      $this->id = $value;
      $this->language = NULL;
    }
    // Notify the parent of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent
        ->onChange($this->name);
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function getString() {
    $language = $this->getValue();
    return $language ? $language->getName() : '';
  }
  
  /**
   * {@inheritdoc}
   */
  public function id() {
    if (isset($this->id)) {
      return $this->id;
    }
    elseif (isset($this->language)) {
      return $this->language
        ->getId();
    }
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.