class StaticMap

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

Changes the source value based on a static lookup map.

Maps the input value to another value using an associative array specified in the configuration.

Available configuration keys:

  • source: The input value - either a scalar or an array.
  • map: An array (of 1 or more dimensions) that defines the mapping between source values and destination values.
  • bypass: (optional) Whether the plugin should proceed when the source is not found in the map array, defaults to FALSE.

    • TRUE: Return the unmodified input value, or another default value, if one is specified.
    • FALSE: Throw a MigrateSkipRowException.
  • default_value: (optional) The value to return if the source is not found in the map array.

While this plugin supports map key values which contain a dot (.), Drupal configuration export does not export keys which contain a dot. Be careful when using this feature with migrations that are stored as configuration entities. These entities cannot contain keys with a dot. In this case, additional manipulation with either custom or contrib process plugins is needed.

Examples:

If the value of the source property 'foo' is 'from' then the value of the destination property bar will be 'to'. Similarly 'this' becomes 'that'.


process:
  bar:
    plugin: static_map
    source: foo
    map:
      from: to
      this: that

The static_map process plugin supports a list of source properties. This is useful in module-delta to machine name conversions. In the example below, value 'filter_url' is returned if the source property 'module' is 'filter' and the source property 'delta' is '2'.


process:
  id:
    plugin: static_map
    source:
      - module
      - delta
    map:
      filter:
        0: filter_html_escape
        1: filter_autop
        2: filter_url
        3: filter_htmlcorrector
        4: filter_html_escape
      php:
        0: php_code

When static_map is used to just rename a few values and leave the others unchanged, a 'bypass: true' option can be used. See the example below. If the value of the source property 'foo' is 'from', 'to' will be returned. If the value of the source property 'foo' is 'another' (a value that is not in the map), 'another' will be returned unchanged.


process:
  bar:
    plugin: static_map
    source: foo
    map:
      from: to
      this: that
    bypass: TRUE

A default value can be defined for all values that are not included in the map. See the example below. If the value of the source property 'foo' is 'yet_another' (a value that is not in the map), 'bar' will be returned.


process:
  bar:
    plugin: static_map
    source: foo
    map:
      from: to
      this: that
    default_value: bar

If your source data has boolean values as strings, you need to use single quotes in the map. See the example below.


process:
  bar:
    plugin: static_map
    source: foo
    map:
      'TRUE': to

A NULL can be mapped. If the value of the source property 'foo' is NULL then the value of the destination property bar will be 'to'.


process:
  bar:
    plugin: static_map
    source: foo
    map:
      NULL: to

If your source data contains booleans, the boolean is treated as a numeric 0 or 1. If the value of the source property 'foo' is TRUE then the value of the destination property bar will be 'bar'. And if the value of the source property 'foo' is FALSE then the value of the destination property bar will be 'bar'.


process:
  bar:
    plugin: static_map
    source: foo
    map:
      0: foo
      1: bar

Attributes

#[MigrateProcess('static_map')]

Hierarchy

Expanded class hierarchy of StaticMap

See also

https://www.drupal.org/project/drupal/issues/2827897

\Drupal\migrate\Plugin\MigrateProcessInterface

4 files declare their use of StaticMap
BlockRegion.php in core/modules/block/src/Plugin/migrate/process/BlockRegion.php
FieldType.php in core/modules/field/src/Plugin/migrate/process/FieldType.php
FilterID.php in core/modules/filter/src/Plugin/migrate/process/FilterID.php
StaticMapTest.php in core/modules/migrate/tests/src/Unit/process/StaticMapTest.php

File

core/modules/migrate/src/Plugin/migrate/process/StaticMap.php, line 147

Namespace

Drupal\migrate\Plugin\migrate\process
View source
class StaticMap extends ProcessPluginBase {
  
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $new_value = $value;
    if (is_array($value)) {
      if (!$value) {
        throw new MigrateException('Can not lookup without a value.');
      }
    }
    else {
      $new_value = [
        $value,
      ];
    }
    $new_value = NestedArray::getValue($this->configuration['map'], $new_value, $key_exists);
    if (!$key_exists) {
      if (array_key_exists('default_value', $this->configuration)) {
        if (!empty($this->configuration['bypass'])) {
          throw new MigrateException('Setting both default_value and bypass is invalid.');
        }
        return $this->configuration['default_value'];
      }
      if (empty($this->configuration['bypass'])) {
        throw new MigrateSkipRowException(sprintf("No static mapping found for '%s' and no default value provided for destination '%s'.", Variable::export($value), $destination_property));
      }
      else {
        return $value;
      }
    }
    return $new_value;
  }

}

Members

Title Sort descending 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 1
DependencySerializationTrait::__wakeup public function #[\ReturnTypeWillChange] 2
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
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 Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 6
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::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
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.
StaticMap::transform public function Performs the associated process. Overrides ProcessPluginBase::transform 3
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.

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