You are here

function search_api_et_update_7201 in Search API Entity Translation 7.2

Migrate the old ItemID format to the newer one.

File

./search_api_et.install, line 164
Install, update and uninstall functions for the Search API ET module.

Code

function search_api_et_update_7201(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(*) FROM {search_api_et_item}')
      ->fetchField();
  }
  $items = db_select('search_api_et_item', 'item')
    ->fields('item', array(
    'item_id',
    'index_id',
  ))
    ->range($sandbox['progress'], 500)
    ->orderBy('item_id', 'ASC')
    ->execute();
  foreach ($items as $item) {

    // Get the old item_id format: "{LANG}_{ENTITY-ID}".
    $parts = explode('_', $item->item_id);
    if (is_array($parts) && count($parts) == 2) {

      // Build the new item_id format: "{ENTITY-ID}/{LANG}".
      $new_item_id = $parts[1] . '/' . $parts[0];
      db_update('search_api_et_item')
        ->fields(array(
        'item_id' => $new_item_id,
      ))
        ->condition('item_id', $item->item_id)
        ->execute();
    }
    $sandbox['progress']++;
  }

  // Inform on the progression.
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished'] != 1) {
    return t('SearchAPI ET: Updated !items of !total total.', array(
      '!items' => $sandbox['progress'],
      '!total' => $sandbox['max'],
    ));
  }
  else {
    return t('SearchAPI ET: Migrate the old ItemID format to the newer one completed. You will need to run a re-index on search_api_et indexes.');
  }
}