You are here

class Substr in Drupal 8

Same name and namespace in other branches
  1. 9 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
     source: some_text_field
     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

File

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

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 = isset($this->configuration['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 = isset($this->configuration['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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Substr::transform public function Performs the associated process. Overrides ProcessPluginBase::transform