class Substr

Same name in other branches
  1. 8.9.x core/modules/migrate/src/Plugin/migrate/process/Substr.php \Drupal\migrate\Plugin\migrate\process\Substr
  2. 10 core/modules/migrate/src/Plugin/migrate/process/Substr.php \Drupal\migrate\Plugin\migrate\process\Substr
  3. 11.x core/modules/migrate/src/Plugin/migrate/process/Substr.php \Drupal\migrate\Plugin\migrate\process\Substr

Returns a substring of the input value.

The substr process plugin returns the portion of the input value specified by the start and length parameters. This is a wrapper around mb_substr().

Available configuration keys:

  • start: (optional) The returned string will start this many characters after the beginning of the string, defaults to 0.
  • length: (optional) The maximum number of characters in the returned string, defaults to NULL.

If start is 0 and length is an integer, the start position is the beginning of the string. If start is an integer and length is NULL, the substring starting from the start position until the end of the string will be returned. If start is 0 and length is NULL the entire string is returned.

Example:


process:
  new_text_field:
    plugin: substr
    source: some_text_field
    start: 6
    length: 10

If some_text_field was 'Marie Skłodowska Curie' then $destination['new_text_field'] would be 'Skłodowska'.

The PHP equivalent of this is:

$destination['new_text_field'] = substr($source['some_text_field'], 6, 10);

The substr plugin requires that the source value is not empty. If empty values are expected, combine skip_on_empty process plugin to the pipeline:


process:
  new_text_field:
   -
     plugin: skip_on_empty
     method: process
     source: some_text_field
   -
     plugin: substr
     start: 6
     length: 10

Plugin annotation


@MigrateProcessPlugin(
  id = "substr"
)

Hierarchy

Expanded class hierarchy of Substr

See also

\Drupal\migrate\Plugin\MigrateProcessInterface

1 file declares its use of Substr
SubstrTest.php in core/modules/migrate/tests/src/Unit/process/SubstrTest.php
5 string references to 'Substr'
d6_taxonomy_vocabulary_translation.yml in core/modules/config_translation/migrations/d6_taxonomy_vocabulary_translation.yml
core/modules/config_translation/migrations/d6_taxonomy_vocabulary_translation.yml
d6_vocabulary_entity_display.yml in core/modules/taxonomy/migrations/d6_vocabulary_entity_display.yml
core/modules/taxonomy/migrations/d6_vocabulary_entity_display.yml
d6_vocabulary_entity_form_display.yml in core/modules/taxonomy/migrations/d6_vocabulary_entity_form_display.yml
core/modules/taxonomy/migrations/d6_vocabulary_entity_form_display.yml
d6_vocabulary_field.yml in core/modules/taxonomy/migrations/d6_vocabulary_field.yml
core/modules/taxonomy/migrations/d6_vocabulary_field.yml
d6_vocabulary_field_instance.yml in core/modules/taxonomy/migrations/d6_vocabulary_field_instance.yml
core/modules/taxonomy/migrations/d6_vocabulary_field_instance.yml

File

core/modules/migrate/src/Plugin/migrate/process/Substr.php, line 68

Namespace

Drupal\migrate\Plugin\migrate\process
View source
class Substr extends ProcessPluginBase {
    
    /**
     * {@inheritdoc}
     */
    public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
        $start = $this->configuration['start'] ?? 0;
        if (!is_int($start)) {
            throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
        }
        $length = $this->configuration['length'] ?? NULL;
        if ($length !== NULL && !is_int($length)) {
            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.');
        }
        if (!is_string($value)) {
            throw new MigrateException('The input value must be a string.');
        }
        // Use optional start or length to return a portion of $value.
        return mb_substr($value, $start, $length);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
Substr::transform public function Performs the associated process. Overrides ProcessPluginBase::transform

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.