If Condition

Last updated on
19 February 2024

Get configured properties based on result of a condition.

if_condition solves the same problem documented here. See also this feature request. It can also act essentially as a default_value process plugin that uses a source property as the default instead of a string literal.

Available configuration keys:

  • condition: The condition plugin to evaluate. Can be either:
    1. The id of the condition. This is possible if the condition does not require any configuration, such as the 'empty' condition.
    2. An array with a 'plugin' key that is the id of the condition. Any additional properties will be used as configuration when creating an instance of the condition.
  • do_get: (optional) property to get and return if condition is met. If not set, the source will be used.

  • else_get: (optional) property to get and return if condition is not met. If not set, NULL will be returned.

  • do_process: (optional) a process pipeline to run if the condition is met.

  • else_process: (optional) a process pipeline to run if the condition is met.

  • do_default_value: (optional) a literal string/array to return if the condition is met.

  • else_default_value: (optional) a literal string/array to return if the condition is not met.

Only one of do_get, do_process, and do_default_value may be set. Only one of else_get, else_process, and else_default_value may be set.

Examples

1.  Fully configured

If animal_family is 'bird', get feather_color. Otherwise, get fur_color.

process:
  animal_color:
    plugin: if_condition
    source: animal_family
    condition:
      plugin: equals
      value: bird
    do_get: feather_color
    else_get: fur_color

2. Nice way to set a default value

The core default_value process plugin cannot set a source/destination property as the default value. Using if_condition, you can easily use a source/destination value as the fallback. In this case we do not set do_get because we want the source to "pass through" if it meets the condition.

process:
  destination_value:
    plugin: if_condition
    source: my_source_value
    condition: not:empty
    else_get: my_fallback_source_value

If using a static value as the default value, you can use else_default_value setting. For example, we can set a cap on an imported value. Let's say we cap want to cap the number at 100:

process:
  number_with_cap:
    plugin: if_condition
    source: field_number
    condition:
      plugin: less_than
      source: 100
    else_default_value: 100

3. Re-create the core entity_exists process plugin

The core entity_exists plugin returns the source value if the source value corresponds to an entity that exists. Otherwise it returns NULL. By no setting do_get, the source value "passes through" if the condition is met. By not configuring else_get, we return null if the condition is not met.

process:
  field_tag:
    plugin: if_condition
    source: source_tid
    condition:
      plugin: entity_exists
      entity_type: taxonomy_term

4. Do some extra processing if a condition is not met

Let's say if the source uid is 0 or 1, you want to keep that value. But any other source uid should be used in a migration lookup.

process:
  uid:
    plugin: if_condition
    source: source_uid
    condition:
      plugin: in_array
      value:
        - 0
        - 1
    else_process:
      plugin: migration_lookup
      migration: users

Note that if you do not explicitly set the source for else_process (or do_process) the source of the if_condition plugin will be assumed.

5. Concatenate values conditionally

Consider a migration of entities that represent remote video. Each entity tells you the provider (YouTube or Vimeo) and the video's ID. But core media wants a url, so we have to do some fancy concatenation.

source:
  ...
  constants:
    youtube_base_url: 'https://youtube.com/watch?v='
    vimeo_base_url: 'https://vimeo.com/'
process:
  field_media_oembed_video:
    plugin: if_condition
    condition:
      plugin: equals
      value: youtube
    source: provider
    do_process:
      plugin: concat
      source:
        - constants/youtube_base_url
        - video_id
    else_process:
      plugin: concat
      source:
        - constants/vimeo_base_url
        - video_id

Note that in this case, both do_process and else_process explicitly define the source, and neither one uses the same source that is used by if_condition.

Also, note that do_process and else_process can contain multiple chained process plugins using the syntax you would expect.

6. Give meaningful title to stubs

Stubbed entities get unreadable random strings as the title. We can leverage if_condition to give a meaningful title. Here's how it could work for a typical node migration:

source:
  ...
  constants:
    fallback_title_prefix: "Stub for sourceid "
process:
  title:
    plugin: if_condition
    condition: not:empty
    source: title
    else_process:
      plugin: concat
      source:
        - constants/fallback_title_prefix
        - nid

Help improve this page

Page status: No known problems

You can: