BeerUser.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 user accounts.
  7. *
  8. * @MigrateSource(
  9. * id = "beer_user"
  10. * )
  11. */
  12. class BeerUser extends SqlBase {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function query() {
  17. return $this->select('migrate_example_beer_account', 'mea')
  18. ->fields('mea', ['aid', 'status', 'registered', 'username', 'nickname',
  19. 'password', 'email', 'sex', 'beers']);
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function fields() {
  25. $fields = [
  26. 'aid' => $this->t('Account ID'),
  27. 'status' => $this->t('Blocked/Allowed'),
  28. 'registered' => $this->t('Registered date'),
  29. 'username' => $this->t('Account name (for login)'),
  30. 'nickname' => $this->t('Account name (for display)'),
  31. 'password' => $this->t('Account password (raw)'),
  32. 'email' => $this->t('Account email'),
  33. 'sex' => $this->t('Gender'),
  34. 'beers' => $this->t('Favorite beers, pipe-separated'),
  35. ];
  36. return $fields;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getIds() {
  42. return [
  43. 'aid' => [
  44. 'type' => 'integer',
  45. 'alias' => 'mea',
  46. ],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function prepareRow(Row $row) {
  53. /**
  54. * prepareRow() is the most common place to perform custom run-time
  55. * processing that isn't handled by an existing process plugin. It is called
  56. * when the raw data has been pulled from the source, and provides the
  57. * opportunity to modify or add to that data, creating the canonical set of
  58. * source data that will be fed into the processing pipeline.
  59. *
  60. * In our particular case, the list of a user's favorite beers is a pipe-
  61. * separated list of beer IDs. The processing pipeline deals with arrays
  62. * representing multi-value fields naturally, so we want to explode that
  63. * string to an array of individual beer IDs.
  64. */
  65. if ($value = $row->getSourceProperty('beers')) {
  66. $row->setSourceProperty('beers', explode('|', $value));
  67. }
  68. /**
  69. * Always call your parent! Essential processing is performed in the base
  70. * class. Be mindful that prepareRow() returns a boolean status - if FALSE
  71. * that indicates that the item being processed should be skipped. Unless
  72. * we're deciding to skip an item ourselves, let the parent class decide.
  73. */
  74. return parent::prepareRow($row);
  75. }
  76. }