You are here

function migrate_ui_edit_mappings in Migrate 7.2

Page callback to edit field mappings for a given migration.

_state

Parameters

$form:

$group_name:

$migration_name:

Return value

array

1 string reference to 'migrate_ui_edit_mappings'
migrate_ui_menu in migrate_ui/migrate_ui.module
Implementation of hook_menu().

File

migrate_ui/migrate_ui.pages.inc, line 1022
Pages for managing migration processes.

Code

function migrate_ui_edit_mappings($form, $form_state, $group_name, $migration_name) {
  drupal_set_title(t('Edit !migration', array(
    '!migration' => filter_xss_admin($migration_name),
  )));
  $form = array();
  $form['#tree'] = TRUE;
  $form['machine_name'] = array(
    '#type' => 'value',
    '#value' => $migration_name,
  );
  $migration = Migration::getInstance($migration_name);
  if (!$migration) {
    return $form;
  }
  $source_migration_options = array(
    '-1' => t(''),
  );
  $all_dependencies = array();
  foreach (migrate_migrations() as $machine_name => $migration_info) {
    $source_migration_options[$machine_name] = $machine_name;
    $dependencies = $migration_info
      ->getDependencies();
    if (!empty($dependencies)) {
      $all_dependencies[$machine_name] = $dependencies;
    }
  }
  if (is_a($migration, 'Migration')) {
    $source_fields = $migration
      ->getSource()
      ->fields();
    $dest_fields = $migration
      ->getDestination()
      ->fields($migration);
    $dest_key = $migration
      ->getDestination()
      ->getKeySchema();
    $field_mappings = $migration
      ->getFieldMappings();
    $form['field_mappings'] = array(
      '#type' => 'fieldset',
      '#title' => t('Field mappings'),
      '#collapsible' => TRUE,
      '#description' => t('For each field available in your Drupal destination, select the source field used to populate it. You can enter a default value to be applied to the destination when there is no source field, or the source field is empty in a given source item. Check the DNM (Do Not Migrate) box for destination fields you do not want populated by migration. Note that any changes you make here override any field mappings defined by the underlying migration module. Clicking the Revert button will remove all such overrides.'),
      '#theme' => array(
        'migrate_ui_field_mapping_form',
      ),
      '#prefix' => '<div id="field-mappings">',
      '#suffix' => '</div>',
    );

    // So the theme function knows whether to include the xpath column.
    $form['field_mappings']['#is_xml_migration'] = is_a($migration, 'XMLMigration');
    $form['source_fields'] = array(
      '#type' => 'fieldset',
      '#title' => t('Source fields'),
      '#collapsible' => TRUE,
      '#description' => t('Check off any source fields that are not being mapped to destination fields. They will be removed from the select lists above.'),
    );
    $base_options = array(
      '-1' => t(''),
    );
    $field_options = array();
    foreach ($source_fields as $name => $description) {

      // Clean up the source field description
      if (is_array($description)) {
        $description = reset($description);
      }
      $description = strip_tags($description);

      // Limit the description length
      if (strlen($description) > 50) {
        $short_description = substr($description, 0, 50) . '...';
      }
      else {
        $short_description = $description;
      }
      if (user_access(MIGRATE_ACCESS_ADVANCED)) {
        $label_format = '!description [!source_field]';
      }
      else {
        $label_format = '!description';
      }
      $label = t($label_format, array(
        '!source_field' => filter_xss_admin($name),
        '!description' => filter_xss_admin($description),
      ));
      $short_label = t($label_format, array(
        '!source_field' => filter_xss_admin($name),
        '!description' => filter_xss_admin($short_description),
      ));
      $dnm_value = 0;

      // Check for a click of the DNM box...
      if (isset($form_state['values']) && $form_state['values']['source_fields'][$name] == 1) {
        $dnm_value = 1;
      }
      else {

        // ... or a source-only mapping with the DNM issue group.
        foreach ($field_mappings as $mapping) {
          if ($mapping
            ->getSourceField() == $name && $mapping
            ->getIssueGroup() == t('DNM')) {
            $dnm_value = 1;
          }
        }
      }

      // So, if this field is not marked DNM, include it in possible source
      // options in the field mappings.
      if (!$dnm_value) {
        $field_options[$name] = $short_label;
      }
      $form['source_fields'][$name] = array(
        '#type' => 'checkbox',
        '#title' => $label,
        '#default_value' => $dnm_value,
      );
    }
    if (isset($field_mappings['is_new'])) {
      $default_is_new = $field_mappings['is_new']
        ->getDefaultValue();
    }
    else {
      $default_is_new = 0;
    }
    foreach ($dest_fields as $name => $description) {

      // Don't map the destination key unless is_new = 1 (ie, TRUE)
      if (isset($dest_key[$name]) && $default_is_new == 0) {
        continue;
      }
      if (is_array($description)) {
        $description = reset($description);
      }
      $options = $base_options + $field_options;
      $default_mapping = '-1';
      $default_value = '';
      if (isset($field_mappings[$name])) {
        $mapping = $field_mappings[$name];
      }
      else {
        $mapping = NULL;
      }

      // If the DNM box has been clicked, make sure we clear the mapping and
      // default value fields.
      if (isset($form_state['values']) && $form_state['values']['field_mappings'][$name]['issue_group'] == 1) {
        $dnm_value = 1;
      }
      else {

        // Determine the default field mapping - if we have an existing one, use
        // that, otherwise try to match on name.
        if (isset($field_mappings[$name])) {
          if ($mapping
            ->getSourceField()) {
            $default_mapping = $mapping
              ->getSourceField();
          }
          $default_value = $mapping
            ->getDefaultValue();
          $dnm_value = $mapping
            ->getIssueGroup() == t('DNM');
        }
        else {
          $dnm_value = 0;
        }
      }

      // Only show the machine name to advanced users.
      if (user_access(MIGRATE_ACCESS_ADVANCED)) {
        $label = '!description [!field_name]';
      }
      else {
        $label = '!description';
      }

      // Indent subfields and options to show their relationship to the parent.
      if (strpos($name, ':') > 0) {
        $description = '&nbsp;&nbsp;' . $description;
      }
      $form['field_mappings'][$name]['issue_group'] = array(
        '#type' => 'checkbox',
        '#default_value' => $dnm_value,
      );
      $form['field_mappings'][$name]['mapping'] = array(
        '#type' => 'select',
        '#title' => t($label, array(
          '!description' => $description,
          '!field_name' => $name,
        )),
        '#options' => $options,
        '#default_value' => $default_mapping,
      );
      $form['field_mappings'][$name]['default_value'] = array(
        '#type' => 'textfield',
        '#default_value' => $default_value,
        '#size' => 20,
      );
      $source_migration = NULL;
      if ($mapping) {
        $source_migration = $mapping
          ->getSourceMigration();
      }
      if (!$source_migration) {
        $source_migration = '-1';
      }
      $form['field_mappings'][$name]['source_migration'] = array(
        '#type' => 'select',
        '#options' => $source_migration_options,
        '#default_value' => $source_migration,
      );
      if (is_a($mapping, 'MigrateXMLFieldMapping')) {

        /** @var MigrateXMLFieldMapping $mapping */
        $form['field_mappings'][$name]['xpath'] = array(
          '#type' => 'textfield',
          '#default_value' => $mapping
            ->getXpath(),
          '#size' => 20,
        );
      }
    }
  }
  unset($source_migration_options[-1]);
  unset($source_migration_options[$migration_name]);
  $form['dependencies'] = array(
    '#type' => 'fieldset',
    '#title' => t('Dependencies'),
    '#collapsible' => TRUE,
    '#collapsed' => is_a($migration, 'Migration'),
    '#theme' => 'migrate_ui_field_mapping_dependencies',
    '#description' => t('Set any dependencies on other migrations here. A <em>hard dependency</em> means that this migration is not allowed to run until the dependent migration has completed (without forcing it). A <em>soft dependency</em> places no such requirement - it simply affects the default order of migration. Note that any migrations that would lead to circular dependencies are not listed here.'),
  );

  // Get the list of possible dependencies - it's the same as the list of
  // possible source migrations, minus the empty choice and ourselves.
  $dependency_options = array(
    0 => t('None'),
    1 => t('Hard'),
    2 => t('Soft'),
  );

  // Remove any migrations depending on us, directly or indirectly. First, get
  // a list of such migrations.
  $descendent_migrations = _migrate_ui_get_descendents($migration_name, $all_dependencies);
  $source_migration_options = array_diff_key($source_migration_options, $descendent_migrations);
  foreach ($source_migration_options as $machine_name) {
    if (in_array($machine_name, $migration
      ->getHardDependencies())) {
      $default_value = 1;
    }
    elseif (in_array($machine_name, $migration
      ->getSoftDependencies())) {
      $default_value = 2;
    }
    else {
      $default_value = 0;
    }
    $form['dependencies'][$machine_name] = array(
      '#type' => 'select',
      '#title' => check_plain($machine_name),
      '#default_value' => $default_value,
      '#options' => $dependency_options,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Apply changes'),
  );
  $form['revert'] = array(
    '#type' => 'submit',
    '#value' => t('Revert'),
    '#submit' => array(
      'migrate_ui_edit_mappings_revert',
    ),
  );
  return $form;
}