Hi, first of all: great module. I am currently using it in a project where I need to migrate data from Drupal 6 to Drupal 7. So I needed the Migrate API to support the Country field. I noticed that without a specific country field handler the Migrate module does not simply accept an ISO value. I've created the following, which you might want to include in the module to support the Migrate API.

First you need to implement hook_migrate_api() in the module:

<?php
function countries_migrate_api() {
  $api = array(
    'api' => 2,
  );
  return $api;
}
?>

Then you can use an include file to add the Country field handler:

<?php
class MigrateCountryFieldHandler extends MigrateFieldHandler {
  public function __construct() {
    $this->registerTypes(array('country'));
  }

  public function prepare(stdClass $entity, array $field_info, array $instance, array $values) {
    $arguments = (isset($values['arguments']))? $values['arguments']: array();
    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
    $delta = 0;

    foreach($values as $value) {
      $return[$language][$delta]['iso2'] = $value;
      $delta++;
    }

    return isset($return) ? $return : NULL;
  }
}
?>

The above works for me in my situation, but I think it is generic enough to be helpful for others.

Cheers, Martijn

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Alan D.’s picture

Version: 7.x-2.x-dev » 7.x-2.0-beta2
Status: Closed (fixed) » Active

Did this work? Isn't it just hook_migrate_api()?

[Edit]
Someone edited the issue summary, so for context this change was:

- function countries_migrate_migrate_api() {
+ function countries_migrate_api() { 
Marty2081’s picture

Hi Alan, I don't understand the question. Anyway: without the CountryFieldHandler the Migrate API will not import any countries. I think the API needs to know how to handle any incoming values for a country field and that's what the Handler defines. The only thing it defines is that the incoming ISO value needs to be inserted into the entity at [language][delta]["iso2"].

Marty2081’s picture

Ah I get the question now... Sorry :). Yes it needs to be just "countries_migrate_api()".

webflo’s picture

Version: 7.x-2.0-beta2 » 7.x-2.x-dev

Can you roll a proper patch?

Alan D.’s picture

Status: Active » Needs review
FileSize
1.69 KB

Here's the patch.

Marty2081’s picture

Sorry, I didn't follow the thread for a while. I see that Alan has already created a patch. Thanks for picking up the request!

Alan D.’s picture

Sorry for slow feedback.

Right, I've refactored this slightly so that the class will be autoloaded.

Looking at the complexity of the migration process, I'm not sure if this will be beneficial to users.

So I'll apply this patch if there is enough demand via a simple poll: Test and vote, and if we get 5 votes (0.5% users), I commit it.

This is one queue where you shouldn't use the Follow buttons!

BarisW’s picture

Hi Alan,

it's a nice improvement to the module, it doesn't add changes to the functionality for existing users and it's a small codebase. Because its loaded using a hook (hook_migrate_api) it doesn't add performance issue for those without the migrate module enabled.

Really, I just don't get why you even question the need of adding this.

Anyway; you've got my vote.

Alan D.’s picture

Status: Needs review » Fixed

Due to the speed of the first response, I guess that there is a need :)

Committed, thanks.

BarisW’s picture

Cool, thanks ;)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

radiobuzzer’s picture

Version: 7.x-2.0-beta2 » 7.x-2.x-dev
Component: Code » Documentation
Status: Active » Needs work

Hi. It's nice that this has been implemented. But how about some documentation? It is still not clear how to call these functions from Migrate API

Marty2081’s picture

There is not much more to it then that you can add a FieldMapping to a 'country' type field of an entity in D7 and mapping it to a country ISO value from your source. If you want to know how to create FieldMappings and in other words: 'use the Migrate API', I think the documentation of the Migrate API module suffices.

radiobuzzer’s picture

Component: Documentation » Code
Status: Needs work » Closed (fixed)

ok. this easy it didn't work in my case, so i will submit a bug report later

Marty2081’s picture

Yeah, I created this more than a year ago and haven't used it since, so I don't know if it still works. If you have found an issue, create a new one so that it can be looked at.

Alan D.’s picture

From memory, I had no issues either. Please clearly state any steps / code that you had issue with in the follow-up.

Cheers

Alan D.’s picture

Issue summary: View changes

Corrected typo

scorchio’s picture

Issue summary: View changes

In this comment, Alan D. posted a way to process the country no matter what format it is in. I think this would be useful to have by default (I just had to migrate ISO3 country codes and it took a while to recognize that I should convert the values.)