You are here

public function EntityTranslationDefaultHandler::saveTranslations in Entity Translation 7

Overrides EntityTranslationHandlerInterface::saveTranslations

See also

EntityTranslationHandlerInterface::saveTranslations()

File

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

Class

EntityTranslationDefaultHandler
Class implementing the default entity translation behaviours.

Code

public function saveTranslations() {
  $translations = $this
    ->getTranslations();

  // Save current values.
  $this
    ->doSaveTranslations($translations, 'entity_translation');

  // Save revision values.
  if ($this
    ->isRevisionable()) {
    $this
      ->doSaveTranslations($translations, 'entity_translation_revision', TRUE);
  }

  // The translation handler interface decouples operations on translations at
  // data structure level from CRUD operations. Hence hooks must be fired
  // after changes are actually persisted.
  if (!empty($translations->hook)) {

    // Hook info is keyed by language code so that subsequent operations at
    // data structure level do not cause multiple hooks for the same data to
    // be fired. For instance if a translation is first updated and then
    // deleted, only the 'delete' hook should be fired, because it is the only
    // change that has actually been persisted.
    foreach ($translations->hook as $langcode => $info) {
      $translation = isset($translations->data[$langcode]) ? $translations->data[$langcode] : $langcode;
      $data = isset($info['data']) ? $info['data'] : NULL;
      module_invoke_all('entity_translation_' . $info['hook'], $this->entityType, $this->entity, $translation, $data);

      // Provide Rules events integration if available.
      if (module_exists('rules')) {

        // Pass the entity as a wrapped one since rules can't do it for us
        // when using the variable type 'entity'.
        rules_invoke_event('entity_translation_' . $info['hook'], $this->entityType, entity_metadata_wrapper($this->entityType, $this->entity), $translation, $data);
      }
    }

    // Avoid firing hooks more than once for the same changes.
    $translations->hook = array();
  }
}