migrate_drupal.module 3.56 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Provides migration from other Drupal sites.
 */

8
use Drupal\Core\Database\DatabaseExceptionWrapper;
9
use Drupal\Core\Routing\RouteMatchInterface;
10 11 12
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\RequirementsInterface;
13 14 15 16 17 18 19 20 21

/**
 * Implements hook_help().
 */
function migrate_drupal_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.migrate_drupal':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
22
      $output .= '<p>' . t('The Migrate Drupal module provides a framework based on the <a href=":migrate">Migrate module</a> to facilitate migration from a Drupal (6, 7, or 8) site to your website. It does not provide a user interface. For more information, see the <a href=":migrate_drupal">online documentation for the Migrate Drupal module</a>.', [':migrate' => \Drupal::url('help.page', ['name' => 'migrate']), ':migrate_drupal' => 'https://www.drupal.org/documentation/modules/migrate_drupal']) . '</p>';
23 24 25
      return $output;
  }
}
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * Implements hook_migration_plugins_alter().
 */
function migrate_drupal_migration_plugins_alter(&$definitions) {
  // This is why the deriver can't do this: the 'd6_taxonomy_vocabulary'
  // definition is not available to the deriver as it is running inside
  // getDefinitions().
  if (isset($definitions['d6_taxonomy_vocabulary'])) {
    $vocabulary_migration_definition = [
      'source' => [
        'ignore_map' => TRUE,
        'plugin' => 'd6_taxonomy_vocabulary',
      ],
      'destination' => [
        'plugin' => 'null',
      ],
43 44 45
      'idMap' => [
        'plugin' => 'null',
      ],
46
    ];
47
    $vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($vocabulary_migration_definition);
48 49 50 51 52 53

    try {
      $source_plugin = $vocabulary_migration->getSourcePlugin();
      if ($source_plugin instanceof RequirementsInterface) {
        $source_plugin->checkRequirements();
      }
54
      $executable = new MigrateExecutable($vocabulary_migration);
55 56 57 58 59 60
      $process = ['vid' => $definitions['d6_taxonomy_vocabulary']['process']['vid']];
      foreach ($source_plugin as $row) {
        $executable->processRow($row, $process);
        $source_vid = $row->getSourceProperty('vid');
        $plugin_ids = ['d6_term_node:' . $source_vid, 'd6_term_node_revision:' . $source_vid];
        foreach ($plugin_ids as $plugin_id) {
61 62
          // Match the field name derivation in d6_vocabulary_field.yml.
          $field_name = substr('field_' . $row->getDestinationProperty('vid'), 0, 32);
63 64 65 66 67 68 69 70 71

          // The Forum module is expecting 'taxonomy_forums' as the field name
          // for the forum nodes. The 'forum_vocabulary' source property is
          // evaluated in Drupal\taxonomy\Plugin\migrate\source\d6\Vocabulary
          // and is set to true if the vocabulary vid being migrated is the
          // same as the one in the 'forum_nav_vocabulary' variable on the
          // source site.
          $destination_vid = $row->getSourceProperty('forum_vocabulary') ? 'taxonomy_forums' : $field_name;
          $definitions[$plugin_id]['process'][$destination_vid] = 'tid';
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        }
      }
    }
    catch (RequirementsException $e) {
      // This code currently runs whenever the definitions are being loaded and
      // if you have a Drupal 7 source site then the requirements will not be
      // met for the d6_taxonomy_vocabulary migration.
    }
    catch (DatabaseExceptionWrapper $e) {
      // When the definitions are loaded it is possible the tables will not
      // exist.
    }

  }
}