123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace Drupal\reinstall\Plugin\migrate\source;
- use Drupal\Component\Plugin\ConfigurablePluginInterface;
- 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 Drupal\reinstall\ReinstallEvents;
- use Drupal\reinstall\SourceEvent;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Symfony\Component\Yaml\Exception\ParseException;
- use Symfony\Component\Yaml\Yaml;
- /**
- * Class SimpleSource provides the basic mechanisms to load a YML entity dump.
- *
- * @MigrateSource(
- * id = "reinstall_base"
- * )
- */
- class ReinstallSourceBase extends SourcePluginBase implements ContainerFactoryPluginInterface, ConfigurablePluginInterface {
- use SimpleSourceTrait;
- /**
- * The event_dispatcher service.
- *
- * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
- */
- protected $eventDispatcher;
- /**
- * The source records.
- *
- * MAY be altered by subscribing to MigrateEvents::PRE_IMPORT.
- *
- * @var array
- */
- public $records;
- /**
- * ReinstallSourceBase constructor.
- *
- * @param array $configuration
- * The plugin configuration.
- * @param string $plugin_id
- * The plugin id.
- * @param mixed $plugin_definition
- * The plugin definition.
- * @param \Drupal\migrate\Plugin\MigrationInterface $migration
- * The migration on which the plugin is invoked.
- * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
- * The event_dispatcher service.
- */
- public function __construct(
- array $configuration,
- $plugin_id,
- $plugin_definition,
- MigrationInterface $migration,
- EventDispatcherInterface $eventDispatcher
- ) {
- parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
- $this->eventDispatcher = $eventDispatcher;
- $this->records = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
- $eventDispatcher->dispatch(ReinstallEvents::POST_SOURCE_PARSE, new SourceEvent($this));
- }
- /**
- * {@inheritdoc}
- */
- public static function create(
- ContainerInterface $container,
- array $configuration,
- $pluginId,
- $pluginDefinition,
- MigrationInterface $migration = NULL
- ) {
- $importPath = $container->getParameter('reinstall.path');
- $configuration['importPath'] = $importPath;
- $dispatcher = $container->get('event_dispatcher');
- return new static($configuration, $pluginId, $pluginDefinition, $migration, $dispatcher);
- }
- /**
- * {@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);
- }
- // Handle bundle['target_id']
- // Exclude image field to keep metadata (alt / title)
- if (isset($item['target_id']) && !isset($item['alt']) && !isset($item['title'])) {
- $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'];
- }
- elseif (isset($item['pid'])) {
- $value = $item['alias'];
- }
- else {
- $value = $item;
- }
- if (empty($item)) {
- $value = NULL;
- }
- $row->setSourceProperty($key, $value);
- }
- }
- /**
- * {@inheritdoc}
- */
- protected function initializeIterator() {
- if (!isset($this->iterator)) {
- $this->iterator = new \ArrayIterator($this->records);
- }
- return $this->iterator;
- }
- /**
- * 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) {
- $this->sstEntityType = $type = $configuration['type'];
- $bundle = $configuration['bundle'];
- $baseFilePath = $this->configuration['file'] ?? "${type}/${bundle}.yml";
- $importPath = $configuration['importPath'] ?? NULL;
- $filePath = "$importPath/$baseFilePath";
- $realPath = realpath($filePath);
- if (!is_file($realPath) || !is_readable($realPath)) {
- 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;
- }
- /**
- * Gets this plugin's configuration.
- *
- * @return array
- * An array of this plugin's configuration.
- */
- public function getConfiguration() {
- return $this->configuration;
- }
- /**
- * Sets the configuration for this plugin instance.
- *
- * @param array $configuration
- * An associative array containing the plugin's configuration.
- */
- public function setConfiguration(array $configuration) {
- $this->configuration = $configuration;
- }
- /**
- * Gets default configuration for this plugin.
- *
- * @return array
- * An associative array with the default configuration.
- */
- public function defaultConfiguration() {
- return [];
- }
- /**
- * Calculates dependencies for the configured plugin.
- *
- * Dependencies are saved in the plugin's configuration entity and are used to
- * determine configuration synchronization order. For example, if the plugin
- * integrates with specific user roles, this method should return an array of
- * dependencies listing the specified roles.
- *
- * @return array
- * An array of dependencies grouped by type (config, content, module,
- * theme). For example:
- *
- * @code
- * array(
- * 'config' => array('user.role.anonymous', 'user.role.authenticated'),
- * 'content' => array('node:article:f0a189e6-55fb-47fb-8005-5bef81c44d6d'),
- * 'module' => array('node', 'user'),
- * 'theme' => array('seven'),
- * );
- * @endcode
- *
- * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
- * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
- */
- public function calculateDependencies() {
- return [];
- }
- }
|