Change record status: 
Project: 
Introduced in branch: 
9.2.x
Introduced in version: 
9.2.0
Description: 

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: {  }
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

danflanagan8’s picture

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.

apotek’s picture

It would be nice to have an idea of how to use multiple arguments to a callback in a chain of plugins.

destination_field:
  - 
    plugin: some_plugin
    source: my_source_field
  - 
    pluginc: callback
    callback: substr
    unpack_source: true
    source: 
      - ????
      - 0
      - 16

substr() takes three arguments. In this case, one of them would be the output of the previous plugin.

mah ham