You are here

function menu_token_update_7001 in Menu Token 7

Implements hook_update_N().

File

./menu_token.install, line 80
Install file for menu_token module.

Code

function menu_token_update_7001(&$sandbox) {

  // Initializing sandbox variables.
  if (!isset($sandbox['progress'])) {

    // Preparing array of menu items for batch insert.
    foreach (variable_get('menu_token_enabled', array()) as $mlid => $link_path) {
      $sandbox['items'][] = array(
        'mlid' => $mlid,
        'link_path' => $link_path,
      );
    }
    $sandbox['progress'] = 0;
    $sandbox['max'] = count($sandbox['items']);
  }

  // Insert current record.
  if (!empty($sandbox['max'])) {
    db_merge('menu_token')
      ->key(array(
      'mlid' => $sandbox['items'][$sandbox['progress']]['mlid'],
    ))
      ->fields(array(
      'link_path' => $sandbox['items'][$sandbox['progress']]['link_path'],
    ))
      ->execute();
  }
  $sandbox['progress']++;
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];

  // Delete variable in case of all queries were executed
  if ($sandbox['#finished']) {
    variable_del('menu_token_enabled');
  }
}