3 namespace Drupal\migrate\Plugin\migrate\source;
5 use Drupal\migrate\Plugin\MigrationInterface;
8 * Allows source data to be defined in the configuration of the source plugin.
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
15 * Available configuration keys
16 * - data_rows: The source data array.
17 * - ids: The unique ID field of the data.
23 * plugin: embedded_data
26 * channel_machine_name: music
27 * channel_description: Music
29 * channel_machine_name: movies
30 * channel_description: Movies
32 * channel_machine_name:
36 * This example migrates a channel vocabulary.
38 * @see \Drupal\migrate\Plugin\MigrateSourceInterface
41 * id = "embedded_data"
44 class EmbeddedDataSource extends SourcePluginBase {
47 * Data obtained from the source plugin configuration.
50 * Array of data rows, each one an array of values keyed by field names.
52 protected $dataRows = [];
55 * Description of the unique ID fields for this source.
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
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'];
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);
90 public function initializeIterator() {
91 return new \ArrayIterator($this->dataRows);
97 public function __toString() {
98 return 'Embedded data';
104 public function getIds() {
111 public function count() {
112 return count($this->dataRows);