You are here

function menu_views_update_7103 in Menu Views 7

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

Updates existing menu views to reference the proper mlid. When creating a new menu item, even if the attach view form was present, caused menu views to not save the mlid as it didn't yet exist. This update converts all existing menu views that have a referenced mlid of 0.

File

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

Code

function menu_views_update_7103(&$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'])) {
      $options['menu_views']['mlid'] = $link->mlid;
      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.');
}