Callback.php 1.38 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\migrate\Plugin\migrate\process;

5
use Drupal\migrate\MigrateExecutableInterface;
6
7
8
9
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
10
 * Passes the source value to a callback.
11
 *
12
13
14
 * The callback process plugin allows simple processing of the value, such as
 * strtolower(). The callable takes the source value as the single mandatory
 * argument. No additional arguments can be passed to the callback.
15
 *
16
 * Available configuration keys:
17
 * - callable: The name of the callable method.
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 *
 * Examples:
 *
 * @code
 * process:
 *   destination_field:
 *     plugin: callback
 *     callable: strtolower
 *     source: source_field
 * @endcode
 *
 * An example where the callable is a static method in a class:
 *
 * @code
 * process:
 *   destination_field:
 *     plugin: callback
 *     callable:
 *       - '\Drupal\Component\Utility\Unicode'
 *       - strtolower
 *     source: source_field
 * @endcode
 *
 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
42
 *
43
44
45
46
47
48
49
50
51
 * @MigrateProcessPlugin(
 *   id = "callback"
 * )
 */
class Callback extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
52
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
53
54
55
56
57
58
59
    if (is_callable($this->configuration['callable'])) {
      $value = call_user_func($this->configuration['callable'], $value);
    }
    return $value;
  }

}