123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?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 {
- /**
- * The source records.
- *
- * @var array
- */
- protected $records;
- /**
- * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
- */
- protected $entityTypeBundleInfo;
- /**
- * @var \Drupal\Core\Entity\EntityFieldManagerInterface
- */
- protected $entityFieldManager;
- /**
- * Constructor.
- */
- public function __construct(
- array $configuration,
- string $pluginId,
- array $pluginDefinition,
- MigrationInterface $migration,
- EntityTypeBundleInfoInterface $ebi,
- EntityFieldManagerInterface $efm
- ) {
- parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
- $this->entityTypeBundleInfo = $ebi;
- $this->entityFieldManager = $efm;
- }
- /**
- * {@inheritdoc}
- */
- public static function create(
- ContainerInterface $container,
- array $configuration,
- $pluginId,
- $pluginDefinition,
- MigrationInterface $migration = NULL
- ) {
- $importPath = $container->getParameter('reinstall.path');
- $configuration['importPath'] = $importPath;
- $ebi = $container->get('entity_type.bundle.info');
- $efm = $container->get('entity_field.manager');
- return new static($configuration, $pluginId, $pluginDefinition, $migration, $ebi, $efm);
- }
- /**
- * {@inheritdoc}
- */
- public function doCount() {
- return count($this->records);
- }
- /**
- * {@inheritdoc}
- */
- public function fields() {
- $bundles = $this->entityTypeBundleInfo->getBundleInfo(static::ENTITY_TYPE);
- $rows = [];
- foreach ($bundles as $bundleName => $bundle) {
- $fieldDefinitions = $this->entityFieldManager->getFieldDefinitions(static::ENTITY_TYPE,
- $bundleName);
- foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
- $rows[$fieldName][$bundleName] = $fieldDefinition->getLabel();
- }
- }
- $fields = [];
- $singleBundle = count($bundles) === 1;
- foreach ($rows as $fieldName => $labels) {
- if ($singleBundle) {
- $fields[$fieldName] = reset($labels);
- continue;
- }
- if (count(array_unique($labels)) === 1) {
- $fields[$fieldName] = reset($labels);
- continue;
- }
- $ret = [];
- ksort($labels);
- foreach ($labels as $ct => $label) {
- $ret[] = $this->t('@ct: @label', ['@ct' => $ct, '@label' => $label]);
- }
- $fields[$fieldName] = implode(', ', $ret);
- }
- ksort($fields);
- return $fields;
- }
- /**
- * 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;
- }
- }
|