SimpleSource.php 3.7 KB

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