class EntityTranslation

Makes translated entity properties available via the Field API.

Hierarchy

Expanded class hierarchy of EntityTranslation

File

drupal/core/lib/Drupal/Core/Entity/Field/Type/EntityTranslation.php, line 22
Definition of Drupal\Core\Entity\Type\EntityTranslation.

Namespace

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

  /**
   * The array of translated properties, each being an instance of
   * FieldInterface.
   *
   * @var array
   */
  protected $properties = array();

  /**
   * The language code of the translation.
   *
   * @var string
   */
  protected $langcode;

  /**
   * The parent entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $parent;

  /**
   * 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;
  }

  /**
   * Implements ContextAwareInterface::getName().
   */
  public function getName() {

    // The name of the translation is the language code.
    return $this->langcode;
  }

  /**
   * Implements ContextAwareInterface::setName().
   */
  public function setName($name) {

    // The name of the translation is the language code.
    $this->langcode = $name;
  }

  /**
   * Implements ContextAwareInterface::getParent().
   *
   * @return \Drupal\Core\Entity\EntityInterface
   */
  public function getParent() {
    return $this->parent;
  }

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

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

    // The value of the translation is the array of translated property objects.
    return $this->properties;
  }

  /**
   * Implements TypedDataInterface::setValue().
   */
  public function setValue($values) {
    $this->properties = $values;
  }

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

  /**
   * Implements TypedDataInterface::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->properties[$property_name];
  }

  /**
   * Implements ComplexDataInterface::set().
   */
  public function set($property_name, $value) {
    $this
      ->get($property_name)
      ->setValue($value);
  }

  /**
   * Implements 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 getter: Gets the translated property.
   */
  public function __get($name) {
    return $this
      ->get($name);
  }

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

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

  /**
   * Implements ComplexDataInterface::getPropertyDefinition().
   */
  public function getPropertyDefinition($name) {
    $definitions = $this
      ->getPropertyDefinitions();
    return isset($definitions[$name]) ? $definitions[$name] : FALSE;
  }

  /**
   * Implements 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 ComplexDataInterface::getPropertyValues().
   */
  public function getPropertyValues() {
    return $this
      ->getValue();
  }

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

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

  /**
   * Implements AccessibleInterface::access().
   */
  public function access($operation = 'view', \Drupal\user\Plugin\Core\Entity\User $account = NULL) {
    $method = $operation . 'Access';
    return entity_access_controller($this->parent
      ->entityType())
      ->{$method}($this->parent, $this->langcode, $account);
  }

  /**
   * Implements TypedDataInterface::validate().
   */
  public function validate($value = NULL) {

    // @todo implement
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTranslation::$langcode protected property The language code of the translation.
EntityTranslation::$parent protected property The parent entity.
EntityTranslation::$properties protected property The array of translated properties, each being an instance of FieldInterface.
EntityTranslation::$strict protected property Whether the entity translation acts in strict mode.
EntityTranslation::access public function Implements AccessibleInterface::access(). Overrides AccessibleInterface::access
EntityTranslation::get public function Implements TypedDataInterface::get(). Overrides ComplexDataInterface::get
EntityTranslation::getIterator public function Implements IteratorAggregate::getIterator().
EntityTranslation::getName public function Implements ContextAwareInterface::getName(). Overrides ContextAwareInterface::getName
EntityTranslation::getParent public function Implements ContextAwareInterface::getParent(). Overrides ContextAwareInterface::getParent
EntityTranslation::getProperties public function Implements ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties
EntityTranslation::getPropertyDefinition public function Implements ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition
EntityTranslation::getPropertyDefinitions public function Implements ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions
EntityTranslation::getPropertyValues public function Implements ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues
EntityTranslation::getStrictMode public function Returns whether the entity translation acts in strict mode.
EntityTranslation::getString public function Implements TypedDataInterface::getString(). Overrides TypedData::getString
EntityTranslation::getValue public function Implements TypedDataInterface::getValue(). Overrides TypedData::getValue
EntityTranslation::isEmpty public function Implements ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty
EntityTranslation::set public function Implements ComplexDataInterface::set(). Overrides ComplexDataInterface::set
EntityTranslation::setName public function Implements ContextAwareInterface::setName(). Overrides ContextAwareInterface::setName
EntityTranslation::setParent public function Implements ContextAwareInterface::setParent(). Overrides ContextAwareInterface::setParent
EntityTranslation::setPropertyValues public function Implements ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues
EntityTranslation::setStrictMode public function Sets whether the entity translation acts in strict mode.
EntityTranslation::setValue public function Implements TypedDataInterface::setValue(). Overrides TypedData::setValue
EntityTranslation::validate public function Implements TypedDataInterface::validate(). Overrides TypedDataInterface::validate
EntityTranslation::__get public function Magic getter: Gets the translated property.
EntityTranslation::__set public function Magic getter: Sets the translated property.
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