0

Can I migrate a Drupal 7 field from one field type to another using Migrate and $this->systemOfRecord = Migration::DESTINATION;?

I'm trying to move field_phone which is an ordinary Field API text field into a mobile_number field which is also a Field API field but provided by the Mobile number module.

uid is the only listed source field, and that makes sense because it's the only field selected in the query, but I don't see any examples that use JOINs to select data from fields attached to an entity.

How do I add field_phone to the source?

public function __construct($arguments) {
  $this->sourceFields['field_phone'] = t('Phone');
  parent::__construct($arguments);

  $this->systemOfRecord = Migration::DESTINATION;

  $query = db_select('users', 'u')
    ->fields('u', array('uid'))
    ->condition('uid', 0, '>');
  $this->source = new MigrateSourceSQL($query);

  $this->destination = new MigrateDestinationUser(array());

  $this->map = new MigrateSQLMap($this->machineName,
    array('uid' => array(
      'type' => 'int',
      'unsigned' => TRUE,
      'default' => 0,
      'description' => "User's {users}.uid.",
    )), 
    MigrateDestinationUser::getKeySchema()
  );  
  $this->addFieldMapping('uid', 'uid');
  $this->addFieldMapping('field_phone_number', 'field_phone');
}

Additionally, If I can keep the same field machine name I rather do that then moving from field_phone to field_phone_number, but it's not important.

1 Answer 1

0

This is hard for me to believe, but the results are telling. The source fields were never loaded, so they weren't available to the destination handler. I added the following prepare() function to my Migration class so that the values would be available. I don't see any examples that look like this, so I'm almost certain there's a better way.

function prepareRow($current_row) {
  if (parent::prepareRow($current_row) === FALSE) {
    return FALSE;
  }

  $phone = db_select("field_data_field_phone", "p")
    ->fields("p", array("field_phone_value"))
    ->condition("p.entity_type", "user")
    ->condition("p.entity_id", $current_row->uid)
    ->execute()
    ->fetchField();
  $current_row->field_phone = $phone;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.