Skip to content

Instantly share code, notes, and snippets.

@silent-e
Created April 10, 2015 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silent-e/32cd2b9c93737eed987c to your computer and use it in GitHub Desktop.
Save silent-e/32cd2b9c93737eed987c to your computer and use it in GitHub Desktop.
Entity Translation upgrade to enable revisions
/**
* Create revision for existing translation.
*/
function module_update_7005(&$sandbox)
{
if (!isset($sandbox['progress'])):
$sandbox['progress'] = 0;
$sandbox['current_type'] = 0;
$sandbox["entity_type"] = array();
$sandbox['max'] = db_query('SELECT COUNT(*) FROM {entity_translation}')->fetchField();
$types = db_query('SELECT DISTINCT(entity_type) FROM {entity_translation}');
$i = 0;
// For each entity type, we initialize some information
foreach($types as $type):
$sandbox["entity_type"][$i]["type"] = $type->entity_type;
$sandbox["entity_type"][$i]['max'] = db_query('SELECT COUNT(*) FROM {entity_translation} WHERE entity_type=:entity_type', array(':entity_type' => $type->entity_type))->fetchField();
$sandbox["entity_type"][$i]['progress'] = 0;
$sandbox["entity_type"][$i]['current'] = 0;
$sandbox["entity_type"][$i]['finish'] = 0;
$i++;
endforeach;
$sandbox["entity_type_count"] = $i;
endif;
// if we have entities to update
if ($sandbox["entity_type_count"] > 0):
$i = $sandbox['current_type'];
$entity_type = $sandbox["entity_type"][$i]["type"];
// Get 25 records at a time to create revision table records.
// BUG IS HERE!! If entity_id has a larger gap than 25 it will loop infinitely. Need to use a proper pager
$result = db_select('entity_translation', 't')
->fields('t')
->orderBy('t.entity_id', 'ASC')
->condition('t.entity_type', $entity_type, "=")
->range($sandbox['progress'], $sandbox['progress'] + 25)
->execute();
$info_entity = entity_get_info($entity_type);
// For each row, we copy information into revision table
foreach ($result as $row):
$entity = entity_load_single($row->entity_type, $row->entity_id);
$revision_id = $entity->{$info_entity['entity keys']['revision'] != "" ? $info_entity['entity keys']['revision'] : $info_entity['entity keys']['id']};
db_insert('entity_translation_revision')
->fields(array(
"entity_type" => $row->entity_type,
"entity_id" => $row->entity_id,
"revision_id" => $revision_id,
"language" => $row->language,
"source" => $row->source,
"uid" => $row->uid,
"status" => $row->status,
"translate" => $row->translate,
"created" => $row->created,
"changed" => $row->changed
))
->execute();
db_update("entity_translation")
->fields(array(
"revision_id" => $revision_id
))
->condition("entity_type", $row->entity_type, "=")
->condition("entity_id", $row->entity_id, "=")
->condition("language", $row->language, "=")
->execute();
// End operation, we update the sandbox
$sandbox['progress']++;
$sandbox["entity_type"][$i]['progress']++;
$sandbox["entity_type"][$i]['current']= $row->entity_id;
$sandbox["entity_type"][$i]['finish'] = empty($sandbox["entity_type"][$i]['max']) ? 1 : ($sandbox["entity_type"][$i]['progress'] / $sandbox["entity_type"][$i]['max']);
endforeach;
if ($sandbox["entity_type"][$i]['finish'] == 1):
$sandbox['current_type']++;
endif;
endif;
// Batch is finished when all operation have been process
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
if ($sandbox['#finished']):
variable_set('entity_translation_revision_enabled', TRUE);
endif;
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment