Skip to content

Instantly share code, notes, and snippets.

@mootari
Last active August 29, 2015 14:18
Show Gist options
  • Save mootari/50b019fffd2ab8a8dba3 to your computer and use it in GitHub Desktop.
Save mootari/50b019fffd2ab8a8dba3 to your computer and use it in GitHub Desktop.
Delete entity translation leftovers (field values in languages other than the node language that are not LANGUAGE_NONE).
<?php
function EXAMPLE_cleanup_field_values($node_type = null) {
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node');
if($node_type) {
$query->entityCondition('bundle', $node_type);
}
$results = $query->execute();
if(empty($results['node'])) {
return;
}
foreach($results['node'] as $node) {
$node = node_load($node->nid);
$node_lang = $node->language;
$updated = false;
foreach(array_keys(field_info_instances('node', $node->type)) as $field_name) {
if(empty($node->$field_name)) {
continue;
}
foreach(array_keys($node->$field_name) as $field_lang) {
if($field_lang !== $node_lang && $field_lang !== LANGUAGE_NONE) {
$node->{$field_name}[$field_lang] = array();
$updated = true;
}
}
}
if($updated) {
node_save($node);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment