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
Comment | File | Size | Author |
---|---|---|---|
#5 | countries-migrate-integration-1378662-5.patch | 1.69 KB | Alan D. |
Comments
Comment #1
Alan D. CreditAttribution: Alan D. commentedDid this work? Isn't it just hook_migrate_api()?
[Edit]
Someone edited the issue summary, so for context this change was:
Comment #2
Marty2081 CreditAttribution: Marty2081 commentedHi 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"].
Comment #3
Marty2081 CreditAttribution: Marty2081 commentedAh I get the question now... Sorry :). Yes it needs to be just "countries_migrate_api()".
Comment #4
webflo CreditAttribution: webflo commentedCan you roll a proper patch?
Comment #5
Alan D. CreditAttribution: Alan D. commentedHere's the patch.
Comment #6
Marty2081 CreditAttribution: Marty2081 commentedSorry, I didn't follow the thread for a while. I see that Alan has already created a patch. Thanks for picking up the request!
Comment #7
Alan D. CreditAttribution: Alan D. commentedSorry 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!
Comment #8
BarisW CreditAttribution: BarisW commentedHi 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.
Comment #9
Alan D. CreditAttribution: Alan D. commentedDue to the speed of the first response, I guess that there is a need :)
Committed, thanks.
Comment #10
BarisW CreditAttribution: BarisW commentedCool, thanks ;)
Comment #12
radiobuzzer CreditAttribution: radiobuzzer commentedHi. 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
Comment #13
Marty2081 CreditAttribution: Marty2081 commentedThere 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.
Comment #14
radiobuzzer CreditAttribution: radiobuzzer commentedok. this easy it didn't work in my case, so i will submit a bug report later
Comment #15
Marty2081 CreditAttribution: Marty2081 commentedYeah, 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.
Comment #16
Alan D. CreditAttribution: Alan D. commentedFrom memory, I had no issues either. Please clearly state any steps / code that you had issue with in the follow-up.
Cheers
Comment #16.0
Alan D. CreditAttribution: Alan D. commentedCorrected typo
Comment #17
scorchio CreditAttribution: scorchio commentedIn 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.)