class Explode

Same name and namespace in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/Explode.php \Drupal\migrate\Plugin\migrate\process\Explode
  2. 8.9.x core/modules/migrate/src/Plugin/migrate/process/Explode.php \Drupal\migrate\Plugin\migrate\process\Explode
  3. 10 core/modules/migrate/src/Plugin/migrate/process/Explode.php \Drupal\migrate\Plugin\migrate\process\Explode

Splits the source string into an array of strings, using a delimiter.

This plugin creates an array of strings by splitting the source parameter on boundaries formed by the delimiter.

Available configuration keys:

  • source: The source string.
  • limit: (optional)
    • If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
    • If limit is set and negative, all components except the last -limit are returned.
    • If the limit parameter is zero, then this is treated as 1.
  • delimiter: The boundary string.
  • strict: (optional) When this boolean is TRUE, the source should be strictly a string. If FALSE is passed, the source value is casted to a string before being split. Also, in this case, the values casting to empty strings are converted to empty arrays, instead of an array with a single empty string item ['']. Defaults to TRUE.

Example:


process:
  bar:
    plugin: explode
    source: foo
    delimiter: /

If foo is "node/1", then bar will be ['node', '1']. The PHP equivalent of this would be:

$bar = explode('/', $foo);

process:
  bar:
    plugin: explode
    source: foo
    limit: 2
    delimiter: /

If foo is "node/1/edit", then bar will be ['node', '1/edit']. The PHP equivalent of this would be:

$bar = explode('/', $foo, 2);

If the 'strict' configuration is set to FALSE, the input value is casted to a string before being spilt:


process:
  bar:
    plugin: explode
    source: foo
    delimiter: /
    strict: false

If foo is 123 (as integer), then bar will be ['123']. If foo is TRUE, then bar will be ['1']. The PHP equivalent of this would be:

$bar = explode('/', (string) 123);
$bar = explode('/', (string) TRUE);

If the 'strict' configuration is set to FALSE, the source value casting to an empty string are converted to an empty array. For example, with the last configuration, if foo is '', NULL or FALSE, then bar will be [].

Attributes

#[MigrateProcess('explode')]

Hierarchy

Expanded class hierarchy of Explode

See also

\Drupal\migrate\Plugin\MigrateProcessInterface

1 file declares its use of Explode
ExplodeTest.php in core/modules/migrate/tests/src/Unit/process/ExplodeTest.php
8 string references to 'Explode'
d6_field_instance_label_description_translation.yml in core/modules/config_translation/migrations/d6_field_instance_label_description_translation.yml
core/modules/config_translation/migrations/d6_field_instance_label_description_translation.yml
d6_url_alias.yml in core/modules/path/migrations/d6_url_alias.yml
core/modules/path/migrations/d6_url_alias.yml
d7_field_instance.yml in core/modules/field/migrations/d7_field_instance.yml
core/modules/field/migrations/d7_field_instance.yml
d7_theme_settings.yml in core/modules/system/migrations/d7_theme_settings.yml
core/modules/system/migrations/d7_theme_settings.yml
d7_url_alias.yml in core/modules/path/migrations/d7_url_alias.yml
core/modules/path/migrations/d7_url_alias.yml

... See full list

File

core/modules/migrate/src/Plugin/migrate/process/Explode.php, line 91

Namespace

Drupal\migrate\Plugin\migrate\process
View source
class Explode extends ProcessPluginBase {
  
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (empty($this->configuration['delimiter'])) {
      throw new MigrateException('delimiter is empty');
    }
    $strict = array_key_exists('strict', $this->configuration) ? $this->configuration['strict'] : TRUE;
    if ($strict && !is_string($value)) {
      throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
    }
    elseif (!$strict) {
      // Check if the incoming value can cast to a string.
      $original = $value;
      if (!is_string($original) && $original != ($value = @strval($value))) {
        throw new MigrateException(sprintf('%s cannot be casted to a string', var_export($original, TRUE)));
      }
      // Empty strings should be exploded to empty arrays.
      if ($value === '') {
        return [];
      }
    }
    $limit = $this->configuration['limit'] ?? PHP_INT_MAX;
    return explode($this->configuration['delimiter'], $value, $limit);
  }
  
  /**
   * {@inheritdoc}
   */
  public function multiple() {
    return TRUE;
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title 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 3
DependencySerializationTrait::__wakeup public function 3
Explode::multiple public function Indicates whether the returned value requires multiple handling. Overrides ProcessPluginBase::multiple
Explode::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
MessengerTrait::$messenger protected property The messenger. 25
MessengerTrait::messenger public function Gets the messenger. 25
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 2
PluginBase::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable Deprecated public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 82
ProcessPluginBase::$stopPipeline protected property Determines if processing of the pipeline is stopped.
ProcessPluginBase::isPipelineStopped public function Determines if the pipeline should stop processing. Overrides MigrateProcessInterface::isPipelineStopped
ProcessPluginBase::reset public function Resets the internal data of a plugin. Overrides MigrateProcessInterface::reset
ProcessPluginBase::stopPipeline protected function Stops pipeline processing after this plugin finishes.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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. 1

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