3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Component\Utility\Variable;
7 use Drupal\migrate\ProcessPluginBase;
8 use Drupal\migrate\MigrateException;
9 use Drupal\migrate\MigrateExecutableInterface;
10 use Drupal\migrate\Row;
11 use Drupal\migrate\MigrateSkipRowException;
14 * Changes the source value based on a static lookup map.
16 * Maps the input value to another value using an associative array specified in
19 * Available configuration keys:
20 * - source: The input value - either a scalar or an array.
21 * - map: An array (of 1 or more dimensions) that defines the mapping between
22 * source values and destination values.
23 * - bypass: (optional) Whether the plugin should proceed when the source is not
24 * found in the map array, defaults to FALSE.
25 * - TRUE: Return the unmodified input value, or another default value, if one
27 * - FALSE: Throw a MigrateSkipRowException.
28 * - default_value: (optional) The value to return if the source is not found in
33 * If the value of the source property 'foo' is 'from' then the value of the
34 * destination property bar will be 'to'. Similarly 'this' becomes 'that'.
45 * The static_map process plugin supports a list of source properties. This is
46 * useful in module-delta to machine name conversions. In the example below,
47 * value 'filter_url' is returned if the source property 'module' is 'filter'
48 * and the source property 'delta' is '2'.
58 * 0: filter_html_escape
61 * 3: filter_htmlcorrector
62 * 4: filter_html_escape
67 * When static_map is used to just rename a few values and leave the others
68 * unchanged, a 'bypass: true' option can be used. See the example below. If the
69 * value of the source property 'foo' is 'from', 'to' will be returned. If the
70 * value of the source property 'foo' is 'another' (a value that is not in the
71 * map), 'another' will be returned unchanged.
83 * A default value can be defined for all values that are not included in the
84 * map. See the example below. If the value of the source property 'foo' is
85 * 'yet_another' (a value that is not in the map), 'bar' will be returned.
97 * If your source data has boolean values as strings, you need to use single
98 * quotes in the map. See the example below.
108 * Mapping from a string which contains a period is not supported. A custom
109 * process plugin can be written to handle this kind of a transformation.
110 * Another option which may be feasible in certain use cases is to first pass
111 * the value through the machine_name process plugin.
113 * @see https://www.drupal.org/project/drupal/issues/2827897
114 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
116 * @MigrateProcessPlugin(
120 class StaticMap extends ProcessPluginBase {
125 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
127 if (is_array($value)) {
129 throw new MigrateException('Can not lookup without a value.');
133 $new_value = [$value];
135 $new_value = NestedArray::getValue($this->configuration['map'], $new_value, $key_exists);
137 if (array_key_exists('default_value', $this->configuration)) {
138 if (!empty($this->configuration['bypass'])) {
139 throw new MigrateException('Setting both default_value and bypass is invalid.');
141 return $this->configuration['default_value'];
143 if (empty($this->configuration['bypass'])) {
144 throw new MigrateSkipRowException(sprintf("No static mapping found for '%s' and no default value provided for destination '%s'.", Variable::export($value), $destination_property));