Callback.php

Same filename in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/Callback.php
  2. 10 core/modules/migrate/src/Plugin/migrate/process/Callback.php
  3. 11.x core/modules/migrate/src/Plugin/migrate/process/Callback.php

Namespace

Drupal\migrate\Plugin\migrate\process

File

core/modules/migrate/src/Plugin/migrate/process/Callback.php

View source
<?php

namespace Drupal\migrate\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * Passes the source value to a callback.
 *
 * The callback process plugin allows simple processing of the value, such as
 * strtolower(). The callable takes the source value as the single mandatory
 * argument. No additional arguments can be passed to the callback.
 *
 * Available configuration keys:
 * - callable: The name of the callable method.
 *
 * Examples:
 *
 * @code
 * process:
 *   destination_field:
 *     plugin: callback
 *     callable: strtolower
 *     source: source_field
 * @endcode
 *
 * An example where the callable is a static method in a class:
 *
 * @code
 * process:
 *   destination_field:
 *     plugin: callback
 *     callable:
 *       - '\Drupal\Component\Utility\Unicode'
 *       - strtolower
 *     source: source_field
 * @endcode
 *
 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
 *
 * @MigrateProcessPlugin(
 *   id = "callback"
 * )
 */
class Callback extends ProcessPluginBase {
    
    /**
     * {@inheritdoc}
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition) {
        if (!isset($configuration['callable'])) {
            throw new \InvalidArgumentException('The "callable" must be set.');
        }
        elseif (!is_callable($configuration['callable'])) {
            throw new \InvalidArgumentException('The "callable" must be a valid function or method.');
        }
        parent::__construct($configuration, $plugin_id, $plugin_definition);
    }
    
    /**
     * {@inheritdoc}
     */
    public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
        return call_user_func($this->configuration['callable'], $value);
    }

}

Classes

Title Deprecated Summary
Callback Passes the source value to a callback.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.