migrate.api.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Migrate module.
  5. */
  6. use Drupal\migrate\Plugin\MigrationInterface;
  7. use Drupal\migrate\Plugin\MigrateSourceInterface;
  8. use Drupal\migrate\Row;
  9. /**
  10. * @defgroup migration Migrate API
  11. * @{
  12. * Overview of the Migrate API, which migrates data into Drupal.
  13. *
  14. * @section overview Overview of a migration
  15. * Migration is an
  16. * @link http://wikipedia.org/wiki/Extract,_transform,_load Extract, Transform, Load @endlink
  17. * (ETL) process. In the Drupal Migrate API, the extract phase is called
  18. * 'source', the transform phase is called 'process', and the load phase is
  19. * called 'destination'. It is important to understand that the term 'load' in
  20. * ETL refers to loading data into the storage while in a typical Drupal context
  21. * the term 'load' refers to loading data from storage.
  22. *
  23. * In the source phase, a set of data, called the row, is retrieved from the
  24. * data source. The data can be migrated from a database, loaded from a file
  25. * (for example CSV, JSON or XML) or fetched from a web service (for example RSS
  26. * or REST). The row is sent to the process phase where it is transformed as
  27. * needed or marked to be skipped. Processing can also determine if a 'stub'
  28. * needs to be created. For example, if a term has a parent term which hasn't
  29. * been migrated yet, a stub term is created so that the parent relation can be
  30. * established, and the stub is updated at a later point. After processing, the
  31. * transformed row is passed to the destination phase where it is loaded (saved)
  32. * into the target Drupal site.
  33. *
  34. * Migrate API uses the Drupal plugin system for many different purposes. Most
  35. * importantly, the overall ETL process is defined as a migration plugin and the
  36. * three phases (source, process and destination) have their own plugin types.
  37. *
  38. * @section sec_migrations Migrate API migration plugins
  39. * Migration plugin definitions are stored in a module's 'migrations' directory.
  40. * The plugin class is \Drupal\migrate\Plugin\Migration, with interface
  41. * \Drupal\migrate\Plugin\MigrationInterface. Migration plugins are managed by
  42. * the \Drupal\migrate\Plugin\MigrationPluginManager class. Migration plugins
  43. * are only available if the providers of their source plugins are installed.
  44. *
  45. * @link https://www.drupal.org/docs/8/api/migrate-api/migrate-destination-plugins-examples Example migrations in Migrate API handbook. @endlink
  46. *
  47. * @section sec_source Migrate API source plugins
  48. * Migrate API source plugins implement
  49. * \Drupal\migrate\Plugin\MigrateSourceInterface and usually extend
  50. * \Drupal\migrate\Plugin\migrate\source\SourcePluginBase. They are annotated
  51. * with \Drupal\migrate\Annotation\MigrateSource annotation and must be in
  52. * namespace subdirectory 'Plugin\migrate\source' under the namespace of the
  53. * module that defines them. Migrate API source plugins are managed by the
  54. * \Drupal\migrate\Plugin\MigrateSourcePluginManager class.
  55. *
  56. * @link https://api.drupal.org/api/drupal/namespace/Drupal!migrate!Plugin!migrate!source List of source plugins provided by the core Migrate module. @endlink
  57. * @link https://www.drupal.org/docs/8/api/migrate-api/migrate-source-plugins Core and contributed source plugin usage examples in Migrate API handbook. @endlink
  58. *
  59. * @section sec_process Migrate API process plugins
  60. * Migrate API process plugins implement
  61. * \Drupal\migrate\Plugin\MigrateProcessInterface and usually extend
  62. * \Drupal\migrate\ProcessPluginBase. They are annotated with
  63. * \Drupal\migrate\Annotation\MigrateProcessPlugin annotation and must be in
  64. * namespace subdirectory 'Plugin\migrate\process' under the namespace of the
  65. * module that defines them. Migrate API process plugins are managed by the
  66. * \Drupal\migrate\Plugin\MigratePluginManager class.
  67. *
  68. * @link https://api.drupal.org/api/drupal/namespace/Drupal!migrate!Plugin!migrate!process List of process plugins for common operations provided by the core Migrate module. @endlink
  69. *
  70. * @section sec_destination Migrate API destination plugins
  71. * Migrate API destination plugins implement
  72. * \Drupal\migrate\Plugin\MigrateDestinationInterface and usually extend
  73. * \Drupal\migrate\Plugin\migrate\destination\DestinationBase. They are
  74. * annotated with \Drupal\migrate\Annotation\MigrateDestination annotation and
  75. * must be in namespace subdirectory 'Plugin\migrate\destination' under the
  76. * namespace of the module that defines them. Migrate API destination plugins
  77. * are managed by the \Drupal\migrate\Plugin\MigrateDestinationPluginManager
  78. * class.
  79. *
  80. * @link https://api.drupal.org/api/drupal/namespace/Drupal!migrate!Plugin!migrate!destination List of destination plugins for Drupal configuration and content entities provided by the core Migrate module. @endlink
  81. *
  82. * @section sec_more_info Documentation handbooks
  83. * @link https://www.drupal.org/docs/8/api/migrate-api Migrate API handbook. @endlink
  84. * @link https://www.drupal.org/docs/8/upgrade Upgrading to Drupal 8 handbook. @endlink
  85. * @}
  86. */
  87. /**
  88. * @addtogroup hooks
  89. * @{
  90. */
  91. /**
  92. * Allows adding data to a row before processing it.
  93. *
  94. * For example, filter module used to store filter format settings in the
  95. * variables table which now needs to be inside the filter format config
  96. * file. So, it needs to be added here.
  97. *
  98. * hook_migrate_MIGRATION_ID_prepare_row() is also available.
  99. *
  100. * @param \Drupal\migrate\Row $row
  101. * The row being imported.
  102. * @param \Drupal\migrate\Plugin\MigrateSourceInterface $source
  103. * The source migration.
  104. * @param \Drupal\migrate\Plugin\MigrationInterface $migration
  105. * The current migration.
  106. *
  107. * @ingroup migration
  108. */
  109. function hook_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
  110. if ($migration->id() == 'd6_filter_formats') {
  111. $value = $source->getDatabase()->query('SELECT value FROM {variable} WHERE name = :name', [':name' => 'mymodule_filter_foo_' . $row->getSourceProperty('format')])->fetchField();
  112. if ($value) {
  113. $row->setSourceProperty('settings:mymodule:foo', unserialize($value));
  114. }
  115. }
  116. }
  117. /**
  118. * Allows adding data to a row for a migration with the specified ID.
  119. *
  120. * This provides the same functionality as hook_migrate_prepare_row() but
  121. * removes the need to check the value of $migration->id().
  122. *
  123. * @param \Drupal\migrate\Row $row
  124. * The row being imported.
  125. * @param \Drupal\migrate\Plugin\MigrateSourceInterface $source
  126. * The source migration.
  127. * @param \Drupal\migrate\Plugin\MigrationInterface $migration
  128. * The current migration.
  129. *
  130. * @ingroup migration
  131. */
  132. function hook_migrate_MIGRATION_ID_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
  133. $value = $source->getDatabase()->query('SELECT value FROM {variable} WHERE name = :name', [':name' => 'mymodule_filter_foo_' . $row->getSourceProperty('format')])->fetchField();
  134. if ($value) {
  135. $row->setSourceProperty('settings:mymodule:foo', unserialize($value));
  136. }
  137. }
  138. /**
  139. * Allows altering the list of discovered migration plugins.
  140. *
  141. * Modules are able to alter specific migrations structures or even remove or
  142. * append additional migrations to the discovery. For example, this
  143. * implementation filters out Drupal 6 migrations from the discovered migration
  144. * list. This is done by checking the migration tags.
  145. *
  146. * @param array[] $migrations
  147. * An associative array of migrations keyed by migration ID. Each value is the
  148. * migration array, obtained by decoding the migration YAML file and enriched
  149. * with some meta information added during discovery phase, like migration
  150. * 'class', 'provider' or '_discovered_file_path'.
  151. *
  152. * @ingroup migration
  153. */
  154. function hook_migration_plugins_alter(array &$migrations) {
  155. $migrations = array_filter($migrations, function (array $migration) {
  156. $tags = isset($migration['migration_tags']) ? (array) $migration['migration_tags'] : [];
  157. return !in_array('Drupal 6', $tags);
  158. });
  159. }
  160. /**
  161. * @} End of "addtogroup hooks".
  162. */