Providing a migration from D6's image module (https://www.drupal.org/project/image) to D7 nodes + imagefield has been on my todo list for a very long time... Doing this within Migrate D2D seems like a good idea.

However, I don't see a way to fit this into the D2D UI.

There should be a way for extra migrations to fit into the UI wizard, or for node type-specific migrations to override the use of DrupalNode6Migration.

Comments

roberttstephens’s picture

It's not a clean way, but you can

1) Extend the drupal 7 node class.
2) Update the migrate_status table to use your new class.

You could extend MigrateD2DWizard and override function reviewForm instead, but that looks like it would duplicate a lot of code. If you do that, you would also have to do something like the following.

/**
 * Implements hook_migrate_api_alter().
 */
function new_module_migrate_api_alter(array &$info) {
  if (isset($info['migrate_d2d_ui']['wizard classes'][0])) {
    $info['migrate_d2d_ui']['wizard classes'][0] = 'MyNewClass';
  }
}

I think the "right" way would be to add a form element to the UI to select which class to use for a given migration.

joachim’s picture

Title: extending D2D migration with support for contrib modules » extending D2D migration to allow contrib modules to add wizard steps
joachim’s picture

> 1) Extend the drupal 7 node class.

That won't work if more than one contrib module is in play!

Presumably this use case is covered in D8, because Migrate in D8 core is based on plugins and config files, and any module can provide them.

So I think that for D7 we need to invent something.

What about allowing extra wizard steps to be provided by other classes, and declared with the migrate info hook?

fengtan’s picture

StatusFileSize
new509 bytes

Or maybe we could use a hook ? Patch suggestion attached.

joachim’s picture

Would be better to do this in Migrate, so that any wizard can take advantage of this?

Also, there's the matter of the order of steps that get added. In the Image module use case, we have two submodules: Image and Image Attach, and one needs to go after the other. I guess each module can use addStep(), and they should know their relative module weights and so act accordingly?

joachim’s picture

> Would be better to do this in Migrate, so that any wizard can take advantage of this?

On second thoughts, that's maybe not such a good idea:

- D2D is the major use case for another module altering a wizard. (I've written several custom migration wizards and never needed something like this.)
- putting it in Migrate would mean the hook implementations would need to check the wizard class to decide whether to act, which would add code to all the D2D implementations.

joachim’s picture

Status: Active » Needs work

Actually, I don't think the alter hook as it stands is going to work.

I'm trying to implement it, and I immediately hit a problem when trying to add extra steps: addStep() expects as a parameter the name of a method on the wizard class. Passing in a PHP callable instead won't work because the base Wizard class does this:

    $form += $this->{$form_method}($form_state);
joachim’s picture

This gets more complicated the more I look at it :)

- addStep() needs to be able to accept a callable as well as a method name
- addStep() needs to be changed to a public method
- the wizard form builder needs to work with a callable for the form step builders
- the wizard needs a method that returns all its current steps, so code that seeks to extend the wizard can pass an $after step to addStep()
- for D2D, the reviewForm() method needs to allow extension so that the extra contrib steps can also be reviewed.

I'm working on something and will file a patch at #2363015: Add ability for other modules to alter a migration wizard.

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new6.4 KB

I've posted a patch to Migrate at #2363015: Add ability for other modules to alter a migration wizard.

This is a patch on D2D which builds on top of that.

I'm making use of this in Image module's migration to D7 -- I'll be committing code to that module soon.

mikeryan’s picture

Priority: Major » Normal
Status: Needs review » Needs work

Pending the latest version of #2363015: Add ability for other modules to alter a migration wizard - I don't see that any changes to migrate_d2d_ui are necessary here, the review form should be alterable by the standard hook_form_alter().

joachim’s picture

Will a hook_form_alter() be able to grab the right wizard extender from the wizard in the form? It'll also have to read out its properties, which means the extenders either need an all-purpose accessor method or to make those properties public.

It does strike me as better DX to be able to keep the logic for the review form inside the same class as the rest of the wizard logic, rather than have it live in another function, in another file.

joachim’s picture

Also, without my proposed reviewAlter() method, how do extenders add migrations? It doesn't make sense to add this in the validation methods, as the user might step backwards and forwards in the wizard, and you could end up with a total mess -- migrations added multiple times, and so on.

Though this does suggest that this should be done at the Migrate level rather than here, but the Migrate wizard class doesn't provide the concept of the final review step, does it?

mikeryan’s picture

