The callback plugin, part of the Migrate API, can be used to apply a PHP function or class method as one step in a process pipeline. Before Drupal 9.2.0, this plugin was limited to functions or methods with exactly one argument.
As of Drupal 9.2.0, the plugin accepts the optional configuration argument unpack_source
. When this argument is set, the value in the process pipeline must be an array and it is interpreted as an argument list.
Before Drupal 9.2.0
process:
destination_field:
plugin: callback
callable: mb_strtolower
source: source_field
The callback function mb_strtolower() takes one argument.
Starting with Drupal 9.2.0
The above example works as before. With the new unpack_source
option, the callback
plugin can now use a 2-variable function such as rtrim():
source:
# plugin ...
constants:
slash: /
process:
field_link_url:
- plugin: callback
callable: rtrim
unpack_source: true
source:
- url
- constants/slash
The callback function rtrim() takes two arguments. This example will remove the trailing /
, if any, from the source URL.
Note: This system will also allow calling a function with no arguments by using unpack_source and using an empty array as a source:
process:
time:
plugin: callback
callable: time
unpack_source: true
source: { }
Comments
This looks good to me. If I
This looks good to me. If I had to suggest anything, we could link to the php docs for the two functions that are mentioned: rtrim and mb_strtolower since the details of how those functions accept arguments is of interest.
Multi-argument use in chaining?
It would be nice to have an idea of how to use multiple arguments to a callback in a chain of plugins.
substr() takes three arguments. In this case, one of them would be the output of the previous plugin.
mah ham