Filter on Condition
Filters an input array on a condition.
This works like php's array_filter function. We keep the values that meet the condition and throw away the values that do not meet the condition.
Available configuration keys:
- condition: The condition plugin to evaluate. Can be either:
- The id of the condition. This is possible if the condition does not require any configuration, such as the 'empty' condition.
- 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.
-
preserve_keys: (optional) Set to TRUE to preserve array keys. Defaults to FALSE.
Please note that when using 'filter_on_condition', it makes no sense to configure the 'source' directly on the condition. The source must be configured on the process plugin in order for this plugin to be useful.
Examples
1. Remove empty values from an array
We keep values from the source array that are not empty.
process:
no_empty_values:
plugin: filter_on_condition
condition: not:empty
source: my_source_array2. Clean up an array of lookup values prior to sub-process
Consider doing a migration lookup against a paragraphs migration, which will have two destination values. If the no_stub option is set, then we will get null returned when a lookup is no successful. We might end up with an array that looks something like this:
[
['123', '456'],
null,
['789', '987'],
]Passing that to a sub-process will not work (like it will blow up) because of the null. So let's filter out that value with filter_on_array before the subprocess!
process:
field_paragraphs:
-
plugin: migration_lookup
source: my_source
migration: paragraphs_migration
no_stub: true
-
plugin: filter_on_condition
condition: not:is_null
-
plugin: sub_process
process:
target_id: '0'
target_revision_id: '1'3. Remove old dates from an array
Filter field_timestamps so that we keep upcoming dates and remove dates that are in the past.
process:
upcoming_dates:
plugin: filter_on_condition
source: field_timestamps
condition:
plugin: not:older_than
format: 'U' #format of timestamps
value: 'now'4. Remove tag ids that do not correspond to tags
Assuming source_tag_ids contains taxonomy term ids, we can save ourselves some validation errors by removing values that do not correspond to a valid entity.
process:
field_tags:
plugin: filter_on_condition
source: source_tag_ids
condition:
plugin: entity_exists
entity_type: taxonomy_termHelp improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion