You are here

function lingotek_field_language_data_cleanup_batch_create in Lingotek Translation 7.5

Same name and namespace in other branches
  1. 7.7 lingotek.batch.inc \lingotek_field_language_data_cleanup_batch_create()
  2. 7.2 lingotek.batch.inc \lingotek_field_language_data_cleanup_batch_create()
  3. 7.3 lingotek.batch.inc \lingotek_field_language_data_cleanup_batch_create()
  4. 7.4 lingotek.batch.inc \lingotek_field_language_data_cleanup_batch_create()
  5. 7.6 lingotek.batch.inc \lingotek_field_language_data_cleanup_batch_create()

Field Language Data Cleanup Utility

Creates a batch to cleanup nodes with data in an 'und' language field.

Parameters

bool $autoset_batch: If this batch was NOT created from a form_submit() handler, then pass in TRUE

1 call to lingotek_field_language_data_cleanup_batch_create()
lingotek_cleanup_field_languages in ./lingotek.util.inc

File

./lingotek.batch.inc, line 16
Central location for batch create functions, before control is handed off to individual batch command files.

Code

function lingotek_field_language_data_cleanup_batch_create($entity_type, $autoset_batch = FALSE) {
  LingotekLog::trace(__METHOD__);
  $operations = array();
  $source_language = lingotek_get_source_language();
  $translated_types = lingotek_translatable_types($entity_type);
  $disabled_types = lingotek_get_disabled_bundles($entity_type);
  $managed_entities = lingotek_get_enabled_entities_by_type($entity_type);
  $enabled_types = array_diff($translated_types, $disabled_types);
  $info = entity_get_info($entity_type);

  // Fix node level language settings
  // This selects all the nodes that are language undefined and are content
  // types we need to translate.  We need to change these nodes from language
  // undefined to the source language.
  $query1 = new EntityFieldQuery();
  $entities1 = $query1
    ->entityCondition('entity_type', $entity_type)
    ->propertyCondition('language', LANGUAGE_NONE, '=')
    ->execute();
  unset($query1);
  if (isset($entities1[$entity_type])) {
    foreach ($entities1[$entity_type] as $entity) {
      $entity_id = $entity->{$info['entity keys']['id']};
      if (isset($managed_entities[$entity_id])) {
        $operations[] = array(
          'lingotek_source_language_cleanup_batch_worker',
          array(
            $entity_type,
            $entity_id,
            $source_language,
          ),
        );
      }
    }
  }

  // Fix field languages
  $query2 = new EntityFieldQuery();
  $entities2 = $query2
    ->entityCondition('entity_type', $entity_type)
    ->execute();
  unset($query2);
  if (isset($entities2[$entity_type])) {
    foreach ($entities2[$entity_type] as $entity) {
      $entity_id = $entity->{$info['entity keys']['id']};
      if (isset($managed_entities[$entity_id])) {
        $operations[] = array(
          'lingotek_field_language_data_cleanup_batch_worker',
          array(
            $entity_type,
            $entity->{$info['entity keys']['id']},
          ),
        );
      }
    }
  }

  // Add missing URL aliases
  $query = db_select('{url_alias}', 'ua')
    ->fields('ua', array(
    'source',
  ))
    ->condition('ua.source', "{$entity_type}/%", 'LIKE')
    ->condition('ua.language', LANGUAGE_NONE)
    ->execute();
  foreach ($query
    ->fetchCol() as $source) {
    try {
      list($et, $id) = explode('/', $source);
      if (isset($managed_entities[$id])) {
        $operations[] = array(
          'lingotek_url_alias_source_language_cleanup_batch_worker',
          array(
            $entity_type,
            $id,
            $source_language,
          ),
        );
      }
    } catch (Exception $ex) {
      LingotekLog::trace('Found url alias that did not fit the normal pattern: @alias.  Ignoring.');
    }
  }
  $batch = array(
    'title' => t('Lingotek Field Language Updater'),
    'operations' => $operations,
    'finished' => 'lingotek_field_language_data_cleanup_batch_finished',
    'file' => 'lingotek.batch.inc',
  );
  if ($autoset_batch) {

    // If this batch was NOT created from a form_submit() handler, do this to initiate the batch.
    batch_set($batch);
    batch_process('<front>');

    // Needed if not inside a form _submit handler.  Setting redirect in batch_process.
  }
  else {
    return $batch;
  }
}