b32fb58eba087092e2e83e6d3a17f9711baa2660
[yaffs-website] / web / core / modules / migrate_drupal / migrate_drupal.module
1 <?php
2
3 /**
4  * @file
5  * Provides migration from other Drupal sites.
6  */
7
8 use Drupal\Core\Database\DatabaseExceptionWrapper;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\migrate\Exception\RequirementsException;
11 use Drupal\migrate\MigrateExecutable;
12 use Drupal\migrate\Plugin\RequirementsInterface;
13
14 /**
15  * Implements hook_help().
16  */
17 function migrate_drupal_help($route_name, RouteMatchInterface $route_match) {
18   switch ($route_name) {
19     case 'help.page.migrate_drupal':
20       $output = '';
21       $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       return $output;
24   }
25 }
26
27 /**
28  * Implements hook_migration_plugins_alter().
29  */
30 function migrate_drupal_migration_plugins_alter(&$definitions) {
31   // This is why the deriver can't do this: the 'd6_taxonomy_vocabulary'
32   // definition is not available to the deriver as it is running inside
33   // getDefinitions().
34   if (isset($definitions['d6_taxonomy_vocabulary'])) {
35     $vocabulary_migration_definition = [
36       'source' => [
37         'ignore_map' => TRUE,
38         'plugin' => 'd6_taxonomy_vocabulary',
39       ],
40       'destination' => [
41         'plugin' => 'null',
42       ],
43       'idMap' => [
44         'plugin' => 'null',
45       ],
46     ];
47     $vocabulary_migration = \Drupal::service('plugin.manager.migration')->createStubMigration($vocabulary_migration_definition);
48
49     try {
50       $source_plugin = $vocabulary_migration->getSourcePlugin();
51       if ($source_plugin instanceof RequirementsInterface) {
52         $source_plugin->checkRequirements();
53       }
54       $executable = new MigrateExecutable($vocabulary_migration);
55       $process = ['vid' => $definitions['d6_taxonomy_vocabulary']['process']['vid']];
56       foreach ($source_plugin as $row) {
57         $executable->processRow($row, $process);
58         $source_vid = $row->getSourceProperty('vid');
59         $plugin_ids = ['d6_term_node:' . $source_vid, 'd6_term_node_revision:' . $source_vid];
60         foreach ($plugin_ids as $plugin_id) {
61           // Match the field name derivation in d6_vocabulary_field.yml.
62           $field_name = substr('field_' . $row->getDestinationProperty('vid'), 0, 32);
63
64           // The Forum module is expecting 'taxonomy_forums' as the field name
65           // for the forum nodes. The 'forum_vocabulary' source property is
66           // evaluated in Drupal\taxonomy\Plugin\migrate\source\d6\Vocabulary
67           // and is set to true if the vocabulary vid being migrated is the
68           // same as the one in the 'forum_nav_vocabulary' variable on the
69           // source site.
70           $destination_vid = $row->getSourceProperty('forum_vocabulary') ? 'taxonomy_forums' : $field_name;
71           $definitions[$plugin_id]['process'][$destination_vid] = 'tid';
72         }
73       }
74     }
75     catch (RequirementsException $e) {
76       // This code currently runs whenever the definitions are being loaded and
77       // if you have a Drupal 7 source site then the requirements will not be
78       // met for the d6_taxonomy_vocabulary migration.
79     }
80     catch (DatabaseExceptionWrapper $e) {
81       // When the definitions are loaded it is possible the tables will not
82       // exist.
83     }
84
85   }
86 }