ReinstallSourceBase.php 5.2 KB

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