Skip to content
Snippets Groups Projects

Allow a callback function for the Source path parameter.

2 unresolved threads
1 file
+ 43
5
Compare changes
  • Side-by-side
  • Inline
@@ -3,6 +3,7 @@
namespace Drupal\migrate_source_csv\Plugin\migrate\source;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Utility\NestedArray;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
@@ -185,14 +186,17 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
*
* @return string
* The file path.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
public function __toString() {
return $this->configuration['path'];
return $this->getPath();
}
/**
* {@inheritdoc}
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\migrate\MigrateException
* @throws \League\Csv\Exception
*/
@@ -263,6 +267,7 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
* @return \League\Csv\Reader
* The reader.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\migrate\MigrateException
* @throws \League\Csv\Exception
*/
@@ -280,16 +285,49 @@ class CSV extends SourcePluginBase implements ConfigurableInterface {
*
* @return \League\Csv\Reader
* The reader.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
protected function createReader() {
if (!file_exists($this->configuration['path'])) {
throw new \RuntimeException(sprintf('File "%s" was not found.', $this->configuration['path']));
$path = $this->getPath();
if (!file_exists($path)) {
throw new \RuntimeException(sprintf('File "%s" was not found.', $path));
}
$csv = fopen($this->configuration['path'], 'r');
$csv = fopen($path, 'r');
if (!$csv) {
throw new \RuntimeException(sprintf('File "%s" could not be opened.', $this->configuration['path']));
throw new \RuntimeException(sprintf('File "%s" could not be opened.', $path));
}
return Reader::createFromStream($csv);
}
/**
* Gets the path from configuration.
*
* @return string
* The path.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
protected function getPath() {
$path = $this->configuration['path'];
// Support a callback to return the path.
if (is_array($path)) {
Please register or sign in to reply
if (!isset($path['callback'])) {
Please register or sign in to reply
throw new InvalidPluginDefinitionException($this->getPluginId(), 'The path is an array, but the callback function is not defined.');
}
if (!is_callable($path['callback'])) {
throw new InvalidPluginDefinitionException($this->getPluginId(), 'The path callback function is not callable.');
}
$path = call_user_func($path['callback']);
if (!is_string($path)) {
throw new InvalidPluginDefinitionException($this->getPluginId(), 'The path callback function did not return a string.');
}
}
return $path;
}
}
Loading