1

I have created several content types and unfortunately, I made a mistake on some of the fields. The fields are field_image and field_screenshot.

Under manage fields, I use field_image and sometimes field_screenshot. Now I want to use one field only for featured image on my node content. So, how can I transfer the value of field_image to field_screenshot? Is this possible?

1

2 Answers 2

8

No, this is not easily possible through the Drupal interface. You will need to execute a custom SQL query to move your data from one field to another.

The following two queries will copy all field data from field_image to field_screenshot for each entity where field_screenshot is empty.

INSERT INTO field_data_field_screenshot
SELECT a.* 
FROM field_data_field_image a
LEFT JOIN field_data_field_screenshot b ON a.entity_id = b.entity_id
AND a.entity_type = b.entity_type
WHERE b.entity_id IS NULL

and

INSERT INTO field_revision_field_screenshot
SELECT a.* 
FROM field_revision_field_image a
LEFT JOIN field_revision_field_screenshot b ON a.entity_id = b.entity_id
AND a.entity_type = b.entity_type
WHERE b.entity_id IS NULL

After running this you can then delete the first field.

If field_screenshot alreadly has data that you want to overwrite you will need to delete it first using these queries. Make sure this is what you want to do first because this WILL delete your data

TRUNCATE TABLE field_data_field_screenshot
TRUNCATE TABLE field_revision_field_screenshot
8
  • Hi, When I deleted the field_image and replaced it with field_screenshot, all of the images are gone. The image url has this value <img src="http://www.sourcecodester.com/sites/default/files/styles/medium/public?itok=IYh6TS4U" alt>. I notice that the value of the images are deleted under file_managed table. Any help please. I don't know what to do now. I still have the backup copy of the images.
    – jaypabs
    Dec 13, 2013 at 15:21
  • Never mind. I already fixed the problem. Thank you
    – jaypabs
    Dec 13, 2013 at 15:53
  • Hi @Thomas4019, I want to transfer another field from field_code to field_code_file. Can I use the same query above and just change the field name say from field_data_field_image to field_data_field_code and field_data_field_screenshot to field_data_field_code_file?
    – jaypabs
    Jan 11, 2014 at 1:48
  • Yes, that should work. Just make sure to backup your database first just in case anything goes wrong.
    – Thomas4019
    Jan 11, 2014 at 5:24
  • The only problem I am facing now is when I delete the old field. When it's deleted, the file is also deleted. So how can I delete the old field but retain the file?
    – jaypabs
    Jan 11, 2014 at 6:58
0

Here is how you can do it in a module with COMBO of db_query and db_insert

$result = db_query('SELECT a.* 
  FROM {field_data_field_image} a
  LEFT JOIN {field_data_field_screenshot} b ON a.entity_id = b.entity_id
  AND a.entity_type = b.entity_type
  WHERE b.entity_id IS NULL'
);

$records = $result->fetchAll();
foreach ($records as $record) {
  db_insert('field_data_field_screenshot') // Table name no longer needs {}
        ->fields(array(
          'entity_type' => $record->entity_type,
          'bundle' => $record->bundle,
          'deleted' => $record->deleted,
          'entity_id' => $record->entity_id,
          . // All Columns
          .
          .
          .
        ->execute();
}

And repeat the revision field as well.

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.