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 the PHP 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 NULL.
21 * - length: (optional) The maximum number of characters in the returned
22 * string. Defaults to NULL.
24 * If start is NULL 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 both start and length are NULL the entire string is returned.
35 * source: some_text_field
40 * If some_text_field was 'Marie Skłodowska Curie' then
41 * $destination['new_text_field'] would be 'Skłodowska'.
43 * The PHP equivalent of this is:
46 * $destination['new_text_field'] = substr($source['some_text_field'], 6, 10);
49 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
51 * @MigrateProcessPlugin(
55 class Substr extends ProcessPluginBase {
60 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
61 $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
62 if (!is_int($start)) {
63 throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
65 $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
66 if (!is_null($length) && !is_int($length)) {
67 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.');
69 if (!is_string($value)) {
70 throw new MigrateException('The input value must be a string.');
73 // Use optional start or length to return a portion of $value.
74 $new_value = Unicode::substr($value, $start, $length);