Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / migrate_plus / migrate_example / src / Plugin / migrate / source / BeerNode.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 content.
10  *
11  * @MigrateSource(
12  *   id = "beer_node"
13  * )
14  */
15 class BeerNode extends SqlBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function query() {
21     /**
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()
30      * below.
31      */
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']);
36     return $query;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function fields() {
43     $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'),
58     ];
59
60     return $fields;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function getIds() {
67     return [
68       'bid' => [
69         'type' => 'integer',
70         'alias' => 'b',
71       ],
72     ];
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function prepareRow(Row $row) {
79     /**
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).
83      */
84     $terms = $this->select('migrate_example_beer_topic_node', 'bt')
85                  ->fields('bt', ['style'])
86       ->condition('bid', $row->getSourceProperty('bid'))
87       ->execute()
88       ->fetchCol();
89     $row->setSourceProperty('terms', $terms);
90
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));
95     }
96     return parent::prepareRow($row);
97   }
98
99 }