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;
9 use Drupal\Component\Utility\Unicode;
12 * Returns a substring of the input value.
14 * The substr process plugin returns the portion of the input value specified by
15 * the start and length parameters. This is a wrapper around
16 * \Drupal\Component\Utility\Unicode::substr().
18 * Available configuration keys:
19 * - start: (optional) The returned string will start this many characters after
20 * the beginning of the string, defaults to 0.
21 * - length: (optional) The maximum number of characters in the returned
22 * string, defaults to NULL.
24 * If start is 0 and length is an integer, the start position is the
25 * beginning of the string. If start is an integer and length is NULL, the
26 * substring starting from the start position until the end of the string will
27 * be returned. If start is 0 and length is NULL the entire string is returned.
35 * source: some_text_field
39 * If some_text_field was 'Marie Skłodowska Curie' then
40 * $destination['new_text_field'] would be 'Skłodowska'.
42 * The PHP equivalent of this is:
44 * $destination['new_text_field'] = substr($source['some_text_field'], 6, 10);
47 * The substr plugin requires that the source value is not empty. If empty
48 * values are expected, combine skip_on_empty process plugin to the pipeline:
53 * plugin: skip_on_empty
55 * source: some_text_field
58 * source: some_text_field
63 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
65 * @MigrateProcessPlugin(
69 class Substr extends ProcessPluginBase {
74 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
75 $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
76 if (!is_int($start)) {
77 throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
79 $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
80 if (!is_null($length) && !is_int($length)) {
81 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.');
83 if (!is_string($value)) {
84 throw new MigrateException('The input value must be a string.');
87 // Use optional start or length to return a portion of $value.
88 $new_value = Unicode::substr($value, $start, $length);