Commit f9a581b1 authored by claudiu.cristea's avatar claudiu.cristea Committed by claudiu.cristea

Issue #2875377 by claudiu.cristea, drupalmonkey: fields method returns nothing...

Issue #2875377 by claudiu.cristea, drupalmonkey: fields method returns nothing if no columns are defined in yml
parent 7164045c
......@@ -9,6 +9,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate_spreadsheet\SpreadsheetIteratorInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -31,6 +32,20 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
*/
protected $fileSystem;
/**
* The migrate spreadsheet iterator.
*
* @var \Drupal\migrate_spreadsheet\SpreadsheetIteratorInterface
*/
protected $spreadsheetIterator;
/**
* Flag to determine if the iterator has been initialized.
*
* @var bool
*/
protected $iteratorIsInitialized = FALSE;
/**
* Constructs a spreadsheet migration source plugin object.
*
......@@ -44,11 +59,14 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
* The current migration.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
* @param \Drupal\migrate_spreadsheet\SpreadsheetIteratorInterface $spreadsheet_iterator
* The migrate spreadsheet iterator.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, FileSystemInterface $file_system) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, FileSystemInterface $file_system, SpreadsheetIteratorInterface $spreadsheet_iterator) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->setConfiguration($configuration);
$this->fileSystem = $file_system;
$this->spreadsheetIterator = $spreadsheet_iterator;
}
/**
......@@ -60,7 +78,8 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
$plugin_id,
$plugin_definition,
$migration,
$container->get('file_system')
$container->get('file_system'),
$container->get('migrate_spreadsheet.iterator')
);
}
......@@ -125,8 +144,13 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
* {@inheritdoc}
*/
public function fields() {
$columns = $this->getConfiguration()['columns'];
if (!empty($row_index_column = $this->getConfiguration()['row_index_colums'])) {
// No column headers provided in config, read worksheet for header row.
if (!$columns = $this->getConfiguration()['columns']) {
$this->initializeIterator();
$columns = array_keys($this->spreadsheetIterator->getHeaders());
}
// Add $row_index_column if it's been configured.
if ($row_index_column = $this->getConfiguration()['row_index_column']) {
$columns[] = $row_index_column;
}
return array_combine($columns, $columns);
......@@ -136,13 +160,20 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
* {@inheritdoc}
*/
public function initializeIterator() {
$configuration = $this->getConfiguration();
$configuration['worksheet'] = $this->loadWorksheet();
$configuration['keys'] = array_keys($configuration['keys']);
// The 'file' and 'plugin' items are not part of iterator configuration.
unset($configuration['file'], $configuration['plugin']);
return \Drupal::service('migrate_spreadsheet.iterator')
->setConfiguration($configuration);
if (!$this->iteratorIsInitialized) {
$configuration = $this->getConfiguration();
$configuration['worksheet'] = $this->loadWorksheet();
$configuration['keys'] = array_keys($configuration['keys']);
// The 'file' and 'plugin' items are not part of iterator configuration.
unset($configuration['file'], $configuration['plugin']);
$this->spreadsheetIterator->setConfiguration($configuration);
// Flag that the iterator has been initialized.
$this->iteratorIsInitialized = TRUE;
}
return $this->spreadsheetIterator;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment