BeerTerm.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Drupal\migrate_example\Plugin\migrate\source;
  3. use Drupal\migrate\Plugin\migrate\source\SqlBase;
  4. /**
  5. * This is an example of a simple SQL-based source plugin. Source plugins are
  6. * classes which deliver source data to the processing pipeline. For SQL
  7. * sources, the SqlBase class provides most of the functionality needed - for
  8. * a specific migration, you are required to implement the three simple public
  9. * methods you see below.
  10. *
  11. * This annotation tells Drupal that the name of the MigrateSource plugin
  12. * implemented by this class is "beer_term". This is the name that the migration
  13. * configuration references with the source "plugin" key.
  14. *
  15. * @MigrateSource(
  16. * id = "beer_term"
  17. * )
  18. */
  19. class BeerTerm extends SqlBase {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function query() {
  24. /**
  25. * The most important part of a SQL source plugin is the SQL query to
  26. * retrieve the data to be imported. Note that the query is not executed
  27. * here - the migration process will control execution of the query. Also
  28. * note that it is constructed from a $this->select() call - this ensures
  29. * that the query is executed against the database configured for this
  30. * source plugin.
  31. */
  32. return $this->select('migrate_example_beer_topic', 'met')
  33. ->fields('met', ['style', 'details', 'style_parent', 'region', 'hoppiness'])
  34. // We sort this way to ensure parent terms are imported first.
  35. ->orderBy('style_parent', 'ASC');
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function fields() {
  41. /**
  42. * This method simply documents the available source fields provided by
  43. * the source plugin, for use by front-end tools. It returns an array keyed
  44. * by field/column name, with the value being a translated string explaining
  45. * to humans what the field represents. You should always
  46. */
  47. $fields = [
  48. 'style' => $this->t('Account ID'),
  49. 'details' => $this->t('Blocked/Allowed'),
  50. 'style_parent' => $this->t('Registered date'),
  51. // These values are not currently migrated - it's OK to skip fields you
  52. // don't need.
  53. 'region' => $this->t('Region the style is associated with'),
  54. 'hoppiness' => $this->t('Hoppiness of the style'),
  55. ];
  56. return $fields;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getIds() {
  62. /**
  63. * This method indicates what field(s) from the source row uniquely identify
  64. * that source row, and what their types are. This is critical information
  65. * for managing the migration. The keys of the returned array are the field
  66. * names from the query which comprise the unique identifier. The values are
  67. * arrays indicating the type of the field, used for creating compatible
  68. * columns in the map tables that track processed items.
  69. */
  70. return [
  71. 'style' => [
  72. 'type' => 'string',
  73. // 'alias' is the alias for the table containing 'style' in the query
  74. // defined above. Optional in this case, but necessary if the same
  75. // column may occur in multiple tables in a join.
  76. 'alias' => 'met',
  77. ],
  78. ];
  79. }
  80. }