Commit fc2cc00b authored by git's avatar git Committed by claudiu.cristea

Issue #2932332 by adinac, claudiu.cristea: Use an absolute filepath for spreadsheet files

parent 8ce8b951
......@@ -4,10 +4,13 @@ namespace Drupal\migrate_spreadsheet\Plugin\migrate\source;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a source plugin that migrate from spreadsheet files.
......@@ -19,7 +22,14 @@ use PhpOffice\PhpSpreadsheet\IOFactory;
* id = "spreadsheet"
* )
*/
class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterface/*, ContainerFactoryPluginInterface*/ {
class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterface, ContainerFactoryPluginInterface {
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs a spreadsheet migration source plugin object.
......@@ -32,10 +42,26 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
* The plugin implementation definition.
* @param \Drupal\migrate\Plugin\MigrationInterface
* The current migration.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->setConfiguration($configuration);
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('file_system')
);
}
/**
......@@ -143,8 +169,10 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
// Load the workbook.
try {
$file_path = $this->fileSystem->realpath($config['file']);
// Identify the type of the input file.
$type = IOFactory::identify($config['file']);
$type = IOFactory::identify($file_path);
// Create a new Reader of the file type.
/** @var \PhpOffice\PhpSpreadsheet\Reader\BaseReader $reader */
......@@ -157,7 +185,7 @@ class Spreadsheet extends SourcePluginBase implements ConfigurablePluginInterfac
$reader->setLoadSheetsOnly($config['worksheet']);
/** @var \PhpOffice\PhpSpreadsheet\Spreadsheet $workbook */
$workbook = $reader->load($config['file']);
$workbook = $reader->load($file_path);
return $workbook->getSheet(0);
}
......
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