You are here

function menu_views_update_7102 in Menu Views 7.2

Same name and namespace in other branches
  1. 8.3 menu_views.install \menu_views_update_7102()
  2. 7 menu_views.install \menu_views_update_7102()

FIXED UPDATE: Replaces attached view arguments (%N and %P) of menu link items with Token API equivalents.

File

./menu_views.install, line 43
Install and update functions for the menu_views module.

Code

function menu_views_update_7102(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_mlid'] = 0;

    // Only count links that possibly have a key class with a string value in
    // its serialized options array.
    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT mlid) FROM {menu_links}")
      ->fetchField();
  }

  // Process twenty links at a time.
  $limit = 20;

  // Fetch links
  $links = db_query_range("SELECT mlid, options FROM {menu_links} WHERE mlid > :mlid ORDER BY mlid", 0, $limit, array(
    ':mlid' => $sandbox['current_mlid'],
  ))
    ->fetchAll();
  foreach ($links as $link) {
    $options = unserialize($link->options);
    if (isset($options['menu_views'])) {
      $arguments = array();
      if (isset($options['menu_views']['arguments'])) {
        $arguments = $options['menu_views']['arguments'];
      }
      $options['menu_views']['arguments'] = str_replace(array(
        '%N',
        '%P',
      ), array(
        '[menu-link:node:nid]',
        '[menu-link:parent:node:nid]',
      ), $arguments);
      db_update('menu_links')
        ->fields(array(
        'options' => serialize($options),
      ))
        ->condition('mlid', $link->mlid)
        ->execute();
    }
    $sandbox['progress']++;
    $sandbox['current_mlid'] = $link->mlid;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];

  // To display a message to the user when the update is completed, return it.
  // If you do not want to display a completion message, simply return nothing.
  return t('All menu link items with attached views have had their arguments converted for use with Drupal tokens.');
}