You are here

function metatag_update_replace_entity_tag in Metatag 7

Replace one meta tag's with another in the entity records.

Parameters

array $sandbox: A Batch API sandbox, passed by reference.

string $old_tag: The meta tag that is to be replaced.

string $new_tag: The meta tag that replaces the old one.

4 calls to metatag_update_replace_entity_tag()
metatag_opengraph_update_7103 in metatag_opengraph/metatag_opengraph.install
Change og:video to og:video:url in all metatags.
metatag_twitter_cards_update_7100 in metatag_twitter_cards/metatag_twitter_cards.install
Rename the 'twitter:image:src' meta tag back to 'twitter:image'. Sorry.
metatag_update_7026 in ./metatag.install
Rename the 'copyright' meta tag to 'rights', part 1.
metatag_update_7109 in ./metatag.install
Rename the 'icon_any' meta tag to 'mask-icon' in the entity records.

File

./metatag.install, line 535
Install, update, and uninstall functions for the metatag module.

Code

function metatag_update_replace_entity_tag(array &$sandbox, $old_tag, $new_tag) {
  if (!isset($sandbox['progress'])) {

    // Count of all {metatag} records that contained an entry for the old meta
    // tag.
    $records_count = db_select('metatag', 'm')
      ->condition('m.data', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
      ->countQuery()
      ->execute()
      ->fetchField();
    if (empty($records_count)) {
      return t('No Metatag entity records needed to have the "@tag" meta tag renamed.', array(
        '@tag' => $old_tag,
      ));
    }
    $sandbox['max'] = $records_count;
    $sandbox['progress'] = 0;
  }

  // Count of rows that will be processed per iteration.
  $limit = 100;

  // Fetches a part of records.
  $records = db_select('metatag', 'm')
    ->fields('m', array())
    ->condition('m.data', '%' . db_like('"' . $old_tag . '"') . '%', 'LIKE')
    ->range(0, $limit)
    ->execute();
  $count = 0;
  $keys = array(
    'entity_type',
    'entity_id',
    'revision_id',
    'language',
  );

  // Loop over the values and correct them.
  foreach ($records as $record) {
    $record->data = unserialize($record->data);
    if (isset($record->data[$old_tag])) {
      $record->data[$new_tag] = $record->data[$old_tag];
      unset($record->data[$old_tag]);
      drupal_write_record('metatag', $record, $keys);

      // Clear the cache for the entity this belongs to.
      entity_get_controller($record->entity_type)
        ->resetCache(array(
        $record->entity_id,
      ));
    }
    $count++;
  }
  if (!empty($count)) {
    $sandbox['progress'] += $count;

    // In some cases the query yields results that cannot be fixed and we would
    // run into an infinite loop. Stop immediately if we processed all records.
    if ($sandbox['progress'] >= $sandbox['max']) {
      $sandbox['#finished'] = TRUE;
    }
    else {
      $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
    }
  }
  else {
    $sandbox['#finished'] = TRUE;
    return t('Converted the "@old_tag" meta tag for @count entity records to "@new_tag" meta tag.', array(
      '@old_tag' => $old_tag,
      '@new_tag' => $new_tag,
      '@count' => $sandbox['progress'],
    ));
  }
}