You are here

function services_client_migrate_add_mapping in Services Client 7.2

Create mapping plugins by old configurration.

Parameters

EventHandler $handler: Event handler.

array $field: Legacy field mapping configuration.

array $empty: Empty mapping configuration.

1 call to services_client_migrate_add_mapping()
services_client_migrate_hook in ./services_client.legacy.inc
Migrate old hook with mapping to new system.

File

./services_client.legacy.inc, line 206
Contains functions required for automated converting old Services Client version 1 hooks to events. This file is included only in drush command 'services-client-migrate-hooks'.

Code

function services_client_migrate_add_mapping($handler, $field, $empty) {
  foreach ($field as $local => $remote) {
    $local_info = services_client_migrate_get_mapping_info($local);
    $remote_info = services_client_migrate_get_mapping_info($remote);
    $reader_config = $formatter_config = array();
    $reader = $formatter = '';

    // Add reader
    if ($local_info['type'] == SERVICES_CLIENT_FIELD_TYPE_PROPERTY) {
      $reader = 'ServicesClientPropertyReader';
      $reader_config = $local_info['data'];
    }
    elseif ($local_info['type'] == SERVICES_CLIENT_FIELD_TYPE_FIELD) {
      $reader = 'ServicesClientFieldReader';
      $reader_config = $local_info['data'] + array(
        'all_values' => TRUE,
      );
    }
    elseif ($local_info['type'] == SERVICES_CLIENT_FIELD_TYPE_FIELD_MULTI) {
      $reader = 'ServicesClientFieldReader';
      $property = drush_prompt("Enter property name for local field '{$local}'", 'value', TRUE);
      $reader_config = $local_info['data'] + array(
        'all_values' => TRUE,
        'property' => $property,
      );
    }
    else {
      throw new Exception("Unknown reader type for '{$local}'.");
    }

    // Add formatter info
    if ($remote_info['type'] == SERVICES_CLIENT_FIELD_TYPE_PROPERTY) {
      $formatter = 'ServicesClientPropertyFormatter';
      $formatter_config = $remote_info['data'] + array(
        'multivalue' => 'force_single',
        'empty' => services_client_migrate_get_empty_info($remote_info, $empty),
        'default_value' => '',
      );
    }
    elseif ($remote_info['type'] == SERVICES_CLIENT_FIELD_TYPE_FIELD) {
      $formatter = 'ServicesClientFieldFormatter';
      $formatter_config = $remote_info['data'] + array(
        'multivalue' => 'all_values',
        'empty' => services_client_migrate_get_empty_info($remote_info, $empty),
        'default_value' => '',
      );
    }
    elseif ($remote_info['type'] == SERVICES_CLIENT_FIELD_TYPE_FIELD_MULTI) {
      $property = drush_prompt("Enter property name for remote field '{$remote}'", 'value', TRUE);
      $formatter = 'ServicesClientFieldFormatter';
      $formatter_config = $remote_info['data'] + array(
        'multivalue' => 'all_values',
        'empty' => services_client_migrate_get_empty_info($remote_info, $empty),
        'default_value' => '',
        'property' => $property,
      );
    }
    elseif ($remote_info['type'] == SERVICES_CLIENT_FIELD_TYPE_D6_FIELD) {
      $formatter = 'ServicesClientFieldD6Formatter';
      $formatter_config = $remote_info['data'] + array(
        'multivalue' => 'all_values',
        'empty' => services_client_migrate_get_empty_info($remote_info, $empty),
        'default_value' => '',
      );
    }
    else {
      throw new Exception("Unknown reader type for '{$local}'.");
    }
    $uuid = $handler
      ->addPlugin('mapping', 'ServicesClientMappingPlugin');
    $config = array(
      'reader' => $reader,
      'formatter' => $formatter,
      'reader_config' => $reader_config,
      'formatter_config' => $formatter_config,
    );
    $handler
      ->setPluginConfig('mapping', $uuid, $config);
  }
}