3 namespace Drupal\migrate_example\Plugin\migrate\source;
5 use Drupal\migrate\Plugin\migrate\source\SqlBase;
6 use Drupal\migrate\Row;
9 * Source plugin for beer user accounts.
15 class BeerUser extends SqlBase {
20 public function query() {
21 return $this->select('migrate_example_beer_account', 'mea')
22 ->fields('mea', ['aid', 'status', 'registered', 'username', 'nickname',
23 'password', 'email', 'sex', 'beers']);
29 public function fields() {
31 'aid' => $this->t('Account ID'),
32 'status' => $this->t('Blocked/Allowed'),
33 'registered' => $this->t('Registered date'),
34 'username' => $this->t('Account name (for login)'),
35 'nickname' => $this->t('Account name (for display)'),
36 'password' => $this->t('Account password (raw)'),
37 'email' => $this->t('Account email'),
38 'sex' => $this->t('Gender'),
39 'beers' => $this->t('Favorite beers, pipe-separated'),
48 public function getIds() {
60 public function prepareRow(Row $row) {
62 * prepareRow() is the most common place to perform custom run-time
63 * processing that isn't handled by an existing process plugin. It is called
64 * when the raw data has been pulled from the source, and provides the
65 * opportunity to modify or add to that data, creating the canonical set of
66 * source data that will be fed into the processing pipeline.
68 * In our particular case, the list of a user's favorite beers is a pipe-
69 * separated list of beer IDs. The processing pipeline deals with arrays
70 * representing multi-value fields naturally, so we want to explode that
71 * string to an array of individual beer IDs.
73 if ($value = $row->getSourceProperty('beers')) {
74 $row->setSourceProperty('beers', explode('|', $value));
77 * Always call your parent! Essential processing is performed in the base
78 * class. Be mindful that prepareRow() returns a boolean status - if FALSE
79 * that indicates that the item being processed should be skipped. Unless
80 * we're deciding to skip an item ourselves, let the parent class decide.
82 return parent::prepareRow($row);