What I have: a D7 site with around 2k nodes on it, many of these nodes have images <img> in the html of the body but those images are not assigned to the image field. (It is an old drupal site that was upgraded to D7).

I would like to parse all the nodes so that the first image in the body content is assigned programatically to the image field of the node. Manually editing is not an option, as there are over 2.000 nodes on that site.

Is there a way to do that programatically? I guess field api may help (and so my tag) but I dont really know.



I would write a module that read the field_data_body table in database. This table contains a field called body_value where the values are stored. If the module find a mark, you can get the "src" attributte.

You don't forget to make a comparison in field_revision_body table to avoid problems.

Once you have the path of the file, you must save it as managed file in database. For this, take a look to "file.inc".

Here a little summary for last changes in D7.

I think it's important that you understand the difference between managed files and unmanaged files. Once you have saved the file, you could load the node, and assign to the field_image field, then node_save().

  • Thanks Rufein, I just got back to this (didnt have time during the week). I'll try what you recommend :)
    – versvs
    Mar 8 '13 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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