diff -u b/src/Plugin/migrate/process/CallbackPlus.php b/src/Plugin/migrate/process/CallbackPlus.php --- b/src/Plugin/migrate/process/CallbackPlus.php +++ b/src/Plugin/migrate/process/CallbackPlus.php @@ -2,50 +2,57 @@ namespace Drupal\migrate_plus\Plugin\migrate\process; +use Drupal\migrate\MigrateException; use Drupal\migrate\MigrateExecutableInterface; -use Drupal\migrate\ProcessPluginBase; +use Drupal\migrate\Plugin\migrate\process\Callback; use Drupal\migrate\Row; /** - * Passes the source value and additional parameters to a callback. + * Passes the source value to a callback with additional callback parameters. * - * The callback_plus process plugin allows processing of the value, such - * as ltrim(), which requires multiple parameters. Cores callback process plugin - * don't allow to pass multiple parameters. The callable passes the source array - * as arguments to the callback. + * @MigrateProcessPlugin( + * id = "callback_plus" + * ) + * + * The callback_plus process plugin allows processing using callback functions + * that allow/require additional parameters. + * + * Only functions that take the 'value' to transform as the first parameter + * are currently supported. * - * Available configuration keys: - * - callable: The name of the callable method. + * Example: * - * Examples: + * Say you have an url source with slashes (/) added to the end. With rtrim and + * '/' as a parameter you can remove the slash and make the url suitable for + * Drupal's path aliases. * * @code * process: * destination_field: * plugin: callback_plus - * callable: ltrim - * source: - * - source_field - * - constant/character_mask + * callable: rtrim + * parameters: + * - '/' * @endcode * - * * @see \Drupal\migrate\Plugin\MigrateProcessInterface * - * @MigrateProcessPlugin( - * id = "callback_plus" - * ) */ -class CallbackPlus extends ProcessPluginBase { +class CallbackPlus extends Callback { /** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { - if (is_callable($this->configuration['callable'])) { - $param_arr = is_array($value)? $value : array($value); - $value = call_user_func_array($this->configuration['callable'], $param_arr); + $parameters = isset($this->configuration['parameters']) ? $this->configuration['parameters'] : []; + if(empty($parameters) || !is_array($this->configuration['parameters'])) { + throw new MigrateException('The callback plus plugin requires an array of "parameters".'); + } + if(!isset($this->configuration['callable']) || !is_callable($this->configuration['callable'])){ + throw new MigrateException('The callback plus plugin is missing a valid "callable".'); } + $parameters = array_merge([$value], $parameters); + $value = call_user_func_array($this->configuration['callable'], $parameters); return $value; } only in patch2: unchanged: --- /dev/null +++ b/tests/src/Unit/process/CallbackPlusTest.php @@ -0,0 +1,53 @@ +transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('/foo/bar', $actual); + } + + /** + * Tests that an exception is thrown when no array of parameters is present. + */ + public function testIncorrectParametersException() { + $value = '/foo/bar/'; + $configuration['parameters'] = 'foo'; + $configuration['callable'] = 'rtrim'; + $plugin = new CallbackPlus($configuration, 'callback_plus', []); + $this->setExpectedException('\Drupal\migrate\MigrateException', 'The callback plus plugin requires an array of "parameters".'); + $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests that an exception is thrown when callable is not valid. + */ + public function testNonValidCallbale() { + $value = '/foo/bar/'; + $configuration['parameters'] = ['/']; + $configuration['callback'] = 'footrim'; + $plugin = new CallbackPlus($configuration, 'callback_plus', []); + $this->setExpectedException('\Drupal\migrate\MigrateException', 'The callback plus plugin is missing a valid "callable".'); + $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + +}