Last active
August 29, 2015 14:06
-
-
Save plopesc/29b22b0dfc783bd25740 to your computer and use it in GitHub Desktop.
enable entity_translation revision support for existing site (Non previous revisionable data)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Enables entity_translation revisions support. | |
*/ | |
function my_module_update_70XX() { | |
$revisionable_entity_types = array(); | |
$query = db_select('entity_translation', 'et') | |
->fields('et', array('entity_type')) | |
->distinct(); | |
$entity_types = $query->execute()->fetchCol(); | |
foreach ($entity_types as $entity_type) { | |
$entity_info = entity_get_info($entity_type); | |
if(!empty($entity_info['entity keys']['revision'])) { | |
$revisionable_entity_types[] = $entity_type; | |
$query = db_select('entity_translation', 'et') | |
->fields('et', array('entity_id')) | |
->condition('et.entity_type', $entity_type) | |
->distinct(); | |
$entity_ids = $query->execute()->fetchCol(); | |
$entities = entity_load($entity_type, $entity_ids); | |
// Set the current revision_id as the entity_translation revision_id. | |
foreach ($entities as $entity) { | |
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); | |
db_update('entity_translation') | |
->fields(array( | |
'revision_id' => $vid, | |
)) | |
->condition('entity_id', $id) | |
->condition('entity_type', $entity_type) | |
->execute(); | |
} | |
} | |
} | |
// Copies data from entity_translation to entity_translation_revision table. | |
if (!empty($revisionable_entity_types)) { | |
$query = db_select('entity_translation', 'et') | |
->fields('et', array('entity_type', 'entity_id', 'revision_id', 'language', 'source', 'uid', 'status', 'translate', 'created', 'changed')) | |
->condition('et.entity_type', $revisionable_entity_types); | |
db_insert('entity_translation_revision') | |
->from($query) | |
->execute(); | |
} | |
// Enable revision support setting variable to TRUE. | |
variable_set('entity_translation_revision_enabled', TRUE); | |
return t('Entity Translation revision support is enabled'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment