class 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.

Optionally, this class may be used as computed property, see the supported settings below. E.g., it is used as 'language' property of language items.

Supported settings (below the definition's 'settings' key) are:

  • langcode source: If used as computed property, the langcode property used to load the language object.

Hierarchy

Expanded class hierarchy of Language

34 string references to 'Language'
comment_views_data in drupal/core/modules/comment/comment.views.inc
Implements hook_views_data().
EntityTestFormController::form in drupal/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestFormController.php
Overrides Drupal\Core\Entity\EntityFormController::form().
LanguageBrowserDetectionUnitTest::getInfo in drupal/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php
LanguageConfigurationElementTest::getInfo in drupal/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationElementTest.php
LanguageConfigurationTest::getInfo in drupal/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationTest.php

... See full list

File

drupal/core/lib/Drupal/Core/TypedData/Type/Language.php, line 28
Definition of Drupal\Core\TypedData\Type\Language.

Namespace

Drupal\Core\TypedData\Type
View source
class Language extends TypedData implements TypedDataInterface, ContextAwareInterface {

  /**
   * The name.
   *
   * @var string
   */
  protected $name;

  /**
   * The parent data structure.
   *
   * @var mixed
   */
  protected $parent;

  /**
   * The language code of the language if no 'langcode source' is used.
   *
   * @var string
   */
  protected $langcode;

  /**
   * Implements ContextAwareInterface::getName().
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Implements ContextAwareInterface::setName().
   */
  public function setName($name) {
    $this->name = $name;
  }

  /**
   * Implements ContextAwareInterface::getParent().
   */
  public function getParent() {
    return $this->parent;
  }

  /**
   * Implements ContextAwareInterface::setParent().
   */
  public function setParent($parent) {
    $this->parent = $parent;
  }

  /**
   * Implements TypedDataInterface::getValue().
   */
  public function getValue() {
    $source = $this
      ->getLanguageCodeSource();
    $langcode = $source ? $source
      ->getValue() : $this->langcode;
    return $langcode ? language_load($langcode) : NULL;
  }

  /**
   * Helper to get the typed data object holding the source language code.
   *
   * @return \Drupal\Core\TypedData\TypedDataInterface|FALSE
   */
  protected function getLanguageCodeSource() {
    return !empty($this->definition['settings']['langcode source']) ? $this->parent
      ->get($this->definition['settings']['langcode source']) : FALSE;
  }

  /**
   * Implements TypedDataInterface::setValue().
   *
   * Both the langcode and the language object may be passed as value.
   */
  public function setValue($value) {

    // Support passing language objects.
    if (is_object($value)) {
      $value = $value->langcode;
    }
    elseif (isset($value) && !is_scalar($value)) {
      throw new InvalidArgumentException('Value is no valid langcode or language object.');
    }
    $source = $this
      ->getLanguageCodeSource();
    if ($source) {
      $source
        ->setValue($value);
    }
    else {
      $this->langcode = $value;
    }
  }

  /**
   * Implements TypedDataInterface::getString().
   */
  public function getString() {
    $language = $this
      ->getValue();
    return $language ? $language->name : '';
  }

  /**
   * Implements TypedDataInterface::validate().
   */
  public function validate() {

    // TODO: Implement validate() method.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Language::$langcode protected property The language code of the language if no 'langcode source' is used.
Language::$name protected property The name.
Language::$parent protected property The parent data structure.
Language::getLanguageCodeSource protected function Helper to get the typed data object holding the source language code.
Language::getName public function Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface::getName
Language::getParent public function Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface::getParent
Language::getString public function Implements TypedDataInterface::getString(). Overrides TypedData::getString
Language::getValue public function Implements TypedDataInterface::getValue(). Overrides TypedData::getValue
Language::setName public function Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface::setName
Language::setParent public function Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface::setParent
Language::setValue public function Implements TypedDataInterface::setValue(). Overrides TypedData::setValue
Language::validate public function Implements TypedDataInterface::validate(). Overrides TypedDataInterface::validate
TypedData::$definition protected property The data definition.
TypedData::getDefinition public function Implements TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getType public function Implements TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::__construct public function Constructs a TypedData object given its definition. 3