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

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/lib/Drupal/Core/TypedData/Type/Language.php, line 28
Contains \Drupal\Core\TypedData\Type\Language.

Namespace

Drupal\Core\TypedData\Type
View source
class Language extends TypedData {

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

  /**
   * Overrides TypedData::getValue().
   */
  public function getValue() {
    if (!empty($this->definition['settings']['langcode source'])) {
      $this->langcode = $this->parent
        ->__get($this->definition['settings']['langcode source']);
    }
    if ($this->langcode) {
      $language = language_load($this->langcode);
      return $language ?: new LanguageObject(array(
        'langcode' => $this->langcode,
      ));
    }
  }

  /**
   * 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)) {
      $value = $value->langcode;
    }
    elseif (isset($value) && !is_scalar($value)) {
      throw new InvalidArgumentException('Value is no valid langcode or language object.');
    }

    // Update the 'langcode source' property, if given.
    if (!empty($this->definition['settings']['langcode source'])) {
      $this->parent
        ->__set($this->definition['settings']['langcode source'], $value, $notify);
    }
    else {

      // Notify the parent of any changes to be made.
      if ($notify && isset($this->parent)) {
        $this->parent
          ->onChange($this->name);
      }
      $this->langcode = $value;
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
Language::$langcode protected property The language code of the language if no 'langcode source' is used.
Language::getString public function Overrides TypedData::getString(). Overrides TypedData::getString
Language::getValue public function Overrides TypedData::getValue(). Overrides TypedData::getValue
Language::setValue public function Overrides TypedData::setValue(). Overrides TypedData::setValue
TypedData::$definition protected property The data definition.
TypedData::$name protected property The property name.
TypedData::$parent protected property The parent typed data object.
TypedData::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints 2
TypedData::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
TypedData::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
TypedData::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
TypedData::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
TypedData::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
TypedData::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType
TypedData::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext 1
TypedData::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 3
TypedData::__construct public function Constructs a TypedData object given its definition and context. 5