1

Is there an easy way to migrate Asset-Fields to the native File Manager without loosing the referenced Image?

2

It depends upon what you call 'easy'. It can be done, but is quite complicated, and AFAIK none has written a utility to do this migration for you (though it probably could be created).

Assets bases its file referencing on the File Upload directories used natively by EE, but actually references the files stored in a quite different way.

For EE native files, the file information in the field looks like this within the SQL database:

EE File Field ->    {filedir_x}image_name.jpg

Where {filedir_x} relates to the upload location used to store the file.

For Assets fields, the SQL database stores just the file name in the field,

Assets File Field -> image_name.jpg

When you access an Assets field in a template, Assets adds the equivalent of the EE Filedir information by searching for the file name within a SQL table dedicated to providing Asset location information (I'm guessing it is exp_assets_index_data).

So, migrating from Assets to EE File Manager fields is certainly possible, and in some ways fairly straightforward. But the work required is not without some heavy lifting. At the very least you would need to (be able to) do the following steps:

  1. Move all of your Assets files to the 'top-level' folders within Assets (since EE's File Manager does not recognise / see "subdirectories") - this is easily done by drag-and-drop within Assets, so not too hard.
  2. Create new EE File fields in your EE system to match the existing Assets fields (or at least the ones you want to migrate)
  3. Copy the file names from the Assets Fields to the equivalent File fields, and prefix them with an appropriately formatted tag to indicate which of the EE File Upload locations the file is stored in. Doing this probably involves editing your SQL database directly. One way to do this is to batch your SQL edits based on which location the images / files are in: a simple approach would be to write a custom EE template to generate the appropriate SQL commands for this. The actual SQL you need will depend on which version of EE you are using (as data for native EE4 fields are stored differently in SQL to fields created in EE3 or EE2).
  4. Make sure that equivalent EE File upload locations exist for each top-level Assets folder, and are sync'd (after you've moved the files to the Assets top-level folder). Then test to see if the newly populated native image fields are being recognised within a test template. If so, modify your templates to use the new fields and celebrate... :)

As ever, if you set out to do this type of work make sure you back everything up before you start, and do the work on a development server...

HTH.

2

I've been working on upgrading an EE2 6-site MSM installation up to EE5 and in the process realised that Assets now requires a separate license for each site. So to save myself $600 I wrote a script that converts all instances of Assets to EE's native File including Assets used in Matrix columns (for various reasons I can't use EE's Matrix to Grid converter due to the column fieldtypes in use).

It's specific to my installation but it could easily be tweaked. I set it up as a function in a custom module I use and call it from a template with the parameter do=assets or do=matrix to perform one conversion or the other.

ee()->utilitylib is a custom library I use for many things including database queries and dumping data for debugging so those function calls would need to be switched out for other query functions.

It's not pretty or commented but it works. As always backup, backup, backup!!

Note that the File Directories will all need to be resynced after conversion.

Maybe this will be useful to someone else one day :)

===

