class Substr in Drupal 8
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\migrate\Plugin\migrate\process\Substr
- class \Drupal\migrate\ProcessPluginBase implements MigrateProcessInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\processView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
ProcessPluginBase:: |
public | function |
Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface:: |
3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
Substr:: |
public | function |
Performs the associated process. Overrides ProcessPluginBase:: |