Cybo Advanced Field EngineCyboweb Technologies · Documentation · v1.0.2

cafe_register_migration_transformer()

Purpose

Adds a value transformation to CAFE → Migration, shown next to the built-in ones in the mapping screen: turn old codes into new values, split text, look things up.

Syntax

cafe_register_migration_transformer( TransformerInterface|string $transformer, string $label = '', ?callable $callback = null, array $controls = [] ): bool

Parameters

Parameter Type Default Description
$transformer TransformerInterface|string - A transformer object, or a unique name (lowercase letters, digits, underscores).
$label string '' Name shown in the mapping screen (with a name).
$callback callable|null null With a name: function ( $value, array $options, TransformContext $context ) returning the new value.
$controls array [] Options the administrator can set, e.g. [ [ 'name' => 'prefix', 'label' => 'Prefix', 'type' => 'text' ] ].

Return value

true when registered, false for a missing callback or a taken name.

Example

use Cybo\AdvancedFieldEngine\Connector\Transform\TransformFailed;

add_action( 'cafe_register_migration_transformers', function () {
	cafe_register_migration_transformer(
		'sale_status',
		'Old status → sale status',
		function ( $value, array $options, $context ) {
			return match ( (string) $value ) {
				'1'     => 'available',
				'0'     => 'sold',
				default => throw new TransformFailed( "Unknown status {$value}." ),
			};
		}
	);
} );

Notes

  • Call it on the cafe_register_migration_transformers action.
  • Throw Cybo\AdvancedFieldEngine\Connector\Transform\TransformFailed (or return a WP_Error) when a value cannot be transformed: the record is reported and not written. Messages are shown to administrators; never include secrets.
  • $context gives the whole source record ($context->record->get( 'other_column' )), the target field, the migration and whether it is a dry run.
  • Transformations that look things up should implement Cybo\AdvancedFieldEngine\Connector\Transform\BatchTransformerInterface to load a whole batch with one query (Transformations).
  • Since 0.13.0.