BeerNode.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\migrate_example\Plugin\migrate\source;
  3. use Drupal\migrate\Plugin\migrate\source\SqlBase;
  4. use Drupal\migrate\Row;
  5. /**
  6. * Source plugin for beer content.
  7. *
  8. * @MigrateSource(
  9. * id = "beer_node"
  10. * )
  11. */
  12. class BeerNode extends SqlBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function query() {
  17. /**
  18. * An important point to note is that your query *must* return a single row
  19. * for each item to be imported. Here we might be tempted to add a join to
  20. * migrate_example_beer_topic_node in our query, to pull in the
  21. * relationships to our categories. Doing this would cause the query to
  22. * return multiple rows for a given node, once per related value, thus
  23. * processing the same node multiple times, each time with only one of the
  24. * multiple values that should be imported. To avoid that, we simply query
  25. * the base node data here, and pull in the relationships in prepareRow()
  26. * below.
  27. */
  28. $query = $this->select('migrate_example_beer_node', 'b')
  29. ->fields('b', ['bid', 'name', 'body', 'excerpt', 'aid',
  30. 'countries', 'image', 'image_alt', 'image_title',
  31. 'image_description']);
  32. return $query;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function fields() {
  38. $fields = [
  39. 'bid' => $this->t('Beer ID'),
  40. 'name' => $this->t('Name of beer'),
  41. 'body' => $this->t('Full description of the beer'),
  42. 'excerpt' => $this->t('Abstract for this beer'),
  43. 'aid' => $this->t('Account ID of the author'),
  44. 'countries' => $this->t('Countries of origin. Multiple values, delimited by pipe'),
  45. 'image' => $this->t('Image path'),
  46. 'image_alt' => $this->t('Image ALT'),
  47. 'image_title' => $this->t('Image title'),
  48. 'image_description' => $this->t('Image description'),
  49. // Note that this field is not part of the query above - it is populated
  50. // by prepareRow() below. You should document all source properties that
  51. // are available for mapping after prepareRow() is called.
  52. 'terms' => $this->t('Applicable styles'),
  53. ];
  54. return $fields;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getIds() {
  60. return [
  61. 'bid' => [
  62. 'type' => 'integer',
  63. 'alias' => 'b',
  64. ],
  65. ];
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function prepareRow(Row $row) {
  71. /**
  72. * As explained above, we need to pull the style relationships into our
  73. * source row here, as an array of 'style' values (the unique ID for
  74. * the beer_term migration).
  75. */
  76. $terms = $this->select('migrate_example_beer_topic_node', 'bt')
  77. ->fields('bt', ['style'])
  78. ->condition('bid', $row->getSourceProperty('bid'))
  79. ->execute()
  80. ->fetchCol();
  81. $row->setSourceProperty('terms', $terms);
  82. // As we did for favorite beers in the user migration, we need to explode
  83. // the multi-value country names.
  84. if ($value = $row->getSourceProperty('countries')) {
  85. $row->setSourceProperty('countries', explode('|', $value));
  86. }
  87. return parent::prepareRow($row);
  88. }
  89. }