1

I am migrating a D6 site to D7 and for some odd reason the redirect module is not creating the redirections. Any ideas? I am not getting any errors, warnings or notices

This is what I have:

class MyNewsMigration extends MyNodeMigration {
  public function __construct(array $arguments) {

    parent::__construct($arguments);

    $this->addFieldMapping('field_tag', '8')
         ->sourceMigration('Tag');
    $this->addFieldMapping('field_tag:source_type')->defaultValue('tid');
    $this->addFieldMapping('field_tag:create_term')->defaultValue(FALSE);
    $this->addFieldMapping('field_tag:ignore_case')->defaultValue(TRUE);

    // Redirect module to issue a 301 for each changing path
    $this->addFieldMapping('pathauto')->defaultValue(1);
    $this->addFieldMapping('migrate_redirects', 'path');

  }
}

Note: I added the migrate.redirect.inc using patch from here https://www.drupal.org/node/1116408

  • Have you tried anything to debug this? Also, you reported a bad behaviour, but you didn't actually ask any question. – Mołot Oct 28 '15 at 21:28
2

I had a slightly different use case - changing aliases for users from /source/user-name to /author/user-name and creating redirects from the old alias the new one - but the steps are still the same.

First, I implemented a complete() method in my migrate class. This is called after the new entity (node, user, etc) has been created:

  /**
   * Implementation of MigrateDestinationEntity::complete() method.
   *
   * @param $entity
   * @param $row
   */
  function complete($account, $row) {
    // Create redirect from old Source alias that uses /source/
    // to new user profile alias.

    if (!redirect_load_by_source($row->dst)) {
      // First, get the alias for the user profile. We have to generate it separately
      // here because pathauto_create_alias() has already been run by the time the code
      // gets here, and we don't have access to that path.
      $new_alias = pathauto_create_alias('user', 'return', 'user/' . $account->uid, array('user' => $account));

      // Next, create the $redirect object and save it.
      $redirect = new stdClass();

      redirect_object_prepare(
        $redirect,
        array(
          'source' => $row->dst,
          'source_options' => array(),
          'redirect' => $new_alias,
          'redirect_options' => array(),
          'language' => LANGUAGE_NONE,
        )
      );
      redirect_save($redirect);
    }
  }

And then, to handle rollbacks (during development), I created a new class that overrides DrupalDestinationUser and implements a custom prepareRollback method:

class CNSUserDestination extends DrupalDestinationUser {
  // Add prepareRollback() method to this class.
  public function prepareRollback($uids) {
    parent::prepareRollback($uids);
    // Load all of the entities, since we need them for pathauto_create_alias().
    $users = entity_load('user', $uids);

    // Load pathauto.inc file.
    module_load_include('inc', 'pathauto', 'pathauto');

    foreach ($users as $uid => $user) {
      $redirects[] = pathauto_create_alias('user', 'return', 'user/' . $user->uid, array('user' => $user));
    }

    // Delete redirects for user profiles.
    db_delete('redirect')
      ->condition('redirect', $redirects)
      ->execute();
  }
}

It worked like a charm.

  • Thank you for your comment and sharing your code, it gave the inside I need it to make it work. I will be posting my answer below. – Emil Orol Nov 3 '15 at 16:53
2

To use the Migrate D2D with the redirect feature listed here so that you only need to only have to do something like this:

$this->addFieldMapping('migrate_redirects', 'my_old_path');

Do the following:

  1. In you class add something like this:
class MyNewsMigration extends MyNodeMigration {
  public function __construct(array $arguments) {

    parent::__construct($arguments);

    $this->addFieldMapping('migrate_redirects', 'my_old_path');

  }
}
  1. Then what I was missing was the definition of the "my_old_path" in the prepareRow method.
public function prepareRow($row) {
  if (parent::prepareRow($row) === FALSE) {
    return FALSE;
  }

  /* 
    You can query the DB to get the alias or 
    built it when you finish doing all the 
    processing assign it. 
  */

  $row->{'my_old_path'}  = 'PUT RESULT HERE';

}
  1. Now you are ready to run your migration and rollbacks as need it.

Note: depending on your needs you can do at lot in the prepareRow to get the path of the old now or taxonomy term or any other entity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.