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