You are here

function migrate_field_handler_invoke_all in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 migrate.module \migrate_field_handler_invoke_all()

Invoke any available handlers attached to a given field type.

Parameters

$entity: The object we are building up before calling example_save().

$field_info: Array of info on the field, from field_info_field().

$instance: Array of info in the field instance, from field_info_instances().

$values: Array of incoming values, to be transformed into the appropriate structure for the field type.

$method: Handler method to call (defaults to prepare()).

2 calls to migrate_field_handler_invoke_all()
MigrateFieldsEntityHandler::complete in plugins/destinations/fields.inc
MigrateFieldsEntityHandler::prepare in plugins/destinations/fields.inc

File

./migrate.module, line 194
API and drush commands to support migration of data from external sources into a Drupal installation.

Code

function migrate_field_handler_invoke_all($entity, array $field_info, array $instance, array $values, $method = 'prepare') {
  static $types_handled = array();
  static $methods_handled = array();
  static $disabled = null;
  $return = array();
  $type = $field_info['type'];
  static $class_list = null;
  if (!$class_list) {
    $class_list = _migrate_class_list('MigrateFieldHandler');
  }

  // No need to do this unserialize/variable_get/serialize so often,
  // it never changes.
  if (!is_array($disabled)) {
    $disabled = unserialize(variable_get('migrate_disabled_handlers', serialize(array())));
  }

  // This function is called a lot. Rather than determine if the field type is
  // handled once for every record, the value should be determined once per
  // execution.
  // The same can go for whether the handler/method pair exists
  if (!isset($types_handled[$type])) {
    foreach ($class_list as $class_name => $handler) {
      $types_handled[$type][$class_name] = $handler
        ->handlesType($type);
    }
  }
  if (!isset($methods_handled[$method])) {
    foreach ($class_list as $class_name => $handler) {
      $methods_handled[$method][$class_name] = method_exists($handler, $method);
    }
  }
  $handler_called = FALSE;
  foreach ($class_list as $class_name => $handler) {
    if (!in_array($class_name, $disabled) && $types_handled[$type][$class_name] && $methods_handled[$method][$class_name]) {
      migrate_instrument_start($class_name . '->' . $method);
      $result = call_user_func_array(array(
        $handler,
        $method,
      ), array(
        $entity,
        $field_info,
        $instance,
        $values,
      ));
      $handler_called = TRUE;
      migrate_instrument_stop($class_name . '->' . $method);
      if (isset($result) && is_array($result)) {
        $return = array_merge_recursive($return, $result);
      }
      elseif (isset($result)) {
        $return[] = $result;
      }
    }
  }
  if (!$handler_called && $method == 'prepare') {
    $handler = new MigrateDefaultFieldHandler();
    migrate_instrument_start('MigrateDefaultFieldHandler->prepare');
    $result = call_user_func_array(array(
      $handler,
      'prepare',
    ), array(
      $entity,
      $field_info,
      $instance,
      $values,
    ));
    migrate_instrument_stop('MigrateDefaultFieldHandler->prepare');
    if (isset($result) && is_array($result)) {
      $return = array_merge_recursive($return, $result);
    }
    elseif (isset($result)) {
      $return[] = $result;
    }
  }
  return $return;
}