b0d963d3fd00946b03390bae7c185dcbbb576235
[yaffs-website] / web / modules / contrib / migrate_plus / migrate_example / src / Plugin / migrate / source / BeerUser.php
1 <?php
2
3 namespace Drupal\migrate_example\Plugin\migrate\source;
4
5 use Drupal\migrate\Plugin\migrate\source\SqlBase;
6 use Drupal\migrate\Row;
7
8 /**
9  * Source plugin for beer user accounts.
10  *
11  * @MigrateSource(
12  *   id = "beer_user"
13  * )
14  */
15 class BeerUser extends SqlBase {
16
17   /**
18    * {@inheritdoc}
19    */
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']);
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function fields() {
30     $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'),
40     ];
41
42     return $fields;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getIds() {
49     return [
50       'aid' => [
51         'type' => 'integer',
52         'alias' => 'mea',
53       ],
54     ];
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function prepareRow(Row $row) {
61     /**
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.
67      *
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.
72      */
73     if ($value = $row->getSourceProperty('beers')) {
74       $row->setSourceProperty('beers', explode('|', $value));
75     }
76     /**
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.
81      */
82     return parent::prepareRow($row);
83   }
84
85 }