You are here

function path_breadcrumbs_update_7200 in Path Breadcrumbs 7.2

Same name and namespace in other branches
  1. 7.3 path_breadcrumbs.install \path_breadcrumbs_update_7200()

Migrate data from 7.x-1.x to 7.x-2.x.

File

./path_breadcrumbs.install, line 97
Provides database structure for PATH BREADCRUMBS module.

Code

function path_breadcrumbs_update_7200(&$sandbox) {

  // Select all data from old table.
  $variants = db_select('path_breadcrumbs', 'p')
    ->fields('p')
    ->execute();
  $new_variants = array();
  foreach ($variants as $variant) {

    // Replace '*' in path on a new argument placeholder.
    $path_arguments = explode('/', $variant->path);
    $new_path = array();
    $arguments = array();
    $arg_counter = 0;
    foreach ($path_arguments as $index => $arg) {
      if ($arg == '*') {
        $keyword = 'argument_' . ++$arg_counter;
        $new_path[] = '%' . $keyword;
        $arguments[$keyword] = array(
          'position' => $index,
        );
      }
      else {
        $new_path[] = $arg;
      }
    }
    $variant->path = implode('/', $new_path);

    // Create machine name from variant name.
    $variant->machine_name = 'path_breadcrumbs_' . $variant->path_id;

    // Build data array.
    $variant->data = serialize(array(
      'titles' => unserialize($variant->titles),
      'paths' => unserialize($variant->paths),
      'home' => $variant->home,
      'translatable' => 1,
      'arguments' => $arguments,
      'access' => array(),
    ));
    $new_variants[] = $variant;
  }

  // Drop old table.
  db_drop_table('path_breadcrumbs');

  // Create new table.
  db_create_table('path_breadcrumbs', drupal_get_schema_unprocessed('path_breadcrumbs', 'path_breadcrumbs'));

  // Insert old data into updated table.
  foreach ($new_variants as $variant) {
    db_insert('path_breadcrumbs')
      ->fields(array(
      'path_id' => $variant->path_id,
      'name' => $variant->name,
      'machine_name' => $variant->machine_name,
      'path' => $variant->path,
      'data' => $variant->data,
      'weight' => $variant->path_id,
      'disabled' => 0,
    ))
      ->execute();
  }

  // Enable module that is new for 7.x-2.x but was included in 7.x-1.x core.
  module_enable(array(
    'path_breadcrumbs_ui',
  ));
  return t('Path breadcrumbs successfully converted data from old table to a new one.');
}