Commit 7a526044 authored by heddn's avatar heddn Committed by heddn

Issue #2944544 by heddn: Fix failing phpcs testing

parent 81482505
...@@ -2,11 +2,16 @@ ...@@ -2,11 +2,16 @@
/** /**
* @file * @file
* Install file for migrate example module.
*
* Set up source data and destination configuration for the migration example * Set up source data and destination configuration for the migration example
* module. We do this in a separate module so migrate_example itself is a pure * module. We do this in a separate module so migrate_example itself is a pure
* migration module. * migration module.
*/ */
/**
* Implements hook_schema().
*/
function migrate_example_setup_schema() { function migrate_example_setup_schema() {
$schema['migrate_example_beer_account'] = migrate_example_beer_schema_account(); $schema['migrate_example_beer_account'] = migrate_example_beer_schema_account();
$schema['migrate_example_beer_node'] = migrate_example_beer_schema_node(); $schema['migrate_example_beer_node'] = migrate_example_beer_schema_node();
...@@ -17,6 +22,9 @@ function migrate_example_setup_schema() { ...@@ -17,6 +22,9 @@ function migrate_example_setup_schema() {
return $schema; return $schema;
} }
/**
* Implements hook_install().
*/
function migrate_example_setup_install() { function migrate_example_setup_install() {
// Populate our tables. // Populate our tables.
migrate_example_beer_data_account(); migrate_example_beer_data_account();
...@@ -26,6 +34,12 @@ function migrate_example_setup_install() { ...@@ -26,6 +34,12 @@ function migrate_example_setup_install() {
migrate_example_beer_data_topic_node(); migrate_example_beer_data_topic_node();
} }
/**
* The hook_schema definition for node.
*
* @return array
* The schema definition.
*/
function migrate_example_beer_schema_node() { function migrate_example_beer_schema_node() {
return [ return [
'description' => 'Beers of the world.', 'description' => 'Beers of the world.',
...@@ -92,6 +106,12 @@ function migrate_example_beer_schema_node() { ...@@ -92,6 +106,12 @@ function migrate_example_beer_schema_node() {
]; ];
} }
/**
* The hook_schema definition for topic.
*
* @return array
* The schema definition.
*/
function migrate_example_beer_schema_topic() { function migrate_example_beer_schema_topic() {
return [ return [
'description' => 'Categories', 'description' => 'Categories',
...@@ -129,6 +149,12 @@ function migrate_example_beer_schema_topic() { ...@@ -129,6 +149,12 @@ function migrate_example_beer_schema_topic() {
]; ];
} }
/**
* The hook_schema definition for topic node.
*
* @return array
* The schema definition.
*/
function migrate_example_beer_schema_topic_node() { function migrate_example_beer_schema_topic_node() {
return [ return [
'description' => 'Beers topic pairs.', 'description' => 'Beers topic pairs.',
...@@ -149,6 +175,12 @@ function migrate_example_beer_schema_topic_node() { ...@@ -149,6 +175,12 @@ function migrate_example_beer_schema_topic_node() {
]; ];
} }
/**
* The hook_schema definition for comment.
*
* @return array
* The schema definition.
*/
function migrate_example_beer_schema_comment() { function migrate_example_beer_schema_comment() {
return [ return [
'description' => 'Beers comments.', 'description' => 'Beers comments.',
...@@ -202,6 +234,12 @@ function migrate_example_beer_schema_comment() { ...@@ -202,6 +234,12 @@ function migrate_example_beer_schema_comment() {
]; ];
} }
/**
* The hook_schema definition for account.
*
* @return array
* The schema definition.
*/
function migrate_example_beer_schema_account() { function migrate_example_beer_schema_account() {
return [ return [
'description' => 'Beers accounts.', 'description' => 'Beers accounts.',
...@@ -262,16 +300,63 @@ function migrate_example_beer_schema_account() { ...@@ -262,16 +300,63 @@ function migrate_example_beer_schema_account() {
]; ];
} }
/**
* Populate node table.
*/
function migrate_example_beer_data_node() { function migrate_example_beer_data_node() {
$fields = ['bid', 'name', 'body', 'excerpt', 'countries', 'aid', 'image', $fields = [
'image_alt', 'image_title', 'image_description']; 'bid',
'name',
'body',
'excerpt',
'countries',
'aid',
'image',
'image_alt',
'image_title',
'image_description',
];
$query = db_insert('migrate_example_beer_node') $query = db_insert('migrate_example_beer_node')
->fields($fields); ->fields($fields);
// Use high bid numbers to avoid overwriting an existing node id. // Use high bid numbers to avoid overwriting an existing node id.
$data = [ $data = [
[99999999, 'Heineken', 'Blab Blah Blah Green', 'Green', 'Netherlands|Belgium', 0, 'heineken.jpg', 'Heinekin alt', 'Heinekin title', 'Heinekin description'], // comes with migrate_example project. // Comes with migrate_example project.
[99999998, 'Miller Lite', 'We love Miller Brewing', 'Tasteless', 'USA|Canada', 1, NULL, NULL, NULL, NULL], [
[99999997, 'Boddington', 'English occasionally get something right', 'A treat', 'United Kingdom', 1, NULL, NULL, NULL, NULL], 99999999,
'Heineken',
'Blab Blah Blah Green',
'Green',
'Netherlands|Belgium',
0,
'heineken.jpg',
'Heinekin alt',
'Heinekin title',
'Heinekin description',
],
[
99999998,
'Miller Lite',
'We love Miller Brewing',
'Tasteless',
'USA|Canada',
1,
NULL,
NULL,
NULL,
NULL,
],
[
99999997,
'Boddington',
'English occasionally get something right',
'A treat',
'United Kingdom',
1,
NULL,
NULL,
NULL,
NULL,
],
]; ];
foreach ($data as $row) { foreach ($data as $row) {
$query->values(array_combine($fields, $row)); $query->values(array_combine($fields, $row));
...@@ -280,18 +365,65 @@ function migrate_example_beer_data_node() { ...@@ -280,18 +365,65 @@ function migrate_example_beer_data_node() {
} }
/** /**
* Populate account table.
*
* Note that alice has duplicate username. Exercises dedupe_entity plugin. * Note that alice has duplicate username. Exercises dedupe_entity plugin.
*@TODO duplicate email also. * TODO duplicate email also.
*/ */
function migrate_example_beer_data_account() { function migrate_example_beer_data_account() {
$fields = ['status', 'registered', 'username', 'nickname', 'password', 'email', 'sex', 'beers']; $fields = [
'status',
'registered',
'username',
'nickname',
'password',
'email',
'sex',
'beers',
];
$query = db_insert('migrate_example_beer_account') $query = db_insert('migrate_example_beer_account')
->fields($fields); ->fields($fields);
$data = [ $data = [
[1, '2010-03-30 10:31:05', 'alice', 'alice in beerland', 'alicepass', 'alice@example.com', '1', '99999999|99999998|99999997'], [
[1, '2010-04-04 10:31:05', 'alice', 'alice in aleland', 'alicepass', 'alice2@example.com', '1', '99999999|99999998|99999997'], 1,
[0, '2007-03-15 10:31:05', 'bob', 'rebob', 'bobpass', 'bob@example.com', '0', '99999999|99999997'], '2010-03-30 10:31:05',
[1, '2004-02-29 10:31:05', 'charlie', 'charlie chocolate', 'mykids', 'charlie@example.com', '0', '99999999|99999998'], 'alice',
'alice in beerland',
'alicepass',
'alice@example.com',
'1',
'99999999|99999998|99999997',
],
[
1,
'2010-04-04 10:31:05',
'alice',
'alice in aleland',
'alicepass',
'alice2@example.com',
'1',
'99999999|99999998|99999997',
],
[
0,
'2007-03-15 10:31:05',
'bob',
'rebob',
'bobpass',
'bob@example.com',
'0',
'99999999|99999997',
],
[
1,
'2004-02-29 10:31:05',
'charlie',
'charlie chocolate',
'mykids',
'charlie@example.com',
'0',
'99999999|99999998',
],
]; ];
foreach ($data as $row) { foreach ($data as $row) {
$query->values(array_combine($fields, $row)); $query->values(array_combine($fields, $row));
...@@ -299,6 +431,9 @@ function migrate_example_beer_data_account() { ...@@ -299,6 +431,9 @@ function migrate_example_beer_data_account() {
$query->execute(); $query->execute();
} }
/**
* Populate comment table.
*/
function migrate_example_beer_data_comment() { function migrate_example_beer_data_comment() {
$fields = ['bid', 'cid_parent', 'subject', 'body', 'name', 'mail', 'aid']; $fields = ['bid', 'cid_parent', 'subject', 'body', 'name', 'mail', 'aid'];
$query = db_insert('migrate_example_beer_comment') $query = db_insert('migrate_example_beer_comment')
...@@ -308,7 +443,15 @@ function migrate_example_beer_data_comment() { ...@@ -308,7 +443,15 @@ function migrate_example_beer_data_comment() {
[99999998, NULL, 'im second', 'aromatic', 'alice', 'alice@example.com', 0], [99999998, NULL, 'im second', 'aromatic', 'alice', 'alice@example.com', 0],
[99999999, NULL, 'im parent', 'malty', 'alice', 'alice@example.com', 0], [99999999, NULL, 'im parent', 'malty', 'alice', 'alice@example.com', 0],
[99999999, 1, 'im child', 'cold body', 'bob', NULL, 1], [99999999, 1, 'im child', 'cold body', 'bob', NULL, 1],
[99999999, 4, 'im grandchild', 'bitter body', 'charlie@example.com', NULL, 1], [
99999999,
4,
'im grandchild',
'bitter body',
'charlie@example.com',
NULL,
1,
],
]; ];
foreach ($data as $row) { foreach ($data as $row) {
$query->values(array_combine($fields, $row)); $query->values(array_combine($fields, $row));
...@@ -316,6 +459,9 @@ function migrate_example_beer_data_comment() { ...@@ -316,6 +459,9 @@ function migrate_example_beer_data_comment() {
$query->execute(); $query->execute();
} }
/**
* Populate topic table.
*/
function migrate_example_beer_data_topic() { function migrate_example_beer_data_topic() {
$fields = ['style', 'details', 'style_parent', 'region', 'hoppiness']; $fields = ['style', 'details', 'style_parent', 'region', 'hoppiness'];
$query = db_insert('migrate_example_beer_topic') $query = db_insert('migrate_example_beer_topic')
...@@ -323,7 +469,13 @@ function migrate_example_beer_data_topic() { ...@@ -323,7 +469,13 @@ function migrate_example_beer_data_topic() {
$data = [ $data = [
['ale', 'traditional', NULL, 'Medieval British Isles', 'Medium'], ['ale', 'traditional', NULL, 'Medieval British Isles', 'Medium'],
['red ale', 'colorful', 'ale', NULL, NULL], ['red ale', 'colorful', 'ale', NULL, NULL],
['pilsner', 'refreshing', NULL, 'Pilsen, Bohemia (now Czech Republic)', 'Low'], [
'pilsner',
'refreshing',
NULL,
'Pilsen, Bohemia (now Czech Republic)',
'Low',
],
]; ];
foreach ($data as $row) { foreach ($data as $row) {
$query->values(array_combine($fields, $row)); $query->values(array_combine($fields, $row));
...@@ -331,6 +483,9 @@ function migrate_example_beer_data_topic() { ...@@ -331,6 +483,9 @@ function migrate_example_beer_data_topic() {
$query->execute(); $query->execute();
} }
/**
* Populate topic node table.
*/
function migrate_example_beer_data_topic_node() { function migrate_example_beer_data_topic_node() {
$fields = ['bid', 'style']; $fields = ['bid', 'style'];
$query = db_insert('migrate_example_beer_topic_node') $query = db_insert('migrate_example_beer_topic_node')
......
...@@ -17,9 +17,18 @@ class BeerComment extends SqlBase { ...@@ -17,9 +17,18 @@ class BeerComment extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function query() { public function query() {
$fields = [
'cid',
'cid_parent',
'name',
'mail',
'aid',
'body',
'bid',
'subject',
];
$query = $this->select('migrate_example_beer_comment', 'mec') $query = $this->select('migrate_example_beer_comment', 'mec')
->fields('mec', ['cid', 'cid_parent', 'name', 'mail', 'aid', ->fields('mec', $fields)
'body', 'bid', 'subject'])
->orderBy('cid_parent', 'ASC'); ->orderBy('cid_parent', 'ASC');
return $query; return $query;
} }
......
...@@ -18,21 +18,29 @@ class BeerNode extends SqlBase { ...@@ -18,21 +18,29 @@ class BeerNode extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function query() { public function query() {
/** // An important point to note is that your query *must* return a single row
* An important point to note is that your query *must* return a single row // for each item to be imported. Here we might be tempted to add a join to
* for each item to be imported. Here we might be tempted to add a join to // migrate_example_beer_topic_node in our query, to pull in the
* migrate_example_beer_topic_node in our query, to pull in the // relationships to our categories. Doing this would cause the query to
* relationships to our categories. Doing this would cause the query to // return multiple rows for a given node, once per related value, thus
* return multiple rows for a given node, once per related value, thus // processing the same node multiple times, each time with only one of the
* processing the same node multiple times, each time with only one of the // multiple values that should be imported. To avoid that, we simply query
* multiple values that should be imported. To avoid that, we simply query // the base node data here, and pull in the relationships in prepareRow()
* the base node data here, and pull in the relationships in prepareRow() // below.
* below. $fields = [
*/ 'bid',
'name',
'body',
'excerpt',
'aid',
'countries',
'image',
'image_alt',
'image_title',
'image_description',
];
$query = $this->select('migrate_example_beer_node', 'b') $query = $this->select('migrate_example_beer_node', 'b')
->fields('b', ['bid', 'name', 'body', 'excerpt', 'aid', ->fields('b', $fields);
'countries', 'image', 'image_alt', 'image_title',
'image_description']);
return $query; return $query;
} }
...@@ -76,11 +84,9 @@ class BeerNode extends SqlBase { ...@@ -76,11 +84,9 @@ class BeerNode extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function prepareRow(Row $row) { public function prepareRow(Row $row) {
/** // As explained above, we need to pull the style relationships into our
* As explained above, we need to pull the style relationships into our // source row here, as an array of 'style' values (the unique ID for
* source row here, as an array of 'style' values (the unique ID for // the beer_term migration).
* the beer_term migration).
*/
$terms = $this->select('migrate_example_beer_topic_node', 'bt') $terms = $this->select('migrate_example_beer_topic_node', 'bt')
->fields('bt', ['style']) ->fields('bt', ['style'])
->condition('bid', $row->getSourceProperty('bid')) ->condition('bid', $row->getSourceProperty('bid'))
......
...@@ -5,11 +5,12 @@ namespace Drupal\migrate_example\Plugin\migrate\source; ...@@ -5,11 +5,12 @@ namespace Drupal\migrate_example\Plugin\migrate\source;
use Drupal\migrate\Plugin\migrate\source\SqlBase; use Drupal\migrate\Plugin\migrate\source\SqlBase;
/** /**
* This is an example of a simple SQL-based source plugin. Source plugins are * This is an example of a simple SQL-based source plugin.
* classes which deliver source data to the processing pipeline. For SQL *
* sources, the SqlBase class provides most of the functionality needed - for * Source plugins are classes which deliver source data to the processing
* a specific migration, you are required to implement the three simple public * pipeline. For SQL sources, the SqlBase class provides most of the
* methods you see below. * functionality needed - for a specific migration, you are required to
* implement the three simple public methods you see below.
* *
* This annotation tells Drupal that the name of the MigrateSource plugin * This annotation tells Drupal that the name of the MigrateSource plugin
* implemented by this class is "beer_term". This is the name that the migration * implemented by this class is "beer_term". This is the name that the migration
...@@ -25,16 +26,15 @@ class BeerTerm extends SqlBase { ...@@ -25,16 +26,15 @@ class BeerTerm extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function query() { public function query() {
/** // The most important part of a SQL source plugin is the SQL query to
* The most important part of a SQL source plugin is the SQL query to // retrieve the data to be imported. Note that the query is not executed
* retrieve the data to be imported. Note that the query is not executed // here - the migration process will control execution of the query. Also
* here - the migration process will control execution of the query. Also // note that it is constructed from a $this->select() call - this ensures
* note that it is constructed from a $this->select() call - this ensures // that the query is executed against the database configured for this
* that the query is executed against the database configured for this // source plugin.
* source plugin. $fields = ['style', 'details', 'style_parent', 'region', 'hoppiness'];
*/
return $this->select('migrate_example_beer_topic', 'met') return $this->select('migrate_example_beer_topic', 'met')
->fields('met', ['style', 'details', 'style_parent', 'region', 'hoppiness']) ->fields('met', $fields)
// We sort this way to ensure parent terms are imported first. // We sort this way to ensure parent terms are imported first.
->orderBy('style_parent', 'ASC'); ->orderBy('style_parent', 'ASC');
} }
...@@ -43,12 +43,10 @@ class BeerTerm extends SqlBase { ...@@ -43,12 +43,10 @@ class BeerTerm extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function fields() { public function fields() {
/** // This method simply documents the available source fields provided by the
* This method simply documents the available source fields provided by // source plugin, for use by front-end tools. It returns an array keyed by
* the source plugin, for use by front-end tools. It returns an array keyed // field/column name, with the value being a translated string explaining
* by field/column name, with the value being a translated string explaining // to humans what the field represents.
* to humans what the field represents. You should always
*/
$fields = [ $fields = [
'style' => $this->t('Beer style'), 'style' => $this->t('Beer style'),
'details' => $this->t('Style details'), 'details' => $this->t('Style details'),
...@@ -66,14 +64,12 @@ class BeerTerm extends SqlBase { ...@@ -66,14 +64,12 @@ class BeerTerm extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getIds() { public function getIds() {
/** // This method indicates what field(s) from the source row uniquely identify
* This method indicates what field(s) from the source row uniquely identify // that source row, and what their types are. This is critical information
* that source row, and what their types are. This is critical information // for managing the migration. The keys of the returned array are the field
* for managing the migration. The keys of the returned array are the field // names from the query which comprise the unique identifier. The values are
* names from the query which comprise the unique identifier. The values are // arrays indicating the type of the field, used for creating compatible
* arrays indicating the type of the field, used for creating compatible // columns in the map tables that track processed items.
* columns in the map tables that track processed items.
*/
return [ return [
'style' => [ 'style' => [
'type' => 'string', 'type' => 'string',
......
...@@ -18,9 +18,19 @@ class BeerUser extends SqlBase { ...@@ -18,9 +18,19 @@ class BeerUser extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function query() { public function query() {
$fields = [
'aid',
'status',
'registered',
'username',
'nickname',
'password',
'email',
'sex',
'beers',
];
return $this->select('migrate_example_beer_account', 'mea') return $this->select('migrate_example_beer_account', 'mea')
->fields('mea', ['aid', 'status', 'registered', 'username', 'nickname', ->fields('mea', $fields);
'password', 'email', 'sex', 'beers']);
} }
/** /**
...@@ -58,27 +68,22 @@ class BeerUser extends SqlBase { ...@@ -58,27 +68,22 @@ class BeerUser extends SqlBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function prepareRow(Row $row) { public function prepareRow(Row $row) {
/** // A prepareRow() is the most common place to perform custom run-time
* prepareRow() is the most common place to perform custom run-time // processing that isn't handled by an existing process plugin. It is called
* processing that isn't handled by an existing process plugin. It is called // when the raw data has been pulled from the source, and provides the
* when the raw data has been pulled from the source, and provides the // opportunity to modify or add to that data, creating the canonical set of
* opportunity to modify or add to that data, creating the canonical set of // source data that will be fed into the processing pipeline.
* source data that will be fed into the processing pipeline. // In our particular case, the list of a user's favorite beers is a pipe-
* // separated list of beer IDs. The processing pipeline deals with arrays
* In our particular case, the list of a user's favorite beers is a pipe- // representing multi-value fields naturally, so we want to explode that
* separated list of beer IDs. The processing pipeline deals with arrays // string to an array of individual beer IDs.
* representing multi-value fields naturally, so we want to explode that
* string to an array of individual beer IDs.
*/
if ($value = $row->getSourceProperty('beers')) { if ($value = $row->getSourceProperty('beers')) {
$row->setSourceProperty('beers', explode('|', $value)); $row->setSourceProperty('beers', explode('|', $value));
} }
/** // Always call your parent! Essential processing is performed in the base
* Always call your parent! Essential processing is performed in the base // class. Be mindful that prepareRow() returns a boolean status - if FALSE
* class. Be mindful that prepareRow() returns a boolean status - if FALSE // that indicates that the item being processed should be skipped. Unless
* that indicates that the item being processed should be skipped. Unless // we're deciding to skip an item ourselves, let the parent class decide.
* we're deciding to skip an item ourselves, let the parent class decide.
*/
return parent::prepareRow($row); return parent::prepareRow($row);
} }
......
...@@ -2,20 +2,20 @@ ...@@ -2,20 +2,20 @@
/** /**
* @file * @file
* Install, update and uninstall functions for the migrate_example_advanced module. * Install, update and uninstall functions for migrate_example_advanced module.
*/ */
use Drupal\migrate_plus\Entity\Migration; use Drupal\migrate_plus\Entity\Migration;
/** /**
* Implements hook_install(). * Implements hook_install().
*
* We need the urls to be absolute for the XML source plugin to read them, but
* the static configuration files on disk can't know the server and port to
* use. So, in the .yml files we provide the REST resources relative to the
* site root and here rewrite them to fully-qualified paths.
*/ */
function migrate_example_advanced_install() { function migrate_example_advanced_install() {
// We need the urls to be absolute for the XML source plugin to read them, but
// the static configuration files on disk can't know the server and port to
// use. So, in the .yml files we provide the REST resources relative to the
// site root and here rewrite them to fully-qualified paths.
/** @var \Drupal\migrate_plus\Entity\MigrationInterface $wine_role_xml_migration */ /** @var \Drupal\migrate_plus\Entity\MigrationInterface $wine_role_xml_migration */
$wine_role_xml_migration = Migration::load('wine_role_xml'); $wine_role_xml_migration = Migration::load('wine_role_xml');
if ($wine_role_xml_migration) { if ($wine_role_xml_migration) {
......
...@@ -31,22 +31,26 @@ class VarietyItems extends ResourceBase { ...@@ -31,22 +31,26 @@ class VarietyItems extends ResourceBase {
$varieties = [ $varieties = [
'retsina' => [ 'retsina' => [
'name' => 'Retsina', 'name' => 'Retsina',
'parent' => 1, // categoryid for 'white'. // The categoryid for 'white'.
'parent' => 1,
'details' => 'Greek', 'details' => 'Greek',
], ],
'trebbiano' => [ 'trebbiano' => [
'name' => 'Trebbiano', 'name' => 'Trebbiano',
'parent' => 1, // categoryid for 'white'. // The categoryid for 'white'.
'parent' => 1,
'details' => 'Italian', 'details' => 'Italian',
], ],
'valpolicella' => [ 'valpolicella' => [
'name' => 'Valpolicella', 'name' => 'Valpolicella',
'parent' => 3, // categoryid for 'red'. // The categoryid for 'red'.
'parent' => 3,
'details' => 'Italian Venoto region', 'details' => 'Italian Venoto region',
], ],
'bardolino' => [ 'bardolino' => [
'name' => 'Bardolino', 'name' => 'Bardolino',