Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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  *   source_module = "migrate"
43  * )
44  */
45 class EmbeddedDataSource extends SourcePluginBase {
46
47   /**
48    * Data obtained from the source plugin configuration.
49    *
50    * @var array[]
51    *   Array of data rows, each one an array of values keyed by field names.
52    */
53   protected $dataRows = [];
54
55   /**
56    * Description of the unique ID fields for this source.
57    *
58    * @var array[]
59    *   Each array member is keyed by a field name, with a value that is an
60    *   array with a single member with key 'type' and value a column type such
61    *   as 'integer'.
62    */
63   protected $ids = [];
64
65   /**
66    * {@inheritdoc}
67    */
68   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
69     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
70     $this->dataRows = $configuration['data_rows'];
71     $this->ids = $configuration['ids'];
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function fields() {
78     if ($this->count() > 0) {
79       $first_row = reset($this->dataRows);
80       $field_names = array_keys($first_row);
81       return array_combine($field_names, $field_names);
82     }
83     else {
84       return [];
85     }
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function initializeIterator() {
92     return new \ArrayIterator($this->dataRows);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function __toString() {
99     return 'Embedded data';
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public function getIds() {
106     return $this->ids;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function count($refresh = FALSE) {
113     return count($this->dataRows);
114   }
115
116 }