When migrating from a staging site into the "real" site, content is moved, but none of the book hierarchy comes over. This is particularly frustrating since the D6 to D7 migration lost that and I manually recreated it. I expected moving D7 to D7 would transfer this over.

I can't find any optional module that handles this, and the nodes themselves come over just fine.

Comments

rbroberts created an issue. See original summary.

herved’s picture

Not sure if this can help but I wrote a dedicated migration class below to import the book hierarchy (menu links) from D7 to D7.
I needed this patch https://www.drupal.org/files/issues/migrate_d2d-menu_names_and_cleanup-1...
And my task declaration looks like this:

'MyMigrateImportBookMenuLinks' => array(
  'class_name' => 'MyMigrateImportBookMenuLinks',
  'description' => 'Migration of Book hierarchy',
  'menu_names' => 'book-toc-%',
  'node_migrations' => array(
    'MyMigrateImportNodePage',
    'MyMigrateImportNodeBook',
    // Other dependencies? (content types involved with books)...
  ),
),
class MyMigrateImportBookMenuLinks extends DrupalMenuLinks7Migration {
  public function __construct(array $arguments) {
    parent::__construct($arguments);
  }

  protected function query() {
    $query = Database::getConnection('default', $this->sourceConnection)
      ->select('menu_links', 'm')
      ->fields('m');

    $query->join('book', 'b', 'm.mlid = b.mlid');
    $query->fields('b');

    if (!empty($this->menu_names)) {
      $query->condition('m.menu_name', $this->menu_names, 'LIKE');
    }

    return $query;
  }

  public function prepareRow($row) {
    if (parent::prepareRow($row) === FALSE) {
      return FALSE;
    }

    $regex = '/^book-toc-([\d]+)$/';
    $matches = array();
    if (preg_match($regex, $row->menu_name, $matches)) {
      if ($value = $this->handleSourceMigration($this->node_migrations, $matches[1])) {
        $row->menu_name = 'book-toc-' . $value;
      }
      else {
        return FALSE;
      }
    }
    else {
      return FALSE;
    }
  }

  public function complete($menu_link, stdClass $row) {
    // Add related entry in the book table.
    $regex = '/(?<=node\/)[\d]+(?=$|\/)/';
    $matches = array();

    if (preg_match($regex, $row->link_path, $matches)) {
      $dst_nid = $matches[0];
      if ($dst_bid = $this->handleSourceMigration($this->node_migrations, $row->bid)) {
        db_insert('book')
          ->fields(array(
            'nid' => $dst_nid,
            'mlid' => $menu_link->mlid,
            'bid' => $dst_bid,
          ))
          ->execute();
      }
    }
  }
}
mikeryan’s picture

Category: Bug report » Feature request
Status: Active » Closed (duplicate)
Related issues: +#2146961: Preserve Book hierarchies