class EntityTranslation

Allows accessing and updating translated entity fields.

Via this object translated entity fields may be read and updated in the same way as untranslatable entity fields on the entity object.

Hierarchy

Expanded class hierarchy of EntityTranslation

File

drupal/core/lib/Drupal/Core/Entity/Field/Type/EntityTranslation.php, line 25
Contains \Drupal\Core\Entity\Type\EntityTranslation.

Namespace

Drupal\Core\Entity\Field\Type
View source
class EntityTranslation extends TypedData implements IteratorAggregate, AccessibleInterface, ComplexDataInterface {

  /**
   * The array of translated fields, each being an instance of
   * \Drupal\Core\Entity\FieldInterface.
   *
   * @var array
   */
  protected $fields = array();

  /**
   * Whether the entity translation acts in strict mode.
   *
   * @var boolean
   */
  protected $strict = TRUE;

  /**
   * Returns whether the entity translation acts in strict mode.
   *
   * @return boolean
   *   Whether the entity translation acts in strict mode.
   */
  public function getStrictMode() {
    return $this->strict;
  }

  /**
   * Sets whether the entity translation acts in strict mode.
   *
   * @param boolean $strict
   *   Whether the entity translation acts in strict mode.
   *
   * @see \Drupal\Core\TypedData\TranslatableInterface::getTranslation()
   */
  public function setStrictMode($strict = TRUE) {
    $this->strict = $strict;
  }

  /**
   * Overrides \Drupal\Core\TypedData\TypedData::getValue().
   */
  public function getValue() {

    // The plain value of the translation is the array of translated field
    // objects.
    return $this->fields;
  }

  /**
   * Overrides \Drupal\Core\TypedData\TypedData::setValue().
   */
  public function setValue($values, $notify = TRUE) {

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

  /**
   * Overrides \Drupal\Core\TypedData\TypedData::getString().
   */
  public function getString() {
    $strings = array();
    foreach ($this
      ->getProperties() as $property) {
      $strings[] = $property
        ->getString();
    }
    return implode(', ', array_filter($strings));
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::get().
   */
  public function get($property_name) {
    $definitions = $this
      ->getPropertyDefinitions();
    if (!isset($definitions[$property_name])) {
      throw new InvalidArgumentException(format_string('Field @name is unknown or not translatable.', array(
        '@name' => $property_name,
      )));
    }
    return $this->fields[$property_name];
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::set().
   */
  public function set($property_name, $value, $notify = TRUE) {
    $this
      ->get($property_name)
      ->setValue($value, FALSE);
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getProperties().
   */
  public function getProperties($include_computed = FALSE) {
    $properties = array();
    foreach ($this
      ->getPropertyDefinitions() as $name => $definition) {
      if ($include_computed || empty($definition['computed'])) {
        $properties[$name] = $this
          ->get($name);
      }
    }
    return $properties;
  }

  /**
   * Magic method: Gets a translated field.
   */
  public function __get($name) {
    return $this
      ->get($name);
  }

  /**
   * Magic method: Sets a translated field.
   */
  public function __set($name, $value) {
    $this
      ->get($name)
      ->setValue($value);
  }

  /**
   * Implements \IteratorAggregate::getIterator().
   */
  public function getIterator() {
    return new ArrayIterator($this
      ->getProperties());
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinition().
   */
  public function getPropertyDefinition($name) {
    $definitions = $this
      ->getPropertyDefinitions();
    if (isset($definitions[$name])) {
      return $definitions[$name];
    }
    else {
      return FALSE;
    }
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
   */
  public function getPropertyDefinitions() {
    $definitions = array();
    foreach ($this->parent
      ->getPropertyDefinitions() as $name => $definition) {
      if (!empty($definition['translatable']) || !$this->strict) {
        $definitions[$name] = $definition;
      }
    }
    return $definitions;
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyValues().
   */
  public function getPropertyValues() {
    return $this
      ->getValue();
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::setPropertyValues().
   */
  public function setPropertyValues($values) {
    foreach ($values as $name => $value) {
      $this
        ->get($name)
        ->setValue($value);
    }
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::isEmpty().
   */
  public function isEmpty() {
    foreach ($this
      ->getProperties() as $property) {
      if ($property
        ->getValue() !== NULL) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Implements \Drupal\Core\TypedData\ComplexDataInterface::onChange().
   */
  public function onChange($property_name) {

    // Notify the parent of changes.
    if (isset($this->parent)) {
      $this->parent
        ->onChange($this->name);
    }
  }

  /**
   * Implements \Drupal\Core\TypedData\AccessibleInterface::access().
   */
  public function access($operation = 'view', AccountInterface $account = NULL) {

    // Determine the language code of this translation by cutting of the
    // leading "@" from the property name to get the langcode.
    // @todo Add a way to set and get the langcode so that's more obvious what
    // we're doing here.
    $langcode = substr($this
      ->getName(), 1);
    return \Drupal::entityManager()
      ->getAccessController($this->parent
      ->entityType())
      ->access($this->parent, $operation, $langcode, $account);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTranslation::$fields protected property The array of translated fields, each being an instance of \Drupal\Core\Entity\FieldInterface.
EntityTranslation::$strict protected property Whether the entity translation acts in strict mode.
EntityTranslation::access public function Implements \Drupal\Core\TypedData\AccessibleInterface::access(). Overrides AccessibleInterface::access
EntityTranslation::get public function Implements \Drupal\Core\TypedData\ComplexDataInterface::get(). Overrides ComplexDataInterface::get
EntityTranslation::getIterator public function Implements \IteratorAggregate::getIterator().
EntityTranslation::getProperties public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties
EntityTranslation::getPropertyDefinition public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition
EntityTranslation::getPropertyDefinitions public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions
EntityTranslation::getPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues
EntityTranslation::getStrictMode public function Returns whether the entity translation acts in strict mode.
EntityTranslation::getString public function Overrides \Drupal\Core\TypedData\TypedData::getString(). Overrides TypedData::getString
EntityTranslation::getValue public function Overrides \Drupal\Core\TypedData\TypedData::getValue(). Overrides TypedData::getValue
EntityTranslation::isEmpty public function Implements \Drupal\Core\TypedData\ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty
EntityTranslation::onChange public function Implements \Drupal\Core\TypedData\ComplexDataInterface::onChange(). Overrides ComplexDataInterface::onChange
EntityTranslation::set public function Implements \Drupal\Core\TypedData\ComplexDataInterface::set(). Overrides ComplexDataInterface::set
EntityTranslation::setPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues
EntityTranslation::setStrictMode public function Sets whether the entity translation acts in strict mode.
EntityTranslation::setValue public function Overrides \Drupal\Core\TypedData\TypedData::setValue(). Overrides TypedData::setValue
EntityTranslation::__get public function Magic method: Gets a translated field.
EntityTranslation::__set public function Magic method: Sets a translated field.
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