Migrate URL Alias With Sequential IDs

When migrating a site, it's important to catch all the url aliases and redirects. Otherwise, existing links on the web may not work and search rankings will drop.

URL Alias

The d7_url_alias source plugin fetches data from the url_alias table and provides pid, source, alias, and language properties.

The migration needs to get the entity id from the source (node/123) and use it in a migration lookup.

Here is a hook_migrate_prepare_row() function for extracting the necessary properties.

function my_module_migrate_prepare_row(\Drupal\migrate\Row $row, \Drupal\migrate\Plugin\MigrateSourceInterface $source, \Drupal\migrate\Plugin\MigrationInterface $migration) {
  if ($migration->id() == 'upgrade_d7_url_alias') {
    $source = $row->getSourceProperty('source');
    $source_parts = explode('/', $source);
    $source_id = array_pop($source_parts);
    $source_path = implode('/', $source_parts);
    $source_type = str_replace('/', '_', $source_path);

    $row->setSourceProperty('source_id', $source_id);
    $row->setSourceProperty('source_type', $source_type);
    $row->setSourceProperty('source_path', $source_path);

  }
}

This sets source_id, source_type, and source_path properties.

Here's a migration that uses the properties to convert the source path.

source:
  plugin: d7_url_alias
  constants:
    slash: /
process:
  _dest_id:
    -
      plugin: skip_on_value
      value:
        - node
      not_equals: true
      method: row
      source: source_type
    -
      plugin: migration_lookup
      source: source_id
      migration:
        - upgrade_d7_node_blog
        - upgrade_d7_node_event
        - upgrade_d7_node_page
  path:
      plugin: concat
      source:
        - source
        - '@_dest_id'
  alias:
    -
      plugin: concat
      source:
        - constants/slash
        - alias
  langcode:
    -
      plugin: get
      source: language
Tags
Migration
Modules