Hi,

I'm using CCK's filefield module and have a multi-value file field on my custom content type. It's actually an imagefield, but I don't think that should make any difference. I can easily migrate one filefield entry but am unsure how to handle it when multiple files need to be migrated for the one field. I'm trying to do it in a hook_migrate_prepare_node() function, but don't know what to set the $node->field_file_sourcefile variable too. I'm guessing it needs to be some sort of an array or object.

Cheers,
Stella

Comments

Status:Active» Needs review
StatusFileSize
new1.5 KB

Attached patch adds multi-value filefield functionality. To use, you will need to implement a hook_migrate_prepare_node() that sets up your filefield (e.g. $node->field_file_sourcefile) as an array of filepaths. The patch also works if just one filepath string (not an array) is configured.

Status:Needs review» Needs work

Ahh ok, so after reading this for a while i think i get it.

Its expecting you to pre-set the $node->$source_key value in your own hook_migrate_prepare_node() to be an array of file names.

However it no longer cleanly applies to dev

patching file filefield.migrate.inc
Hunk #1 FAILED at 15.
Hunk #2 succeeded at 36 (offset 11 lines).
1 out of 2 hunks FAILED -- saving rejects to file filefield.migrate.inc.rej

Status:Needs work» Needs review
StatusFileSize
new2.74 KB

Update to the patch, so it applies cleanly.

Required for http://drupal.org/project/migrate_drupal

Hello,

Thanks for this patch! I am looking into using it for several multivalue filefields so this is great.

I have one of the filefields that is not so straightforward and I'd like to know how best to use it to migrate the data over. I have this multivalue filefield that is inside a multigroup (cck3.x-dev)...together with two other fields, one has content taxonomy and the other is just a text field.

Below is how I've set the multigroup on Drupal:

Multigroup
File(filefield)                Fileformat(content taxonomy)    Version(text)
file1                           fileformatname3                     v1
file2                           fileformatname5                     v17

and here is how I have the incoming data/files

Tables Data
File1                      Fileformat1     Version1         File2                          Fileformat1           Version2
..../folder/file1         fileformatname3     v1              ..../folder/file2             fileformatname5         v17

I'd like to know whether it is possible to do it with migrate extras+the multivalue fields patch above.

Thanks,

KH

Patch @ #3 is failing on migrate_extras-6.x-1.0-beta1

patching file filefield.migrate.inc
Hunk #1 FAILED at 14.
Hunk #2 succeeded at 30 (offset -11 lines).

Status:Needs review» Needs work

Yeah, looks like dev has moved on since i rolled this patch. Its not that complicated, so should be easy enough to re-roll.

+1 subscribe. Having the same problem please please please post the new patch against current version and give us an real world example of hook_migrate_prepare_node() implementation for multivalue filefields.

Thank you so much :)

Title:multi-value filefieldsexplode() on comma?

Thanks Stella for the patch - it works great for me. However, in my case, the files in the DB were stuffed into one column, as comma separated values. I modified the patch so that it "just works" when it encounters such an instance, and in doing so eliminated the need to write any PHP code at all to import multi-value filefields.

The one caveat here is that your filenames cannot contain commas. I pity the soul who has to deal with that arrangement :)

Is this a patch worth posting, or is the "no commas in filenames" requirement to stringent?

Title:explode() on comma?multi-value filefields

gah, sorry for the title change... normally I'm not so bone-headed.

What about using the field "edit-multiple-separator" from the "migrate-content-set-mappings" form instead of hard-coded commas?

@wapnik: even better!

If the patch would get accepted, I can take the time to code it up against current dev.

@justintime: yeah, go for it!

Status:Needs work» Needs review
StatusFileSize
new3.23 KB

I unfortunately didn't have time to test - @wapnik, can you review? This patch is against 6.1 HEAD.

StatusFileSize
new1.71 KB

Silly Eclipse -- there's some extra cruft in that patch. Use this one instead.

Status:Needs review» Reviewed & tested by the community

Yes works good for me.

One note. If the filefield used is not a multiple filefield, the first file is migrated. The same case i think if there are less files permitted in the filefield than the record is holding, remaining files are rejected. That's the behavior i was expecting but i think it will be ok to put a message about those remaining files somewhere. A system (error) message will be enough.

subscribe

Patch from #15 works for me as well.

I went ahead and rolled the patch from #825352: Unable to migrate nodes containing empty (not required) filefield paths into this since they affect the same lines of code. And for readability, the same patch ignoring whitespace. I can certainly post another patch over on #825352 after this one is committed if you'd rather keep them separate.

This ("ignoring whitespace") works great! Thank you so much for fixing this, justintime and cedarm!

The only change I'd like to see is indenting that if statement you're wrapping with a foreach, as per the coding standards.

@tobiassjosten: indenting as per coding standards is the whitespace change.. apply the other patch and feel better that your site runs pretty code :)

Could I have some context on this patch? Can I use it to get multivalue file fields migrated using the UI of migrate 6.x-1.0 or does this apply only to the dev version?

Is this functionality usable only by coding hooks or can I use it from the UI as well?

You should be able to use the UI. The trick with migrate and filefields/imagefields is getting the source path right so the source file can be found. If I'm doing a simple migration and don't want to use hooks then I'll tweak the source data first.

For example, if you have "filename" in the source table, add another column "migrate_filename" and run an update statement:

UPDATE source_files SET migrate_filename = concat('source_files/', filename);

If your situation allow preparing the source data like this, then you can avoid implementing any hooks at all.

But you want a multi-value filefield, right? Filling a migrate_filenames column using SQL is a bit more complex. If you're up to the task, go for it. If you use commas to separate the filenames, just make sure no filenames actually contain commas. I suggest using pipes instead. Does that help?

Oh, and it applies to 6.x-1.x-dev. But you can also apply it to 6.x-1.0-beta1 if you first change line 17 of filefield.migrate.inc from:

<?php
   
if (isset($node->$source_key)) {
?>

to
<?php
   
if (!empty($node->$source_key)) {
?>

(Which is a patch from another issue.)

Status:Reviewed & tested by the community» Closed (won't fix)

Migrate and Migrate Extras V1 are no longer supported. Multi-value filefields are supported in Migrate V2.