function taxonomy_orphanage_batch_roundup in Taxonomy Orphanage 7
Batch callback to remove orphaned taxonomy references from entities.
1 string reference to 'taxonomy_orphanage_batch_roundup'
- taxonomy_orphanage_roundup in ./
taxonomy_orphanage.module - Looks for orphaned taxonomy references and removes them.
File
- ./
taxonomy_orphanage.batch.inc, line 11 - Batch callbacks for taxonomy_orphanage.
Code
function taxonomy_orphanage_batch_roundup($field, $limit, $display_finished, &$context) {
$batch_limit = $limit > 0 && $limit < 100 ? $limit : 100;
$query = db_select('field_data_' . $field['field_name'], 'f');
$query
->fields('f', array(
'entity_type',
'entity_id',
$field['field_name'] . '_tid',
))
->leftJoin('taxonomy_term_data', 't', 't.tid=f.' . $field['field_name'] . '_tid');
$query
->where('t.tid IS NULL')
->orderBy('f.entity_id', 'ASC');
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $limit;
$count_query = $query
->countQuery();
$count = $count_query
->execute()
->fetchField();
if ($limit <= 0 || $count < $limit) {
$context['sandbox']['max'] = $count;
}
if (!isset($context['results']['count'])) {
$context['results']['count'] = 0;
$context['results']['display_finished'] = $display_finished;
}
}
// First collect all the tids that exist for this field on the entities.
$terms = array();
$query
->range(0, $batch_limit);
$res = $query
->execute();
foreach ($res as $target) {
$context['sandbox']['progress']++;
$loaded_entity = entity_load($target->entity_type, array(
$target->entity_id,
));
$entity = array_shift($loaded_entity);
if (!$entity) {
continue;
}
foreach ($entity->{$field['field_name']} as $language => $items) {
foreach ($items as $k => $term) {
if ($term['tid'] == $target->{$field['field_name'] . '_tid'}) {
unset($entity->{$field['field_name']}[$language][$k]);
$context['results']['count']++;
if (entity_save($target->entity_type, $entity) === FALSE) {
throw new Exception('Failed to save entity type:' . $target->entity_type . ' id: ' . $target->entity_id);
}
break;
}
}
}
if ($context['sandbox']['progress'] == $context['sandbox']['max']) {
break;
}
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}