SimpleSource.php 3.5 KB

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