migrate.api.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Migration API
  11. * @{
  12. * Overview of the Migration API, which migrates data into Drupal.
  13. *
  14. * @section overview Overview of migration
  15. * Migration is an
  16. * @link http://wikipedia.org/wiki/Extract,_transform,_load Extract, Transform, Load @endlink
  17. * (ETL) process. In the Drupal migration 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 "load" in ETL
  20. * means to load data into storage, while traditionally Drupal uses "load" to
  21. * mean load data from storage into memory.
  22. *
  23. * In the source phase, a set of data, called the row, is retrieved from the
  24. * data source, typically a database but it can be a CSV, JSON or XML file. The
  25. * row is sent to the process phase where it is transformed as needed by the
  26. * destination, or marked to be skipped. Processing can also determine that a
  27. * stub needs to be created, for example, if a term has a parent term that does
  28. * not yet exist. After processing the transformed row is passed to the
  29. * destination phase where it is loaded (saved) into the Drupal 8 site.
  30. *
  31. * The ETL process is configured by the migration plugin. The different phases:
  32. * source, process, and destination are also plugins, and are managed by the
  33. * Migration plugin. So there are four types of plugins in the migration
  34. * process: migration, source, process and destination.
  35. *
  36. * @section sec_migrations Migration plugins
  37. * Migration plugin definitions are stored in a module's 'migrations' directory.
  38. * For backwards compatibility we also scan the 'migration_templates' directory.
  39. * Examples of migration plugin definitions can be found in
  40. * 'core/modules/action/migration_templates'. The plugin class is
  41. * \Drupal\migrate\Plugin\Migration, with interface
  42. * \Drupal\migrate\Plugin\MigrationInterface. Migration plugins are managed by
  43. * the \Drupal\migrate\Plugin\MigrationPluginManager class. Migration plugins
  44. * are only available if the providers of their source plugins are installed.
  45. *
  46. * @section sec_source Source plugins
  47. * Migration source plugins implement
  48. * \Drupal\migrate\Plugin\MigrateSourceInterface and usually extend
  49. * \Drupal\migrate\Plugin\migrate\source\SourcePluginBase. They are annotated
  50. * with \Drupal\migrate\Annotation\MigrateSource annotation, and must be in
  51. * namespace subdirectory Plugin\migrate\source under the namespace of the
  52. * module that defines them. Migration source plugins are managed by the
  53. * \Drupal\migrate\Plugin\MigrateSourcePluginManager class. Source plugin
  54. * providers are determined by their and their parents namespaces.
  55. *
  56. * @section sec_process Process plugins
  57. * Migration process plugins implement
  58. * \Drupal\migrate\Plugin\MigrateProcessInterface and usually extend
  59. * \Drupal\migrate\ProcessPluginBase. They are annotated
  60. * with \Drupal\migrate\Annotation\MigrateProcessPlugin annotation, and must be
  61. * in namespace subdirectory Plugin\migrate\process under the namespace of the
  62. * module that defines them. Migration process plugins are managed by the
  63. * \Drupal\migrate\Plugin\MigratePluginManager class. The Migrate module
  64. * provides process plugins for common operations (setting default values,
  65. * mapping values, etc.).
  66. *
  67. * @section sec_destination Destination plugins
  68. * Migration destination plugins implement
  69. * \Drupal\migrate\Plugin\MigrateDestinationInterface and usually extend
  70. * \Drupal\migrate\Plugin\migrate\destination\DestinationBase. They are
  71. * annotated with \Drupal\migrate\Annotation\MigrateDestination annotation, and
  72. * must be in namespace subdirectory Plugin\migrate\destination under the
  73. * namespace of the module that defines them. Migration destination plugins
  74. * are managed by the \Drupal\migrate\Plugin\MigrateDestinationPluginManager
  75. * class. The Migrate module provides destination plugins for Drupal core
  76. * objects (configuration and entity).
  77. *
  78. * @section sec_more_info More information
  79. * @link https://www.drupal.org/node/2127611 Migration API documentation. @endlink
  80. *
  81. * @see update_api
  82. * @}
  83. */
  84. /**
  85. * @addtogroup hooks
  86. * @{
  87. */
  88. /**
  89. * Allows adding data to a row before processing it.
  90. *
  91. * For example, filter module used to store filter format settings in the
  92. * variables table which now needs to be inside the filter format config
  93. * file. So, it needs to be added here.
  94. *
  95. * hook_migrate_MIGRATION_ID_prepare_row() is also available.
  96. *
  97. * @ingroup migration
  98. */
  99. function hook_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
  100. if ($migration->id() == 'd6_filter_formats') {
  101. $value = $source->getDatabase()->query('SELECT value FROM {variable} WHERE name = :name', [':name' => 'mymodule_filter_foo_' . $row->getSourceProperty('format')])->fetchField();
  102. if ($value) {
  103. $row->setSourceProperty('settings:mymodule:foo', unserialize($value));
  104. }
  105. }
  106. }
  107. /**
  108. * Allows altering the list of discovered migration plugins.
  109. *
  110. * Modules are able to alter specific migrations structures or even remove or
  111. * append additional migrations to the discovery. For example, this
  112. * implementation filters out Drupal 6 migrations from the discovered migration
  113. * list. This is done by checking the migration tags.
  114. *
  115. * @param array[] $migrations
  116. * An associative array of migrations keyed by migration ID. Each value is the
  117. * migration array, obtained by decoding the migration YAML file and enriched
  118. * with some meta information added during discovery phase, like migration
  119. * 'class', 'provider' or '_discovered_file_path'.
  120. *
  121. * @ingroup migration
  122. */
  123. function hook_migration_plugins_alter(array &$migrations) {
  124. $migrations = array_filter($migrations, function (array $migration) {
  125. $tags = isset($migration['migration_tags']) ? (array) $migration['migration_tags'] : [];
  126. return !in_array('Drupal 6', $tags);
  127. });
  128. }
  129. /**
  130. * @} End of "addtogroup hooks".
  131. */