You are here

class Gate in Migrate Plus 8.5

Allow a source value to pass through the gate conditionally.

Imagine the source value as wanting to get through the gate. We provide a different source/destination field that acts as the key. We compare to a set of valid keys. We declare whether the key locks the gate or unlocks the gate.

This is different from skip_on_value because in that plugin, the source is compared to a value. In this plugin, the source is not compared to anything. The source just wants to get through a gate that is operated by another source/destination field.

Unlike skip_on_value, there is no configurable method. The method is essentially restricted to 'process'.

The source is not modified if it passes through the gate.

@MigrateProcessPlugin( id = "gate" )

Available configuration keys:

  • use_as_key: source or destination field to be used as the key to the gate.
  • valid_keys: Value or array of values that are valid keys.
  • key_direction: lock or unlock.

@codingStandardsIgnoreStart

Examples:

Migrate an email address if an opt_in field is set.


  field_email:
    plugin: gate
    source: email
    use_as_key: opt_in
    valid_keys: TRUE
    key_direction: unlock

Colorado requires salary_range data to be displayed. Only migrate salary_range if state is CO. Maybe the data is sloppy and sometimes they use the full state name.


  field_salary_range:
    plugin: gate
    source: salary_range
    use_as_key: state_abbr
    valid_keys:
      - CO
      - Colorado
    key_direction: unlock

While importing baseball players, don't import batting averages for pitchers. The position we use as the key to the gate is stored in a destination field, indicated by @.


  field_batting_average:
    plugin: gate
    source: batting_average
    use_as_key: @position
    valid_keys:
      - RHP
      - LHP
    key_direction: lock

@codingStandardsIgnoreEnd

Hierarchy

Expanded class hierarchy of Gate

1 file declares its use of Gate
GateTest.php in tests/src/Unit/process/GateTest.php

File

src/Plugin/migrate/process/Gate.php, line 82

Namespace

Drupal\migrate_plus\Plugin\migrate\process
View source
class Gate extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (empty($this->configuration['valid_keys']) && !array_key_exists('valid_keys', $this->configuration)) {
      throw new MigrateException('Gate plugin is missing valid_keys configuration.');
    }
    if (empty($this->configuration['use_as_key']) && !array_key_exists('use_as_key', $this->configuration)) {
      throw new MigrateException('Gate plugin is missing use_as_key configuration.');
    }
    if (empty($this->configuration['key_direction']) && !array_key_exists('key_direction', $this->configuration)) {
      throw new MigrateException('Gate plugin is missing key_direction configuration.');
    }
    if (!in_array($this->configuration['key_direction'], [
      'lock',
      'unlock',
    ], TRUE)) {
      throw new MigrateException('Gate plugin only accepts the following values for key_direction: lock and unlock.');
    }
    $valid_keys = is_array($this->configuration['valid_keys']) ? $this->configuration['valid_keys'] : [
      $this->configuration['valid_keys'],
    ];
    $key = $row
      ->get($this->configuration['use_as_key']);
    $key_is_valid = in_array($key, $valid_keys, TRUE);
    $key_direction = $this->configuration['key_direction'];
    $value_can_pass = $key_is_valid && $key_direction == 'unlock' || !$key_is_valid && $key_direction == 'lock';
    if ($value_can_pass) {
      return $value;
    }
    else {
      if ($key_direction == 'lock') {
        $message = sprintf('Processing of destination property %s was skipped: Gate was locked by property %s with value %s.', $destination_property, $this->configuration['use_as_key'], $key);
      }
      else {
        $message = sprintf('Processing of destination property %s was skipped: Gate was not unlocked by property %s with value %s. ', $destination_property, $this->configuration['use_as_key'], $key);
      }
      throw new MigrateSkipProcessException($message);
    }
  }

}

Members

Namesort descending Modifiers Type Description 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 2
Gate::transform public function Performs the associated process. Overrides ProcessPluginBase::transform
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
ProcessPluginBase::multiple public function Indicates whether the returned value requires multiple handling. Overrides MigrateProcessInterface::multiple 3
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.