You are here

public function EntityTranslationDefaultHandler::setTranslation in Entity Translation 7

Overrides EntityTranslationHandlerInterface::setTranslation

See also

EntityTranslationHandlerInterface::setTranslation()

2 calls to EntityTranslationDefaultHandler::setTranslation()
EntityTranslationDefaultHandler::entityFormSubmit in includes/translation.handler.inc
EntityTranslationDefaultHandler::initTranslations in includes/translation.handler.inc

File

includes/translation.handler.inc, line 676
Default translation handler for the translation module.

Class

EntityTranslationDefaultHandler
Class implementing the default entity translation behaviours.

Code

public function setTranslation($translation, $values = NULL) {
  $args = func_get_args();
  if (isset($translation['source']) && $translation['language'] == $translation['source']) {
    throw new Exception('Invalid translation language');
  }
  $translations = $this
    ->getTranslations();
  $langcode = $translation['language'];
  $this
    ->setTranslating(TRUE);
  if (isset($translations->data[$langcode])) {
    $translation = array_merge($translations->data[$langcode], $translation);
    $translation['changed'] = REQUEST_TIME;

    // If a newly inserted translation has not been stored yet do not fire an
    // update hook.
    $hook = empty($translations->hook[$langcode]['hook']) ? 'update' : $translations->hook[$langcode]['hook'];
  }
  else {
    $hook = 'insert';
  }

  // Store the translation data.
  $translations->data[$langcode] = $translation;

  // Keep track that the translation has been inserted or updated.
  $translations->hook[$langcode] = array(
    'hook' => $hook,
    'data' => $values,
  );
  if (is_array($values)) {

    // Update field translations.
    foreach (field_info_instances($this->entityType, $this->bundle) as $instance) {
      $field_name = $instance['field_name'];
      $field = field_info_field($field_name);
      if ($field['translatable'] && isset($values[$field_name][$langcode])) {
        $this->entity->{$field_name}[$langcode] = $values[$field_name][$langcode];
      }
    }
  }
  $this
    ->notifyChildren(__FUNCTION__, $args);
}