0fb1be413c46353c6da13d76c6d544515a47a5d6
[yaffs-website] / vendor / drush / drush / src / Commands / generate / Generators / Migrate / migration.twig
1 <?php
2 namespace Drupal\{{ machine_name }}\Plugin\migrate\source;
3
4 use Drupal\migrate\Plugin\migrate\source\SqlBase;
5 use Drupal\migrate\Row;
6
7 /**
8  * Migrate Source plugin.
9  *
10  * @MigrateSource(
11  *   id = "{{ plugin_id }}"
12  * )
13  */
14 class {{ class }} extends SqlBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function query() {
20     /**
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()
29      * below.
30      */
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']);
35     return $query;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function fields() {
42     $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'),
57     ];
58
59     return $fields;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getIds() {
66     return [
67       'bid' => [
68         'type' => 'integer',
69         'alias' => 'b',
70       ],
71     ];
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function prepareRow(Row $row) {
78     /**
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).
82      */
83     $terms = $this->select('migrate_example_beer_topic_node', 'bt')
84                  ->fields('bt', ['style'])
85       ->condition('bid', $row->getSourceProperty('bid'))
86       ->execute()
87       ->fetchCol();
88     $row->setSourceProperty('terms', $terms);
89
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));
94     }
95     return parent::prepareRow($row);
96   }
97
98 }