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 content.
15 class BeerNode extends SqlBase {
20 public function query() {
22 * An important point to note is that your query *must* return a single row
23 * for each item to be imported. Here we might be tempted to add a join to
24 * migrate_example_beer_topic_node in our query, to pull in the
25 * relationships to our categories. Doing this would cause the query to
26 * return multiple rows for a given node, once per related value, thus
27 * processing the same node multiple times, each time with only one of the
28 * multiple values that should be imported. To avoid that, we simply query
29 * the base node data here, and pull in the relationships in prepareRow()
32 $query = $this->select('migrate_example_beer_node', 'b')
33 ->fields('b', ['bid', 'name', 'body', 'excerpt', 'aid',
34 'countries', 'image', 'image_alt', 'image_title',
35 'image_description']);
42 public function fields() {
44 'bid' => $this->t('Beer ID'),
45 'name' => $this->t('Name of beer'),
46 'body' => $this->t('Full description of the beer'),
47 'excerpt' => $this->t('Abstract for this beer'),
48 'aid' => $this->t('Account ID of the author'),
49 'countries' => $this->t('Countries of origin. Multiple values, delimited by pipe'),
50 'image' => $this->t('Image path'),
51 'image_alt' => $this->t('Image ALT'),
52 'image_title' => $this->t('Image title'),
53 'image_description' => $this->t('Image description'),
54 // Note that this field is not part of the query above - it is populated
55 // by prepareRow() below. You should document all source properties that
56 // are available for mapping after prepareRow() is called.
57 'terms' => $this->t('Applicable styles'),
66 public function getIds() {
78 public function prepareRow(Row $row) {
80 * As explained above, we need to pull the style relationships into our
81 * source row here, as an array of 'style' values (the unique ID for
82 * the beer_term migration).
84 $terms = $this->select('migrate_example_beer_topic_node', 'bt')
85 ->fields('bt', ['style'])
86 ->condition('bid', $row->getSourceProperty('bid'))
89 $row->setSourceProperty('terms', $terms);
91 // As we did for favorite beers in the user migration, we need to explode
92 // the multi-value country names.
93 if ($value = $row->getSourceProperty('countries')) {
94 $row->setSourceProperty('countries', explode('|', $value));
96 return parent::prepareRow($row);