SimpleSource.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Drupal\reinstall\Plugin\migrate\source;
  3. use Drupal\Core\Entity\EntityFieldManagerInterface;
  4. use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
  5. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  6. use Drupal\migrate\MigrateException;
  7. use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
  8. use Drupal\migrate\Plugin\MigrationInterface;
  9. use Drupal\migrate\Row;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  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 source records. MUST be initialized in concrete classes __construct().
  20. *
  21. * @var array
  22. */
  23. protected $records;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function create(
  28. ContainerInterface $container,
  29. array $configuration,
  30. $pluginId,
  31. $pluginDefinition,
  32. MigrationInterface $migration = NULL
  33. ) {
  34. $importPath = $container->getParameter('reinstall.path');
  35. $configuration['importPath'] = $importPath;
  36. return new static($configuration, $pluginId, $pluginDefinition, $migration);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function doCount() {
  42. return count($this->records);
  43. }
  44. /**
  45. * Flatten the field hierarchy. Not correct for all cases.
  46. *
  47. * @param array $record
  48. * The raw source values.
  49. *
  50. * @return array
  51. * The flattened values.
  52. *
  53. * @see \Drupal\reinstall\Plugin\migrate\process\TermParent
  54. */
  55. protected function flattenRecord(array $record) {
  56. $row = new Row($record);
  57. $this->flattenRow($row);
  58. return $row->getSource();
  59. }
  60. /**
  61. * Flatten a typical Drupal 8 field array to a 1-level array.
  62. */
  63. protected function flattenRow(Row $row) {
  64. $source = $row->getSource();
  65. foreach ($source as $key => &$item_list) {
  66. if (is_scalar($item_list)) {
  67. continue;
  68. }
  69. if (count($item_list) > 1) {
  70. $item = $item_list;
  71. }
  72. else {
  73. $item = reset($item_list);
  74. }
  75. if (isset($item['target_id'])) {
  76. $value = $item['target_id'];
  77. }
  78. elseif (is_scalar($item) || (count($item) != 1 && !isset($item['width']) && !isset($item['pid']))) {
  79. $value = $item;
  80. }
  81. elseif (isset($item['value'])) {
  82. $value = $item['value'];
  83. }
  84. // Handle bundle['target_id']
  85. // Exclude image field to keep metadata (alt / title)
  86. elseif (isset($item['target_id']) && !isset($item['alt']) && !isset($item['title'])) {
  87. $value = $item['target_id'];
  88. }
  89. elseif (isset($item['pid'])) {
  90. $value = $item['alias'];
  91. }
  92. else {
  93. $value = $item;
  94. }
  95. if (empty($item)) {
  96. $value = NULL;
  97. }
  98. $row->setSourceProperty($key, $value);
  99. }
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. protected function initializeIterator() {
  105. return new \ArrayIterator($this->records);
  106. }
  107. /**
  108. * Load then parse the file requested in configuration and return its records.
  109. *
  110. * @param array $configuration
  111. * The source configuration from the migration source section.
  112. * @param string $key
  113. * Optional. A top-level key for the source document. If empty, items will
  114. * be parsed from the root of the source document.
  115. *
  116. * @return array
  117. * An array of entity descriptions.
  118. *
  119. * @throws \Drupal\migrate\MigrateException
  120. */
  121. protected function initialParse(array $configuration, string $key = NULL) {
  122. $baseFilePath = $configuration['file'] ?? NULL;
  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. }