public function assets2file()
{

        $do = ee()->TMPL->fetch_param('do');


    // SINGLE ASSET FIELDTYPE
        if($do == 'assets')
        {
        $sql = "SELECT field_id, site_id, field_name, field_content_type, field_settings FROM exp_channel_fields
                            WHERE field_type = 'assets'";
            $query_assets = ee()->utilitylib->db_query_result($sql);
            if($query_assets)
            {                   
                foreach($query_assets as $row)
                {
                    $settings_assets = unserialize(base64_decode($row['field_settings']));
                    $filedir = str_replace('ee:', '', $settings_assets['filedirs'][0]);
                    $field_content_type = (stripos($row['field_name'], 'image') !== FALSE || stripos($row['field_name'], 'logo') !== FALSE) ? 'image' : 'all';
                    $settings_file = array(
                        'field_content_type' => $field_content_type,
                        'allowed_directories' => $filedir,
                        'num_existing' => 50,
                        'field_fmt' => 'none'
                    );
                    //ee()->utilitylib->dumpthis($settings_file);

                    // update channel_fields: fieldtype, field_content_type, field_settings
                    $data = array(
                        'field_type' => 'file',
                        'field_settings' => base64_encode(serialize($settings_file))
                    );
                    //ee()->utilitylib->dumpthis($data);
                    ee()->db->where('site_id', $row['site_id']);
                    ee()->db->where('field_id', $row['field_id']);
                    ee()->db->update('channel_fields', $data);

                    //update channel_data: prefix {filedir_x} if not present
                    $field = 'field_id_'.$row['field_id'];
                    $sql = "SELECT entry_id, $field as colval FROM exp_channel_data WHERE site_id = {$row['site_id']} AND $field <> ''";
                    //ee()->utilitylib->dumpthis($sql);
                    $query2 = ee()->utilitylib->db_query_result($sql);
                    if($query2)
                    {
                        foreach($query2 as $row2)
                        {
                            $update_value = (substr($row2['colval'],0,8) == '{filedir') ? $row2['colval'] : "{filedir_$filedir}".$row2['colval'];
                            ee()->db->where('entry_id', $row2['entry_id']);
                            ee()->db->update('channel_data', array($field => $update_value));
                        }
                    }

                }
            }

        }


    // MATRIX ASSETS COLUMNS
        if($do == 'matrix')
        {

        $sql = "SELECT col_id, site_id, field_id, col_name, col_type, col_settings FROM exp_matrix_cols WHERE col_type = 'assets'";
            $query_matrix = ee()->utilitylib->db_query_result($sql);
            if($query_matrix)
            {   
                //ee()->utilitylib->dumpthis($query_matrix);
                foreach($query_matrix as $row)
                {
                    //ee()->utilitylib->dumpthis($row);
                    $settings_assets = unserialize(base64_decode($row['col_settings']));
                    //ee()->utilitylib->dumpthis($settings_assets);
                    $filedir = str_replace('ee:', '', $settings_assets['filedirs'][0]);
                    $field_content_type = (stripos($row['col_name'], 'image') !== FALSE || stripos($row['col_name'], 'logo') !== FALSE) ? 'image' : 'all';
                    $settings_file = array(
                        'content_type' => $field_content_type,
                        'directory' => $filedir,
                        'file_num_existing' => ''
                    );
                    if($field_content_type == 'all') $settings_file['file_show_existing'] = 'y';
                    //ee()->utilitylib->dumpthis($settings_file);

                    // update channel_fields: fieldtype, field_content_type, field_settings
                    $data = array(
                        'col_type' => 'file',
                        'col_settings' => base64_encode(serialize($settings_file))
                    );
                    //ee()->utilitylib->dumpthis($data);
                    ee()->db->where('col_id', $row['col_id']);
                    ee()->db->where('field_id', $row['field_id']);
                    ee()->db->update('matrix_cols', $data);

                    //update channel_data: prefix {filedir_x} if not present
                    $field = 'col_id_'.$row['col_id'];
                    $sql = "SELECT row_id, $field as colval FROM exp_matrix_data WHERE site_id = {$row['site_id']} AND field_id = {$row['field_id']} AND $field <> ''";
                    //ee()->utilitylib->dumpthis($sql);
                    $query2 = ee()->utilitylib->db_query_result($sql);
                    if($query2)
                    {
                        //ee()->utilitylib->dumpthis($query2);
                        foreach($query2 as $row2)
                        {
                            $update_value = (substr($row2['colval'],0,8) == '{filedir') ? $row2['colval'] : "{filedir_$filedir}".$row2['colval'];
                            //ee()->utilitylib->dumpthis($update_value);
                            ee()->db->where('row_id', $row2['row_id']);
                            ee()->db->update('matrix_data', array($field => $update_value));
                        }
                    }

                }
            }
            //die();

        }


}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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