SimpleSource.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Drupal\reinstall\Plugin\migrate\source;
  3. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  4. use Drupal\migrate\MigrateException;
  5. use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
  6. use Drupal\migrate\Plugin\MigrationInterface;
  7. use Drupal\migrate\Row;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Yaml\Exception\ParseException;
  12. use Symfony\Component\Yaml\Yaml;
  13. /**
  14. * Class SimpleSource provides the basic mechanisms to load a YML entity dump.
  15. */
  16. abstract class SimpleSource extends SourcePluginBase implements ContainerFactoryPluginInterface {
  17. use SimpleSourceTrait;
  18. /**
  19. * The event_dispatcher service.
  20. *
  21. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  22. */
  23. protected $eventDispatcher;
  24. /**
  25. * The source records.
  26. *
  27. * MAY be altered by subscribing to MigrateEvents::PRE_IMPORT.
  28. *
  29. * @var array
  30. */
  31. public $records;
  32. public function __construct(
  33. array $configuration,
  34. $plugin_id,
  35. $plugin_definition,
  36. \Drupal\migrate\Plugin\MigrationInterface $migration,
  37. EventDispatcherInterface $eventDispatcher
  38. ) {
  39. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
  40. $this->eventDispatcher = $eventDispatcher;
  41. $this->records = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public static function create(
  47. ContainerInterface $container,
  48. array $configuration,
  49. $pluginId,
  50. $pluginDefinition,
  51. MigrationInterface $migration = NULL
  52. ) {
  53. $importPath = $container->getParameter('reinstall.path');
  54. $configuration['importPath'] = $importPath;
  55. $dispatcher = $container->get('event_dispatcher');
  56. return new static($configuration, $pluginId, $pluginDefinition, $migration, $dispatcher);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function doCount() {
  62. return count($this->records);
  63. }
  64. /**
  65. * Flatten the field hierarchy. Not correct for all cases.
  66. *
  67. * @param array $record
  68. * The raw source values.
  69. *
  70. * @return array
  71. * The flattened values.
  72. *
  73. * @see \Drupal\reinstall\Plugin\migrate\process\TermParent
  74. */
  75. protected function flattenRecord(array $record) {
  76. $row = new Row($record);
  77. $this->flattenRow($row);
  78. return $row->getSource();
  79. }
  80. /**
  81. * Flatten a typical Drupal 8 field array to a 1-level array.
  82. */
  83. protected function flattenRow(Row $row) {
  84. $source = $row->getSource();
  85. foreach ($source as $key => &$item_list) {
  86. if (is_scalar($item_list)) {
  87. continue;
  88. }
  89. if (count($item_list) > 1) {
  90. $item = $item_list;
  91. }
  92. else {
  93. $item = reset($item_list);
  94. }
  95. if (isset($item['target_id'])) {
  96. $value = $item['target_id'];
  97. }
  98. elseif (is_scalar($item) || (count($item) != 1 && !isset($item['width']) && !isset($item['pid']))) {
  99. $value = $item;
  100. }
  101. elseif (isset($item['value'])) {
  102. $value = $item['value'];
  103. }
  104. // Handle bundle['target_id']
  105. // Exclude image field to keep metadata (alt / title)
  106. elseif (isset($item['target_id']) && !isset($item['alt']) && !isset($item['title'])) {
  107. $value = $item['target_id'];
  108. }
  109. elseif (isset($item['pid'])) {
  110. $value = $item['alias'];
  111. }
  112. else {
  113. $value = $item;
  114. }
  115. if (empty($item)) {
  116. $value = NULL;
  117. }
  118. $row->setSourceProperty($key, $value);
  119. }
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. protected function initializeIterator() {
  125. if (!isset($this->iterator)) {
  126. $this->iterator = new \ArrayIterator($this->records);
  127. }
  128. return $this->iterator;
  129. }
  130. /**
  131. * Load then parse the file requested in configuration and return its records.
  132. *
  133. * @param array $configuration
  134. * The source configuration from the migration source section.
  135. * @param string $key
  136. * Optional. A top-level key for the source document. If empty, items will
  137. * be parsed from the root of the source document.
  138. *
  139. * @return array
  140. * An array of entity descriptions.
  141. *
  142. * @throws \Drupal\migrate\MigrateException
  143. */
  144. protected function initialParse(array $configuration, string $key = NULL) {
  145. $this->sstEntityType = $type = $configuration['type'];
  146. $bundle = $configuration['bundle'];
  147. $baseFilePath = "${type}/${bundle}.yml";
  148. $importPath = $configuration['importPath'] ?? NULL;
  149. $filePath = realpath("$importPath/$baseFilePath");
  150. if (!is_file($filePath) || !is_readable($filePath)) {
  151. throw new MigrateException("${filePath} is not a readable file.");
  152. }
  153. try {
  154. $raw = file_get_contents($filePath);
  155. $data = Yaml::parse($raw);
  156. }
  157. catch (ParseException $e) {
  158. throw new MigrateException("Cannot parse the contents of ${filePath}.");
  159. }
  160. if ($key) {
  161. return $data[$key] ?? [];
  162. }
  163. return $data ?? [];
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function __toString() {
  169. $current = $this->getIterator()->current();
  170. $ret = json_encode($current, JSON_PRETTY_PRINT);
  171. return $ret;
  172. }
  173. }