ProcessPluginBase.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Drupal\migrate;
  3. use Drupal\Core\Plugin\PluginBase;
  4. use Drupal\migrate\Plugin\MigrateProcessInterface;
  5. /**
  6. * The base class for all migrate process plugins.
  7. *
  8. * Migrate process plugins are taking a value and transform them. For example,
  9. * transform a human provided name into a machine name, look up an identifier
  10. * in a previous migration and so on.
  11. *
  12. * Process plugins extending this class can use any number of methods, thus
  13. * offering different alternative ways of processing. In this case, the
  14. * transform() method should not be implemented, and the plugin configuration
  15. * must provide the name of the method to be called via the "method" key. Each
  16. * method must have the same signature as transform().
  17. *
  18. * @see https://www.drupal.org/node/2129651
  19. * @see \Drupal\migrate\Plugin\MigratePluginManager
  20. * @see \Drupal\migrate\Plugin\MigrateProcessInterface
  21. * @see \Drupal\migrate\Annotation\MigrateProcessPlugin
  22. * @see \Drupal\migrate\Plugin\migrate\process\SkipOnEmpty
  23. * @see d7_field_formatter_settings.yml
  24. * @see plugin_api
  25. *
  26. * @ingroup migration
  27. */
  28. abstract class ProcessPluginBase extends PluginBase implements MigrateProcessInterface {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  33. // Do not call this method from children.
  34. if (isset($this->configuration['method'])) {
  35. if (method_exists($this, $this->configuration['method'])) {
  36. return $this->{$this->configuration['method']}($value, $migrate_executable, $row, $destination_property);
  37. }
  38. throw new \BadMethodCallException(sprintf('The %s method does not exist in the %s plugin.', $this->configuration['method'], $this->pluginId));
  39. }
  40. else {
  41. throw new \BadMethodCallException(sprintf('The "method" key in the plugin configuration must to be set for the %s plugin.', $this->pluginId));
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function multiple() {
  48. return FALSE;
  49. }
  50. }