3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\Row;
8 use Drupal\migrate\MigrateException;
11 * Returns a substring of the input value.
13 * The substr process plugin returns the portion of the input value specified by
14 * the start and length parameters. This is a wrapper around mb_substr().
16 * Available configuration keys:
17 * - start: (optional) The returned string will start this many characters after
18 * the beginning of the string, defaults to 0.
19 * - length: (optional) The maximum number of characters in the returned
20 * string, defaults to NULL.
22 * If start is 0 and length is an integer, the start position is the
23 * beginning of the string. If start is an integer and length is NULL, the
24 * substring starting from the start position until the end of the string will
25 * be returned. If start is 0 and length is NULL the entire string is returned.
33 * source: some_text_field
37 * If some_text_field was 'Marie Skłodowska Curie' then
38 * $destination['new_text_field'] would be 'Skłodowska'.
40 * The PHP equivalent of this is:
42 * $destination['new_text_field'] = substr($source['some_text_field'], 6, 10);
45 * The substr plugin requires that the source value is not empty. If empty
46 * values are expected, combine skip_on_empty process plugin to the pipeline:
51 * plugin: skip_on_empty
53 * source: some_text_field
56 * source: some_text_field
61 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
63 * @MigrateProcessPlugin(
67 class Substr extends ProcessPluginBase {
72 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
73 $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
74 if (!is_int($start)) {
75 throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
77 $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
78 if (!is_null($length) && !is_int($length)) {
79 throw new MigrateException('The character length configuration value should be an integer. Omit this key to capture from the start position to the end of the string.');
81 if (!is_string($value)) {
82 throw new MigrateException('The input value must be a string.');
85 // Use optional start or length to return a portion of $value.
86 $new_value = mb_substr($value, $start, $length);