class DefaultValue

Same name in other branches
  1. 8.9.x core/modules/migrate/src/Plugin/migrate/process/DefaultValue.php \Drupal\migrate\Plugin\migrate\process\DefaultValue
  2. 10 core/modules/migrate/src/Plugin/migrate/process/DefaultValue.php \Drupal\migrate\Plugin\migrate\process\DefaultValue
  3. 11.x core/modules/migrate/src/Plugin/migrate/process/DefaultValue.php \Drupal\migrate\Plugin\migrate\process\DefaultValue

Returns a given default value if the input is empty.

The default_value process plugin provides the ability to set a fixed default value. The plugin returns a default value if the input value is considered empty (NULL, FALSE, 0, '0', an empty string, or an empty array). The strict configuration key can be used to set the default only when the incoming value is NULL.

Available configuration keys:

  • default_value: The fixed default value to apply.
  • strict: (optional) Use strict value checking. Defaults to false.
    • FALSE: Apply default when input value is empty().
    • TRUE: Apply default when input value is NULL.

Example:


process:
  uid:
    -
      plugin: migration_lookup
      migration: users
      source: author
      no_stub: true
    -
      plugin: default_value
      default_value: 44

This will look up the source value of author in the users migration and if not found, set the destination property uid to 44.

Plugin annotation


@MigrateProcessPlugin(
  id = "default_value",
  handle_multiples = TRUE
)

Hierarchy

Expanded class hierarchy of DefaultValue

See also

\Drupal\migrate\Plugin\MigrateProcessInterface

1 file declares its use of DefaultValue
DefaultValueTest.php in core/modules/migrate/tests/src/Unit/process/DefaultValueTest.php

File

core/modules/migrate/src/Plugin/migrate/process/DefaultValue.php, line 49

Namespace

Drupal\migrate\Plugin\migrate\process
View source
class DefaultValue extends ProcessPluginBase {
    
    /**
     * {@inheritdoc}
     */
    public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
        if (!empty($this->configuration['strict'])) {
            return $value ?? $this->configuration['default_value'];
        }
        return $value ?: $this->configuration['default_value'];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DefaultValue::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3

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