What I'm working on, both so I get a good feel for exactly how modifying the wizard will work, and to provide an example to help others, is an add-on module to migrate Drupal 6 aggregator configuration (and maybe if ambitious, the imported items) to Drupal 7 feeds_news. I'll see if it is possible to do this with existing APIs without too much trouble, or if we do need to add reviewAlter() or something like that.

On stepping backwards and forwards, that's already an issue, I'll deal with that in #2390149: Make sure back-and-forth in wizard works properly. If an extender adds their migration in the review step, it should all just work fine.

[edit: fixed issue reference]

mikeryan’s picture

OK, I've got a POC which I think works nicely - I haven't actually implemented the migration itself, but have verified that the review form is updated according to the selection in the added step. This works without making any changes to migrate_d2d at all, and with one change to MigrateUIWizard to add an extender getter (I'll put an issue in the Migrate queue for that).

feeds_migrate_aggregator.module:

function feeds_migrate_aggregator_form_migrate_ui_wizard_alter(&$form, &$form_state) {
  // If we are on the last page of a wizard, and our extender is present,
  // make our alterations to the form.
  if (!empty($form['actions']['finish'])) {
    $extender = $form_state['wizard']->getExtender('FeedsMigrateAggregatorExtender');
    if ($extender) {
      $extender->reviewFormAlter($form, $form_state);
    }
  }
}

feeds_migrate_aggregator.migrate.inc:

function feeds_migrate_aggregator_migrate_api() {
  $api = array(
    'api' => 2,
    'wizard extenders' => array(
      'MigrateD2DWizard' => array('FeedsMigrateAggregatorExtender'),
    ),
  );
  return $api;
}

class FeedsMigrateAggregatorExtender extends MigrateUIWizardExtender {
  protected $migrateFeeds = FALSE;

  public function __construct(MigrateD2DWizard $wizard, array $wizard_steps) {
    parent::__construct($wizard, $wizard_steps);
    // If the source has aggregator feeds, add a step to choose whether to
    // import feed configuration.
    // @todo: Verify presence of feeds on source side.
    $wizard->addStep(t('Aggregator feeds'), array($this, 'aggregatorForm'),
                     $wizard_steps[t('Users')]);
  }

  public function aggregatorForm(&$form_state) {
    $form = array();

    $form['feeds_migrate_aggregator'] = array(
      '#type' => 'checkbox',
      '#title' => t("Migrate aggregator feeds to the Feeds module"),
    );

    return $form;
  }

  public function aggregatorFormValidate(&$form_state) {
    $this->migrateFeeds = $form_state['values']['feeds_migrate_aggregator'];
  }

  public function reviewFormAlter(&$form, &$form_state) {
    if ($this->migrateFeeds) {
      $form['import_list']['#markup'] .= '<li>Aggregator feeds will be imported into Feeds.</li>';
      // @todo: $this->wizard->addMigration()
    }
    else {
      $form['non_import_list']['#markup'] .= '<li>Aggregator feeds will not be imported into Feeds.</li>';
    }
  }
}
mikeryan’s picture

See #2331681: Document extending D2D migration to allow contrib modules to add wizard steps - for a form_alter hook to be able to invoke an extender, the wizard class needs to expose its extenders. Adding method getExtender($extender_class).

Bah - swapped tabs and wrote this in the wrong place.

mikeryan’s picture

joachim’s picture

Hmm, the whole of that feeds_migrate_aggregator_form_migrate_ui_wizard_alter() is going to have to be copy-pasted by all other contrib modules -- it's just boilerplate.

mikeryan’s picture

Title: extending D2D migration to allow contrib modules to add wizard steps » Document extending D2D migration to allow contrib modules to add wizard steps
Component: Code » Documentation
Category: Feature request » Task

It wouldn't be identical in all use cases, I would imagine there would frequently be other parts of the wizard besides the review form being altered. Bottom line, building a distinct alter mechanism into migrate_d2d doesn't add functionality, it's simply a convenience to save a few lines in hook_form_alter() - I don't think it's worthwhile.

michaelmallett’s picture

How can I help document this? I've spent a very long time delving through these modules so I can use it extend the wordpress_migrate module, and I'd like to write it down!

sw_msnd’s picture

Help! I am a novice at drupal migration, I am trying to use the drupal2drupal migration feature of the migrate module. I have a database sitting on a different apache webserver, and I am able to connect to it remotely using mysql workbench from my local machine. I have a fresh drupal install on a new webserver and am attempting to use the migrate_d2d wizard, but I keep getting the following error when trying to connect to my remote database:
"Failed to connect to source database, message returned: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES)" Can I actually do this? Also, I have verified my connection information for the remote mysql database.