123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace Drupal\reinstall\Plugin\migrate\source;
- use Drupal\Core\Entity\EntityFieldManagerInterface;
- use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Drupal\migrate\MigrateException;
- use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
- use Drupal\migrate\Plugin\MigrationInterface;
- use Drupal\migrate\Row;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Yaml\Exception\ParseException;
- use Symfony\Component\Yaml\Yaml;
- /**
- * Class SimpleSource provides the basic mechanisms to load a YML entity dump.
- */
- abstract class SimpleSource extends SourcePluginBase implements ContainerFactoryPluginInterface {
- use SimpleSourceTrait;
- /**
- * The source records. MUST be initialized in concrete classes __construct().
- *
- * @var array
- */
- protected $records;
- /**
- * {@inheritdoc}
- */
- public static function create(
- ContainerInterface $container,
- array $configuration,
- $pluginId,
- $pluginDefinition,
- MigrationInterface $migration = NULL
- ) {
- $importPath = $container->getParameter('reinstall.path');
- $configuration['importPath'] = $importPath;
- return new static($configuration, $pluginId, $pluginDefinition, $migration);
- }
- /**
- * {@inheritdoc}
- */
- public function doCount() {
- return count($this->records);
- }
- /**
- * Flatten the field hierarchy. Not correct for all cases.
- *
- * @param array $record
- * The raw source values.
- *
- * @return array
- * The flattened values.
- *
- * @see \Drupal\reinstall\Plugin\migrate\process\TermParent
- */
- protected function flattenRecord(array $record) {
- $row = new Row($record);
- $this->flattenRow($row);
- return $row->getSource();
- }
- /**
- * Flatten a typical Drupal 8 field array to a 1-level array.
- */
- protected function flattenRow(Row $row) {
- $source = $row->getSource();
- foreach ($source as $key => &$item_list) {
- if (is_scalar($item_list)) {
- continue;
- }
- if (count($item_list) > 1) {
- $item = $item_list;
- }
- else {
- $item = reset($item_list);
- }
- if (isset($item['target_id'])) {
- $value = $item['target_id'];
- }
- elseif (is_scalar($item) || (count($item) != 1 && !isset($item['width']) && !isset($item['pid']))) {
- $value = $item;
- }
- elseif (isset($item['value'])) {
- $value = $item['value'];
- }
- // Handle bundle['target_id']
- // Exclude image field to keep metadata (alt / title)
- elseif (isset($item['target_id']) && !isset($item['alt']) && !isset($item['title'])) {
- $value = $item['target_id'];
- }
- elseif (isset($item['pid'])) {
- $value = $item['alias'];
- }
- else {
- $value = $item;
- }
- if (empty($item)) {
- $value = NULL;
- }
- $row->setSourceProperty($key, $value);
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function initializeIterator() {
- return new \ArrayIterator($this->records);
- }
- /**
- * Load then parse the file requested in configuration and return its records.
- *
- * @param array $configuration
- * The source configuration from the migration source section.
- * @param string $key
- * Optional. A top-level key for the source document. If empty, items will
- * be parsed from the root of the source document.
- *
- * @return array
- * An array of entity descriptions.
- *
- * @throws \Drupal\migrate\MigrateException
- */
- protected function initialParse(array $configuration, string $key = NULL) {
- $baseFilePath = $configuration['file'] ?? NULL;
- $importPath = $configuration['importPath'] ?? NULL;
- $filePath = realpath("$importPath/$baseFilePath");
- if (!is_file($filePath) || !is_readable($filePath)) {
- throw new MigrateException("${filePath} is not a readable file.");
- }
- try {
- $raw = file_get_contents($filePath);
- $data = Yaml::parse($raw);
- }
- catch (ParseException $e) {
- throw new MigrateException("Cannot parse the contents of ${filePath}.");
- }
- if ($key) {
- return $data[$key] ?? [];
- }
- return $data ?? [];
- }
- /**
- * {@inheritdoc}
- */
- public function __toString() {
- $current = $this->getIterator()->current();
- $ret = json_encode($current, JSON_PRETTY_PRINT);
- return $ret;
- }
- }
|