Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I've a content type with CCK Link field in my old site.
How can I migrate data from this field (title and URL) to Link field in D7 site using Migrate module?
Comment | File | Size | Author |
---|---|---|---|
#9 | 1004066-link_module_migration_handler-9-D7.patch | 3.32 KB | damien_vancouver |
#8 | 1004066-link_module_migration_handler-8-D7.patch | 3.32 KB | damien_vancouver |
#4 | link.migrate.txt | 997 bytes | royjs |
#3 | link.migrate.txt | 1.5 KB | zabelc |
#2 | link.migrate.txt | 954 bytes | royjs |
Comments
Comment #1
mikeryanSupport for contrib modules would go into Migrate Extras.
To do this, you would need to implement a destination handler. For examples, the best place to look is in the migrate module, in plugins/destinations/fields.inc, where the support for core CCK fields is.
Comment #2
royjs CreditAttribution: royjs commentedI did a little something implemeting a MigrateFieldHandler. The problem with using a MigrateFieldHandler is that the fields have already been added so you're stuck with one field to fill two values (title and url). What I did, (which is sort of a hack) is concat the two values with a pipe (|) in my query and split them in the prepare method.
I joined the code to this post. All you have to do is change the file extension to .inc, copy it in the migrate_extras directory and add it in the migrate_extras.info list of files.
I also tried to use a MigrateDestinationHandler but I couldn't figure out how to remove the field added by the FieldsNodeHandler. And after I added my two new fields I wasn't sure how to deal with them in the prepare method either. I think the best would be to find a way to implement a MigrateFieldHandler that could split the field so it can have two mappings, one for his url column and one for his title column.
Comment #3
zabelc CreditAttribution: zabelc commented@royjs I like the idea of being able to map different parts of the link field using separate top-level fields; but in the interim it strikes me that you might be able to meet this need without field concatenation by using arguments similar to the way the text and summary field supports an attribute for the summary or excerpt. The client side of this API is demonstrated in the BeerNodeMigration (around line 343 of beer.inc) in the way the body excerpt is mapped. Perhaps you could default to supporting the url via the normal mapping, and allow the title field to be mapped as an argument.
All that said, I tried your patch, and needed to modify a few things to get it to work (it may be because I'm currently pointing to the Dev stream of the migrate module). I've attached my updated version to this comment.
Comment #4
royjs CreditAttribution: royjs commentedThanks a lot, that's exactly the kind of solution I was looking for.
I changed the class to use an argument. You'll have to change a few things to make it work on 7.x-2.x-dev since I'm using 6.x-2.x-dev.
It can be used like this (title and url being the fields names in the source database) :
$arguments = MigrateLinkFieldHandler::arguments(array('source_field' => 'title'));
$this->addFieldMapping('field_url', 'url')
->arguments($arguments);
Comment #5
BTMash CreditAttribution: BTMash commented@zabelc, how do you work with multiple values in the arguments? That is one part that I have been struggling with over the course of this. Any help you can provide w/ your solution would be helpful :)
Edit: Nevermind - seems like you went with the separator route which would work :)
Comment #6
mikeryanSee also #943140: Support migration into phone fields.
The argument handling looks wrong - in the function, $arguments['source_field'] should hold the name of the source field containing the desired title value.
Comment #7
royjs CreditAttribution: royjs commentedI'm not sure to understand what you mean when you say that the argument handling looks wrong.
The file I submitted in #4 works fine for me.
Are you talking about the MigrateLinkFieldHandler argument ?
If I'm not mistaken, in my example this line :
$arguments = MigrateLinkFieldHandler::arguments(array('source_field' => 'title'));
means that MigrateLinkFieldHandler will receive an argument containing the value of the 'title' source field, which is what we need. Of course you have to change 'title' to the name of the field for the link's title in your source database.
Comment #8
damien_vancouver CreditAttribution: damien_vancouver commentedI've fixed up the patch from #4 so that it has correct (Migrate 7.x-2.x style) argument handling code.
I also added some comments in link.migrate.inc showing how to use it.
The attached patch is against 7.x-2.0-rc1 (but will probably work for 7.x-2.x as well, or I can reroll if needed).
The MigrateFieldHandler is implemented in link.migrate.inc, which is included from migrate_extras.info.
Instructions on use, from the comments:
Comment #9
damien_vancouver CreditAttribution: damien_vancouver commentedack, some bad grammar in my comments.. fixed for attached #9 patch.
Comment #10
rt_davies CreditAttribution: rt_davies commentedThanks everyone for the contributions. I've almost got patch #9 working for me, with one minor problem. I don't know if it's my mapping or the field handler. In short, the field mapping has a separator on it, which is working for the URL, but not for the title.
Do I need to add a separator to the arguments array or something?
Mapping:
$arguments = MigrateLinkFieldHandler::arguments(array('source_field' => 'ext_ref_titles'));
$this->addFieldMapping('field_external_refs', 'ext_ref_urls')
->arguments($arguments)
->separator('|');
Result in my field table:
URL Title
http://example.com/dest1.html Title 1|Title 2
http://example.com/dest2.html Title 1|Title 2
Comment #11
FrequenceBanane CreditAttribution: FrequenceBanane commentedWith #9 patch, I get the following error message : Fatal error: Declaration of MigrateLinkFieldHandler::prepare() must be compatible with that of MigrateFieldHandler::prepare() in [...]/sites/all/modules/migrate_extras/link.inc on line 85
With the last release of Migrte, MigrateFieldHandler::prepare() seems to have changed and so must do MigrateLinkFieldHandler::prepare()
Comment #12
grendzy CreditAttribution: grendzy commentedAccording to the migrate project page, adding the handler to the link module directly is preferred. Given that the link maintainer has already opened #1010850: Update MigrateFieldHandler to work with migrate-7.x-2.4 and later, I think this should be marked as a duplicate.
Comment #13
philipz CreditAttribution: philipz commentedI removed
stdClass
from first argument ofprepare
function and it fixed problem #11.Comment #14
commonpike CreditAttribution: commonpike commentedGiven that its still (2 years later) not in the link module, and its pretty hard to uninstall / upgrade the link module to a *-dev version on working site, it would be nice if this code was in the extras ... i'm glad its here and i'm patching.