8cee6ddbe4d5a7ef0417387c1b78fd32bbe087f5
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / source / EmbeddedDataSource.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\source;
4
5 use Drupal\migrate\Plugin\MigrationInterface;
6
7 /**
8  * Allows source data to be defined in the configuration of the source plugin.
9  *
10  * The embedded_data source plugin is used to inject source data from the plugin
11  * configuration. One use case is when some small amount of fixed data is
12  * imported, so that it can be referenced by other migrations. Another use case
13  * is testing.
14  *
15  * Available configuration keys
16  * - data_rows: The source data array.
17  * - ids: The unique ID field of the data.
18  *
19  * Examples:
20  *
21  * @code
22  * source:
23  *   plugin: embedded_data
24  *   data_rows:
25  *     -
26  *       channel_machine_name: music
27  *       channel_description: Music
28  *     -
29  *       channel_machine_name: movies
30  *       channel_description: Movies
31  *   ids:
32  *     channel_machine_name:
33  *       type: string
34  * @endcode
35  *
36  * This example migrates a channel vocabulary.
37  *
38  * @see \Drupal\migrate\Plugin\MigrateSourceInterface
39  *
40  * @MigrateSource(
41  *   id = "embedded_data"
42  * )
43  */
44 class EmbeddedDataSource extends SourcePluginBase {
45
46   /**
47    * Data obtained from the source plugin configuration.
48    *
49    * @var array[]
50    *   Array of data rows, each one an array of values keyed by field names.
51    */
52   protected $dataRows = [];
53
54   /**
55    * Description of the unique ID fields for this source.
56    *
57    * @var array[]
58    *   Each array member is keyed by a field name, with a value that is an
59    *   array with a single member with key 'type' and value a column type such
60    *   as 'integer'.
61    */
62   protected $ids = [];
63
64   /**
65    * {@inheritdoc}
66    */
67   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
68     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
69     $this->dataRows = $configuration['data_rows'];
70     $this->ids = $configuration['ids'];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function fields() {
77     if ($this->count() > 0) {
78       $first_row = reset($this->dataRows);
79       $field_names = array_keys($first_row);
80       return array_combine($field_names, $field_names);
81     }
82     else {
83       return [];
84     }
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function initializeIterator() {
91     return new \ArrayIterator($this->dataRows);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function __toString() {
98     return 'Embedded data';
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function getIds() {
105     return $this->ids;
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function count() {
112     return count($this->dataRows);
113   }
114
115 }