You are here

function entity_translation_translatable_batch in Entity Translation 7

Batch operation. Convert field data to or from LANGUAGE_NONE.

1 string reference to 'entity_translation_translatable_batch'
entity_translation_translatable_form_submit in ./entity_translation.admin.inc
Submit handler for the field settings form.

File

./entity_translation.admin.inc, line 622
The entity translation user interface.

Code

function entity_translation_translatable_batch($translatable, $field_name, $copy_all_languages, &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;

    // How many entities will need processing?
    $query = new EntityFieldQuery();
    $count = $query
      ->fieldCondition($field_name)
      ->age(FIELD_LOAD_REVISION)
      ->count()
      ->execute();
    if (intval($count) === 0) {

      // Nothing to do.
      $context['finished'] = 1;
      return;
    }
    $context['sandbox']['max'] = $count;
  }

  // Number of entities to be processed for each step.
  $limit = variable_get('entity_translation_translatable_batch_limit', 10);
  $offset = $context['sandbox']['progress'];
  $query = new EntityFieldQuery();
  $result = $query
    ->fieldCondition($field_name)
    ->entityOrderBy('entity_id')
    ->age(FIELD_LOAD_REVISION)
    ->range($offset, $limit)
    ->execute();
  foreach ($result as $entity_type => $partial_entities) {

    // Load all revisionable entities' revisions.
    if (EntityTranslationDefaultHandler::isEntityTypeRevisionable($entity_type)) {
      $entities = array();

      // Extract the revision identifier from the entity's definition.
      $entity_info = entity_get_info($entity_type);
      $revision_id_key = $entity_info['entity keys']['revision'];

      // Load each revisionable entity's revision using $conditions, which
      // should include the revision id information.
      foreach ($partial_entities as $partial_entity) {
        $conditions = (array) $partial_entity;
        $revision_condition = array_intersect_key($conditions, array(
          $revision_id_key => $revision_id_key,
        ));
        $entity_revisions = entity_load($entity_type, FALSE, $revision_condition);
        $entities[] = reset($entity_revisions);
      }
    }
    else {
      $entities = entity_load($entity_type, array_keys($partial_entities));
    }
    foreach ($entities as $entity) {
      $context['sandbox']['progress']++;
      $handler = entity_translation_get_handler($entity_type, $entity);
      $langcode = $handler
        ->getLanguage();

      // Skip process for language neutral entities.
      if ($langcode == LANGUAGE_NONE) {
        continue;
      }

      // We need a two-steps approach while updating field translations: given
      // that field-specific update functions might rely on the stored values to
      // perform their processing, see for instance file_field_update(), first
      // we need to store the new translations and only after we can remove the
      // old ones. Otherwise we might have data loss, since the removal of the
      // old translations might occur before the new ones are stored.
      if ($translatable && isset($entity->{$field_name}[LANGUAGE_NONE])) {

        // If the field is being switched to translatable and has data for
        // LANGUAGE_NONE then we need to move the data to the right language.
        // In case the 'Copy translations' option was selected, move the
        // available LANGUAGE_NONE field data into all existing translation
        // languages, otherwise only into the entity's language.
        $translations = $handler
          ->getTranslations();
        if ($copy_all_languages && !empty($translations->data)) {
          foreach ($translations->data as $translation) {
            $entity->{$field_name}[$translation['language']] = $entity->{$field_name}[LANGUAGE_NONE];
          }
        }
        else {
          $entity->{$field_name}[$langcode] = $entity->{$field_name}[LANGUAGE_NONE];
        }

        // Store the original value.
        _entity_translation_update_field($entity_type, $entity, $field_name);
        $entity->{$field_name}[LANGUAGE_NONE] = array();

        // Remove the language neutral value.
        _entity_translation_update_field($entity_type, $entity, $field_name);
      }
      elseif (!$translatable && isset($entity->{$field_name}[$langcode])) {

        // The field has been marked untranslatable and has data in the entity
        // language: we need to move it to LANGUAGE_NONE and drop the other
        // translations.
        $entity->{$field_name}[LANGUAGE_NONE] = $entity->{$field_name}[$langcode];

        // Store the original value.
        _entity_translation_update_field($entity_type, $entity, $field_name);

        // Remove translations.
        foreach ($entity->{$field_name} as $langcode => $items) {
          if ($langcode != LANGUAGE_NONE) {
            $entity->{$field_name}[$langcode] = array();
          }
        }
        _entity_translation_update_field($entity_type, $entity, $field_name);
      }
      else {

        // No need to save unchanged entities.
        continue;
      }
    }
  }
  $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}