You are here

function drush_migrate_mappings in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 migrate.drush.inc \drush_migrate_mappings()

Display field mappings for a migration.

File

./migrate.drush.inc, line 472
Drush support for the migrate module

Code

function drush_migrate_mappings($args = NULL) {
  try {
    $full = drush_get_option('full');
    $migrations = drush_migrate_get_migrations($args);
    foreach ($migrations as $name => $migration) {
      drush_print("\n" . dt('@migration Mappings', array(
        '@migration' => $name,
      )) . "\n");

      // In verbose mode, we'll also get source and destination field descriptions
      if ($full) {
        $destination = $migration
          ->getDestination();
        $dest_descriptions = array();
        if (method_exists($destination, 'fields')) {
          foreach ($destination
            ->fields($migration) as $machine_name => $description) {
            if (is_array($description)) {
              $description = reset($description);
            }
            $dest_descriptions[$machine_name] = strip_tags($description);
          }
        }
        $source = $migration
          ->getSource();
        $src_descriptions = array();
        if (method_exists($source, 'fields')) {
          foreach ($source
            ->fields() as $machine_name => $description) {
            if (is_array($description)) {
              $description = reset($description);
            }
            $src_descriptions[$machine_name] = strip_tags($description);
          }
        }
      }
      if (method_exists($migration, 'getFieldMappings')) {

        // First group the mappings. We want "interesting" mappings first, so
        // put the boring Done and DNM mappings last.
        $descriptions = array();
        $done = array();
        $dnm = array();
        foreach ($migration
          ->getFieldMappings() as $mapping) {
          $group = $mapping
            ->getIssueGroup();
          $lowergroup = drupal_strtolower($group);
          if ($lowergroup == dt('done')) {
            $done[$group][] = $mapping;
          }
          elseif ($lowergroup == dt('dnm') || $lowergroup == dt('do not migrate')) {
            $dnm[$group][] = $mapping;
          }
          else {
            $descriptions[$group][] = $mapping;
          }
        }
        $descriptions = array_merge($descriptions, $done, $dnm);

        // Put out each group header
        $table = array();
        if ($full) {
          $table[] = array(
            dt('Destination'),
            dt(''),
            dt('Source'),
            dt(''),
            dt('Default'),
            dt('Description'),
          );
        }
        else {
          $table[] = array(
            dt('Destination'),
            dt('Source'),
            dt('Default'),
            dt('Description'),
          );
        }
        $first = TRUE;
        foreach ($descriptions as $group => $mappings) {
          if ($first) {
            $first = FALSE;
          }
          else {
            $table[] = array(
              ' ',
            );
          }

          // Attempt to highlight the group header a bit so it stands out
          $group_header = '--- ' . drupal_strtoupper($group) . ' ---';
          $table[] = array(
            $group_header,
          );
          foreach ($mappings as $mapping) {
            if (is_array($mapping
              ->getDefaultValue())) {
              $default = implode(',', $mapping
                ->getDefaultValue());
            }
            else {
              $default = $mapping
                ->getDefaultValue();
            }
            $destination = $mapping
              ->getDestinationField();
            $source = $mapping
              ->getSourceField();
            if ($full) {
              if ($destination && $dest_descriptions[$destination]) {
                $dest_description = $dest_descriptions[$destination];
              }
              else {
                $dest_description = '';
              }
              if ($source && $src_descriptions[$source]) {
                $src_description = $src_descriptions[$source];
              }
              else {
                $src_description = '';
              }
              $table[] = array(
                $destination,
                $dest_description,
                $source,
                $src_description,
                $default,
                $mapping
                  ->getDescription(),
              );
            }
            else {
              $table[] = array(
                $destination,
                $source,
                $default,
                $mapping
                  ->getDescription(),
              );
            }
          }
        }
        if (drush_get_option('csv')) {
          foreach ($table as $row) {
            fputcsv(STDOUT, $row);
          }
        }
        else {
          drush_print_table($table, TRUE);
        }
      }
    }
  } catch (MigrateException $e) {
    drush_print($e
      ->getMessage());
    exit;
  }
}