You are here

public function DrupalVersion5::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

d5/d5.inc, line 181
Implementation of DrupalVersion for Drupal 5 sources.

Class

DrupalVersion5
Drupal 5 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') {

        // 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) {
            $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},
                );
              }
              $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;
        $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).
        $field_found = FALSE;
        foreach ($this->sourceFieldInfo as $field_name => $info) {
          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 {
                $query
                  ->addField('f', $column_name, $display_name);
              }
              $field_found = TRUE;
            }
          }
        }
        if ($field_found) {
          $data_row = $query
            ->execute()
            ->fetchObject();
          foreach ($data_row as $name => $value) {
            $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.nid', $row->nid);
    $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;
    }
  }
}