You are here

function metatag_update_7013 in Metatag 7

Fix the {metatag} language value for all non-core entity records.

This might take a while, depending on how much data needs to be converted.

File

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

Code

function metatag_update_7013(array &$sandbox) {

  // Fix the {metatag} table first.
  metatag_update_7015();
  if (module_exists('entity_translation')) {
    drupal_set_message(t("Entity Translation is enabled, meta tags will not be updated for custom entities, to avoid accidental dataloss."));
    return;
  }

  // Use the sandbox at your convenience to store the information needed
  // to track progression between successive calls to the function.
  if (!isset($sandbox['progress'])) {

    // The count of records visited so far.
    $sandbox['progress'] = 0;

    // Because the {metatag} table uses multiple primary keys, there's no easy
    // way to do this, so we're going to cache all record keys and manually
    // step through them.
    $records = db_select('metatag', 'm')
      ->fields('m', array(
      'entity_type',
      'entity_id',
    ))
      ->condition('m.entity_type', array(
      'node',
      'taxonomy_term',
      'user',
    ), 'NOT IN')
      ->orderBy('m.entity_type', 'ASC')
      ->orderBy('m.entity_id', 'ASC')
      ->execute();
    $sandbox['records'] = array();
    foreach ($records as $record) {
      $sandbox['records'][] = $record;
    }

    // If there's no data, don't bother with the extra work.
    if (empty($sandbox['records'])) {
      watchdog('metatag', 'Update 7013: No meta tag records need updating.', array(), WATCHDOG_INFO);
      if (drupal_is_cli()) {
        drupal_set_message(t('Update 7013: No meta tag records need updating.'));
      }
      return t('No meta tag records need updating.');
    }

    // Total records that must be visited.
    $sandbox['max'] = count($sandbox['records']);

    // A place to store messages during the run.
    $sandbox['messages'] = array();

    // An initial record of the number of records to be updated.
    watchdog('metatag', 'Update 7013: !count records to update.', array(
      '!count' => $sandbox['max'],
    ), WATCHDOG_INFO);
    if (drupal_is_cli()) {
      drupal_set_message(t('Update 7013: !count records to update.', array(
        '!count' => $sandbox['max'],
      )));
    }

    // Last record processed.
    $sandbox['current_record'] = -1;
  }

  // Process records by groups of 10 (arbitrary value).
  // When a group is processed, the batch update engine determines whether it
  // should continue processing in the same request or provide progress
  // feedback to the user and wait for the next request.
  $limit = 10;

  // When ran through Drush it's Ok to process a larger number of objects at a
  // time.
  if (drupal_is_cli()) {
    $limit = 100;
  }

  // Set default values.
  for ($ctr = 0; $ctr < $limit; $ctr++) {
    $sandbox['current_record']++;
    if (empty($sandbox['records'][$sandbox['current_record']])) {
      break;
    }

    // Shortcuts for later.
    $entity_type = $sandbox['records'][$sandbox['current_record']]->entity_type;
    $entity_id = $sandbox['records'][$sandbox['current_record']]->entity_id;

    // Load the entity.
    $entities = entity_load($entity_type, array(
      $entity_id,
    ));
    if (!empty($entities)) {
      $entity = array_pop($entities);

      // Make sure that the entity has a language set.
      if (!empty($entity)) {

        // If there's a (non-empty) language value, use it.
        $new_language = entity_language($entity_type, $entity);
        if (empty($new_language)) {
          $new_language = LANGUAGE_NONE;
        }

        // Update the 'language' value.
        db_update('metatag')
          ->fields(array(
          'language' => $new_language,
        ))
          ->condition('entity_type', $entity_type)
          ->condition('entity_id', $entity_id)
          ->execute();
      }
    }

    // Update our progress information.
    $sandbox['progress']++;
  }

  // Set the "finished" status, to tell batch engine whether this function
  // needs to run again. If you set a float, this will indicate the progress of
  // the batch so the progress bar will update.
  if ($sandbox['progress'] >= $sandbox['max']) {
    $sandbox['#finished'] = TRUE;
  }
  else {
    $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  }
  if ($sandbox['#finished']) {

    // Clear all caches so the fixed data will be reloaded.
    cache_clear_all('*', 'cache_metatag', TRUE);

    // A final log of the number of records that were converted.
    watchdog('metatag', 'Update 7013: !count records were updated in total.', array(
      '!count' => $sandbox['progress'],
    ), WATCHDOG_INFO);
    if (drupal_is_cli()) {
      drupal_set_message(t('Update 7013: !count records were updated.', array(
        '!count' => $sandbox['progress'],
      )));
    }

    // hook_update_N() may optionally return a string which will be displayed
    // to the user.
    return t('!count records were updated in total.', array(
      '!count' => $sandbox['progress'],
    ));
  }
}