function metatag_bulk_revert_batch_operation in Metatag 7
Batch callback: delete custom metatags for selected bundles.
1 string reference to 'metatag_bulk_revert_batch_operation'
- metatag_bulk_revert_form_submit in ./
metatag.admin.inc - Form submit handler for metatag reset bulk revert form.
File
- ./
metatag.admin.inc, line 545 - Administration page callbacks for the metatag module.
Code
function metatag_bulk_revert_batch_operation($entity_type, $bundle, $tags, $languages, &$context) {
if (!isset($context['sandbox']['current'])) {
$context['sandbox']['count'] = 0;
$context['sandbox']['current'] = 0;
}
// Query the selected entity table.
$entity_info = entity_get_info($entity_type);
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $entity_type)
->propertyCondition($entity_info['entity keys']['id'], $context['sandbox']['current'], '>')
->propertyOrderBy($entity_info['entity keys']['id']);
if ($entity_type != 'user') {
// Entities which do not define a bundle such as User fail returning no
// results.
// @see https://www.drupal.org/node/1054168#comment-5266208
$query
->entityCondition('bundle', $bundle);
}
// Get the total amount of entities to process.
if (!isset($context['sandbox']['total'])) {
$context['sandbox']['total'] = $query
->count()
->execute();
$query->count = FALSE;
// If there are no bundles to revert, stop immediately.
if (!$context['sandbox']['total']) {
$context['finished'] = 1;
return;
}
}
// Process 25 entities per iteration.
$query
->range(0, 25);
$result = $query
->execute();
$entity_ids = !empty($result[$entity_type]) ? array_keys($result[$entity_type]) : array();
foreach ($entity_ids as $entity_id) {
$metatags = metatag_metatags_load($entity_type, $entity_id);
if (!empty($metatags)) {
$reset = FALSE;
if (empty($tags)) {
// All tags should be reset, so we just delete any records from the db.
$query = db_delete('metatag')
->condition('entity_type', $entity_type)
->condition('entity_id', $entity_id);
if (!empty($languages)) {
$query
->condition('language', $languages, 'IN');
}
$query
->execute();
metatag_metatags_cache_clear($entity_type, $entity_id);
$reset = TRUE;
}
else {
// Iterate over tags and unset those, that we want to reset.
$needs_reset = FALSE;
foreach ($metatags as $metatags_language => $metatags_tags) {
if (empty($languages) || in_array($metatags_language, $languages)) {
foreach ($metatags_tags as $metatags_tag => $metatags_value) {
if (in_array($metatags_tag, $tags)) {
unset($metatags[$metatags_language][$metatags_tag]);
$needs_reset = TRUE;
}
}
}
}
// Save modified metatags.
if ($needs_reset) {
// We don't have a revision id, so we'll get the active one.
// Unfortunately, the only way of getting the active revision ID is to
// first load the entity, and then extract the ID. This is a bit
// performance intensive, but it seems to be the only way of doing it.
$entities = entity_load($entity_type, array(
$entity_id,
));
if (!empty($entities[$entity_id])) {
// We only care about the revision_id.
list(, $revision_id, ) = entity_extract_ids($entity_type, $entities[$entity_id]);
}
metatag_metatags_save($entity_type, $entity_id, $revision_id, $metatags, $bundle);
$reset = TRUE;
}
}
if ($reset) {
$context['results'][] = t('Reverted metatags for @bundle with id @id.', array(
'@bundle' => $entity_type . ': ' . $bundle,
'@id' => $entity_id,
));
}
}
}
$context['sandbox']['count'] += count($entity_ids);
$context['sandbox']['current'] = max($entity_ids);
if ($context['sandbox']['count'] != $context['sandbox']['total']) {
$context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
}
}