0

I have a text field that's been to featured an image since an earlier EE1 install and I would like to convert it to a File field type, so as to better utilize the image URL throughout different templates.

Can I simply change the type from Text Input to File? I'm doubtful, since there is so much other text in the input aside from the image URL. (ie: <img src="{filedir_4}file.jpg" alt="" height="300" width="300" />)

I came across this post with the same question but do not fully understand the solution provided. (I am also too new to comment there.) It says that I can add the following PHP to a template and it will do the migration itself:

<?php

$old_field = 'field_id_XX';
$new_field = 'field_id_XX';

$images = ee()->db->query("
    SELECT entry_id, $old_field FROM exp_channel_data WHERE $old_field != ''
");
foreach($images->result_array() as $image)
{
    preg_match('/src="([^"]*)"/i', $image[$old_field], $match);
    if(!empty($match))
    {
        $data = array(
            $new_field => $match[1]
        );
        ee()->db->query(
            ee()->db->update_string('exp_channel_data', $data, 'entry_id = '.$image['entry_id'])
        );
    }
}

?>

I've added the PHP to the related template (which allows PHP), replaced the field_id_XXs with the proper IDs, then loaded a few pages—but no migration is taking place.

Any suggestions?

  • 1
    You'll only need to load this once on a single PHP-enabled template, and it should migrate all entries which have an image in the specified field. Are you sure your field_ids are correct? You've created a new File field to move the images into? – Derek Hogue Jan 29 '14 at 20:51
  • Yes–I created a new File field (id 217) in the same group as the Text Input field (id 51), then loaded the PHP into the event template that uses the Text Input field, replacing the top two lines with $old_field = '51'; $new_field = '217'; I loaded an event page that uses the template and looked again at the page in EE—no migration has taken place. – Shaun Kardinal Jan 29 '14 at 21:45
  • 1
    Ah - you need to prefix those IDs with field_id_ = so $old_field = 'field_id_51'; $new_field = 'field_id_217'. – Derek Hogue Jan 29 '14 at 22:15
  • gan bless us, every one. cheers mate! – Shaun Kardinal Jan 29 '14 at 22:50
  • Great - just put this in as an answer. – Derek Hogue Jan 29 '14 at 22:59
1

You need to prefix those IDs with field_id_, e.g.,

$old_field = 'field_id_51';
$new_field = 'field_id_217';

That should get the migration working.

Note that you'll only need to load that code once in a single PHP-enabled template, and it should migrate all entries which have an image in the specified field (you mentioned "loading a few pages").

| improve this answer | |

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.