SimpleSource.php 4.3 KB

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