You are here

public function MigrateRedirectEntityHandler::complete in Redirect 7

Same name and namespace in other branches
  1. 7.2 redirect.migrate.inc \MigrateRedirectEntityHandler::complete()

Overrides complete().

Parameters

object $entity: The Drupal entity.

stdClass $row: The row being migrated.

File

./redirect.migrate.inc, line 132
Migrate support for Redirect module.

Class

MigrateRedirectEntityHandler
@file Migrate support for Redirect module.

Code

public function complete($entity, stdClass $row) {
  $migration = Migration::currentMigration();
  $destination = $migration
    ->getDestination();
  $entity_type = $destination
    ->getEntityType();
  $migrate_redirects = $this
    ->getRedirects($entity, $row);
  $redirect_destination = entity_uri($entity_type, $entity);

  // We looked up the destination entity_type in the constructor.
  if (!empty($migrate_redirects) && !empty($redirect_destination)) {
    foreach ($migrate_redirects as $path) {
      $redirect_defaults = array(
        'status_code' => 301,
      );
      if (isset($entity->uid)) {
        $redirect_defaults['uid'] = $entity->uid;
      }
      $redirect_defaults['language'] = $this
        ->getRedirectLanguage($entity, $row);
      $redirect = new stdClass();
      redirect_object_prepare($redirect, $redirect_defaults);
      $redirect->redirect = $redirect_destination['path'];
      $parsed = redirect_parse_url($path);
      $redirect->source = isset($parsed['path']) ? ltrim($parsed['path'], '/') : '';
      if (!empty($parsed['query'])) {
        $redirect->source_options['query'] = $parsed['query'];
      }

      // Only save if the redirect does not already exist.
      if ($this
        ->redirectValidate($redirect)) {
        redirect_save($redirect);
      }
    }
  }
  elseif (!empty($migrate_redirects) && empty($redirect_destination)) {
    $migration
      ->saveMessage(t('The redirect path is empty.'), MigrationBase::MESSAGE_INFORMATIONAL);
  }
}