public function EntityNG::getTranslation

Implements TranslatableInterface::getTranslation().

Return value

\Drupal\Core\Entity\Field\Type\EntityTranslation

Overrides Entity::getTranslation

File

drupal/core/lib/Drupal/Core/Entity/EntityNG.php, line 219
Definition of Drupal\Core\Entity\EntityNG.

Class

EntityNG
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

public function getTranslation($langcode, $strict = TRUE) {

  // If the default language is LANGUAGE_NOT_SPECIFIED, the entity is not
  // translatable, so we use LANGUAGE_DEFAULT.
  if ($langcode == LANGUAGE_DEFAULT || in_array($this
    ->language()->langcode, array(
    LANGUAGE_NOT_SPECIFIED,
    $langcode,
  ))) {

    // No translation needed, return the entity.
    return $this;
  }

  // Check whether the language code is valid, thus is of an available
  // language.
  $languages = language_list(LANGUAGE_ALL);
  if (!isset($languages[$langcode])) {
    throw new InvalidArgumentException("Unable to get translation for the invalid language '{$langcode}'.");
  }
  $fields = array();
  foreach ($this
    ->getPropertyDefinitions() as $name => $definition) {

    // Load only translatable properties in strict mode.
    if (!empty($definition['translatable']) || !$strict) {
      $fields[$name] = $this
        ->getTranslatedField($name, $langcode);
    }
  }
  $translation_definition = array(
    'type' => 'entity_translation',
    'constraints' => array(
      'entity type' => $this
        ->entityType(),
      'bundle' => $this
        ->bundle(),
    ),
  );
  $translation = typed_data()
    ->create($translation_definition, $fields, array(
    'parent' => $this,
    'name' => $langcode,
  ));
  $translation
    ->setStrictMode($strict);
  return $translation;
}