You are here

public function Entity::getTranslation in Entity API 7

Gets the raw, translated value of a property or field.

Supports retrieving field translations as well as i18n string translations.

Note that this returns raw data values, which might not reflect what has been declared for hook_entity_property_info() as no 'getter callbacks' are invoked or no referenced entities are loaded. For retrieving values reflecting the property info make use of entity metadata wrappers, see entity_metadata_wrapper().

@todo Implement an analogous setTranslation() method for updating.

Parameters

$property: The name of the property to return; e.g., 'title'.

$langcode: (optional) The language code of the language to which the value should be translated. If set to NULL, the default display language is being used.

Return value

string The raw, translated property value; or the raw, un-translated value if no translation is available.

Overrides EntityInterface::getTranslation

1 call to Entity::getTranslation()
Entity::defaultLabel in includes/entity.inc
Defines the entity label if the 'entity_class_label' callback is used.

File

includes/entity.inc, line 371
Provides a base class for entities.

Class

Entity
A common class for entities.

Code

public function getTranslation($property, $langcode = NULL) {
  $all_info = entity_get_all_property_info($this->entityType);

  // Assign by reference to avoid triggering notices if metadata is missing.
  $property_info =& $all_info[$property];
  if (!empty($property_info['translatable'])) {
    if (!empty($property_info['field'])) {
      return field_get_items($this->entityType, $this, $property, $langcode);
    }
    elseif (!empty($property_info['i18n string'])) {
      $name = $this->entityInfo['module'] . ':' . $this->entityType . ':' . $this
        ->identifier() . ':' . $property;
      return entity_i18n_string($name, $this->{$property}, $langcode);
    }
  }
  return $this->{$property};
}