You are here

public function DrupalVersion6::getSourceValues in Drupal-to-Drupal data migration 7.2

Populate a migration's source row object with field values.

Parameters

$row:

$entity_id:

Overrides DrupalVersion::getSourceValues

File

d6/d6.inc, line 221
Implementation of DrupalVersion for Drupal 6 sources.

Class

DrupalVersion6
Drupal 6 implementations of functions shared among multiple types of objects.

Code

public function getSourceValues($row, $entity_id) {
  if ($this->entityType == 'user') {

    // First get the core profile values.
    $this
      ->getProfileValues($row, $entity_id);

    // Next, look for any associated profile nodes, and fall through to
    // process them.
    $entity_id_list = $revision_id_list = array();
    foreach ($this->profileTypes as $type) {
      $node = Database::getConnection('default', $this->arguments['source_connection'])
        ->select('node', 'n')
        ->fields('n', array(
        'nid',
        'vid',
      ))
        ->condition('uid', $entity_id)
        ->condition('type', $type)
        ->execute()
        ->fetchObject();
      if ($node) {
        $entity_id_list[] = $node->nid;
        $revision_id_list[] = $node->vid;
      }
    }
  }
  elseif ($this->entityType == 'node') {
    $entity_id_list = array(
      $entity_id,
    );
    $revision_id_list = array(
      $row->vid,
    );
  }
  else {
    return;
  }
  $schema = Database::getConnection('default', $this->arguments['source_connection'])
    ->schema();
  foreach ($entity_id_list as $index => $entity_id) {
    $revision_id = $revision_id_list[$index];

    // Load up field data for dynamically mapped fields
    foreach ($this->sourceFieldInfo as $field_name => $field_info) {
      if ($field_info['type'] != 'taxonomy_term') {
        if ($field_info['type'] == 'body' || $field_info['type'] == 'title') {

          // Special handling to accomodate content_profile titles and bodies.
          $value = Database::getConnection('default', $this->arguments['source_connection'])
            ->select('node_revisions', 'nr')
            ->fields('nr', array(
            $field_info['type'],
          ))
            ->condition('vid', $revision_id)
            ->execute()
            ->fetchField();
          if ($value) {
            $row->{$field_name} = $value;
          }
        }
        else {

          // Fields in the base (content_type_foo) CCK table will have been
          // incorporated into the base query, so just look for the
          // shared/multiple field cases
          $table = "content_{$field_name}";
          if ($schema
            ->tableExists($table)) {
            $query = Database::getConnection('default', $this->arguments['source_connection'])
              ->select($table, 'f')
              ->fields('f')
              ->condition('vid', $revision_id);

            // There isn't always a delta,
            // @see http://drupal.org/node/1715244
            if ($schema
              ->fieldExists($table, 'delta')) {
              $query
                ->orderBy('delta');
            }
            $result = $query
              ->execute();
            foreach ($result as $field_row) {
              $data = NULL;

              // Unserialize file field data to break into alt/title/description.
              if ($this->sourceFieldInfo[$field_name]['type'] == 'filefield') {
                $data_column_name = $field_name . '_data';
                if (isset($field_row->{$data_column_name})) {
                  $data = unserialize($field_row->{$data_column_name});
                }
              }
              $i = 0;
              foreach ($this->sourceFieldInfo[$field_name]['columns'] as $display_name => $column_name) {
                if ($i++ == 0) {
                  $index = $field_name;
                }
                else {
                  $index = $display_name;
                }
                if (isset($row->{$index}) && !is_array($row->{$index})) {
                  $row->{$index} = array(
                    $row->{$index},
                  );
                }

                // File field subfields need to be pulled from $data.
                if ($display_name == "{$field_name}:alt" && is_array($data) && isset($data['alt'])) {
                  $row->{$index}[] = $data['alt'];
                }
                elseif ($display_name == "{$field_name}:title" && is_array($data) && isset($data['title'])) {
                  $row->{$index}[] = $data['title'];
                }
                elseif ($display_name == "{$field_name}:description" && is_array($data) && isset($data['description'])) {
                  $row->{$index}[] = $data['description'];
                }
                elseif (isset($field_row->{$column_name})) {
                  $row->{$index}[] = $field_row->{$column_name};
                }
              }
            }
          }
        }
      }
    }

    // Users only (nodes do this via their base query) - get the profile node
    // row
    if ($this->entityType == 'user') {
      foreach ($this->profileTypes as $type) {
        $cck_table = 'content_type_' . $type;
        if (Database::getConnection('default', $this->arguments['source_connection'])
          ->schema()
          ->tableExists($cck_table)) {
          $query = Database::getConnection('default', $this->arguments['source_connection'])
            ->select($cck_table, 'f')
            ->condition('vid', $revision_id);

          // The main column for the field should be rendered with
          // the field name, not the column name (e.g., field_foo rather
          // than field_foo_value).
          $fix_field_names = array();
          $field_found = FALSE;
          foreach ($this->sourceFieldInfo as $field_name => $info) {

            // All fields are stored in sourceFieldInfo. If there are
            // multiple non-core profile node types, we need to specify
            // which one each field belongs to.
            if (isset($info['bundle']) && $info['bundle'] != $type) {
              continue;
            }
            if (isset($info['columns']) && !$info['multiple'] && $info['db_storage']) {
              $i = 0;
              foreach ($info['columns'] as $display_name => $column_name) {
                if ($i++ == 0) {
                  $query
                    ->addField('f', $column_name, $field_name);
                }
                else {

                  // The database API won't allow colons in column aliases, so we
                  // will accept the default alias, and fix up the field names later.
                  // Remember how to translate the field names.
                  $clean_name = str_replace(':', '_', $display_name);
                  $fix_field_names[$clean_name] = $display_name;
                  $query
                    ->addField('f', $column_name);
                }
                $field_found = TRUE;
              }
            }
          }
          if ($field_found) {
            $data_row = $query
              ->execute()
              ->fetchObject();
            if (is_object($data_row)) {
              foreach ($data_row as $name => $value) {
                if (isset($fix_field_names[$name])) {
                  $name = $fix_field_names[$name];
                }
                $row->{$name} = $value;
              }
            }
          }
        }
      }
    }

    // And, load up the data for taxonomy terms.
    $query = Database::getConnection('default', $this->arguments['source_connection'])
      ->select('term_node', 'tn')
      ->fields('tn', array(
      'tid',
    ))
      ->condition('tn.vid', $revision_id);
    $query
      ->innerJoin('term_data', 'td', 'tn.tid=td.tid');
    $query
      ->fields('td', array(
      'vid',
    ));
    $result = $query
      ->execute();
    foreach ($result as $term_row) {
      $row->{$term_row->vid}[] = $term_row->tid;
    }
    if (Database::getConnection('default', $this->arguments['source_connection'])
      ->schema()
      ->tableExists('upload')) {
      $result = Database::getConnection('default', $this->arguments['source_connection'])
        ->select('upload', 'u')
        ->fields('u', array(
        'fid',
        'description',
        'list',
        'weight',
      ))
        ->condition('vid', $revision_id)
        ->orderBy('weight')
        ->execute();
      foreach ($result as $upload_row) {
        $row->upload[] = $upload_row->fid;
        $row->{'upload:description'}[] = $upload_row->description;
        $row->{'upload:list'}[] = $upload_row->list;
        $row->{'upload:weight'}[] = $upload_row->weight;
      }
    }
  }
}