2 namespace Drupal\{{ machine_name }}\Plugin\migrate\source;
4 use Drupal\migrate\Plugin\migrate\source\SqlBase;
5 use Drupal\migrate\Row;
8 * Migrate Source plugin.
11 * id = "{{ plugin_id }}"
14 class {{ class }} extends SqlBase {
19 public function query() {
21 * An important point to note is that your query *must* return a single row
22 * for each item to be imported. Here we might be tempted to add a join to
23 * migrate_example_beer_topic_node in our query, to pull in the
24 * relationships to our categories. Doing this would cause the query to
25 * return multiple rows for a given node, once per related value, thus
26 * processing the same node multiple times, each time with only one of the
27 * multiple values that should be imported. To avoid that, we simply query
28 * the base node data here, and pull in the relationships in prepareRow()
31 $query = $this->select('migrate_example_beer_node', 'b')
32 ->fields('b', ['bid', 'name', 'body', 'excerpt', 'aid',
33 'countries', 'image', 'image_alt', 'image_title',
34 'image_description']);
41 public function fields() {
43 'bid' => $this->t('Beer ID'),
44 'name' => $this->t('Name of beer'),
45 'body' => $this->t('Full description of the beer'),
46 'excerpt' => $this->t('Abstract for this beer'),
47 'aid' => $this->t('Account ID of the author'),
48 'countries' => $this->t('Countries of origin. Multiple values, delimited by pipe'),
49 'image' => $this->t('Image path'),
50 'image_alt' => $this->t('Image ALT'),
51 'image_title' => $this->t('Image title'),
52 'image_description' => $this->t('Image description'),
53 // Note that this field is not part of the query above - it is populated
54 // by prepareRow() below. You should document all source properties that
55 // are available for mapping after prepareRow() is called.
56 'terms' => $this->t('Applicable styles'),
65 public function getIds() {
77 public function prepareRow(Row $row) {
79 * As explained above, we need to pull the style relationships into our
80 * source row here, as an array of 'style' values (the unique ID for
81 * the beer_term migration).
83 $terms = $this->select('migrate_example_beer_topic_node', 'bt')
84 ->fields('bt', ['style'])
85 ->condition('bid', $row->getSourceProperty('bid'))
88 $row->setSourceProperty('terms', $terms);
90 // As we did for favorite beers in the user migration, we need to explode
91 // the multi-value country names.
92 if ($value = $row->getSourceProperty('countries')) {
93 $row->setSourceProperty('countries', explode('|', $value));
95 return parent::prepareRow($row);