I have code here that does the work for you

// Put this into an update.

if (!module_exists('mediafield')) {
  return t('');
}
 
$field = 'field_local_audio'; // This is specific to me.
$display_sepc = array (
  'description' => 'Flag to control whether this file should be displayed when viewing content.',
  'type' => 'int',
  'size' => 'tiny',
  'default' => 1,
  'unsigned' => TRUE,
  'not null' => TRUE,
);
 
$description_sepc = array (
  'description' => 'A description of the file.',
  'type' => 'text',
  'not null' => FALSE,
);

// Change the thingies
db_add_field("field_data_$field", $field_display, $display_sepc);
db_add_field("field_data_$field", $field_description, $description_sepc);
 
db_add_field("field_revision_$field", $field_display, $display_sepc);
db_add_field("field_revision_$field", $field_description, $description_sepc); //*/
 
// Add the data to config
$result = db_update('field_config')
  ->fields(array(
    'type' => 'file',
    'module' => 'file',
  ))
  ->condition('field_name', $field)
  ->execute();
 
module_disable(array('mediafield'));

Comments

aaron’s picture

should this be part of the media module instead?

dave reid’s picture

Project: File Entity (fieldable files) » D7 Media
Version: 7.x-2.0-unstable3 » 7.x-2.x-dev
Component: Code » Media field

Yeah I would welcome this to be fully supported in mediafield_uninstall().

philippejadin’s picture

Now that mediafield is deprecated, can we expect to have an "upgrade" path to filefield on 7.x-1.0?
This would be great !

dave reid’s picture

It's not too high on the list of priorities since we'll still be support the mediafield, but yes some time down the road we'll need a migration to move those fields to file fields. The code above is a start but it needs work to make it a more generic migration.

avillanueva-npr’s picture

The code above is mostly hard coded but it's a start. I wanted to put it here for posterity so we don't need to remember how to do this later down the line and instead search for it. Either way it's not taking into account the field that needs to be replaced (I hard coded it for our implementation here at NPR) nor does it remove the old fields that exist from the previous field implementation from Media 1.0

adarkling’s picture

Thanks avillanueva-npr !
That really came in handy! I adapted it a bit to handle the 2 issues you just spoke about.

/**
 * Controller for helloworld_convert_mediafield
 * I suppose it would be better to iterate through all records in table field_config
 * and convert everything with a 'media' type, but that would've been overkill.
 * Besides, you still need to edit the associated content types afterwards. -aDarkling-
 */
function helloworld_convert_mediafields_util() {
	helloworld_convert_mediafield('field_lead_image');
	helloworld_convert_mediafield('field_am_section_image');
	return t('Done converting deprecated media fields to file fields.');
}

/** Generic D7 Media 2.x mediafield -> filefield conversion script.
 * Adapted from avillanueva-npr's code at http://drupal.org/node/1493454
 * @param string $field
 */
function helloworld_convert_mediafield($field) {
$field_display = $field.'_display';
$display_sepc = array (
  'description' => 'Flag to control whether this file should be displayed when viewing content.',
  'type' => 'int',
  'size' => 'tiny',
  'default' => 1,
  'unsigned' => TRUE,
  'not null' => TRUE,
);

$field_description = $field.'_description';
$description_sepc = array (
  'description' => 'A description of the file.',
  'type' => 'text',
  'not null' => FALSE,
);

// Change the associated table structures
db_add_field("field_data_$field", $field_display, $display_sepc);
db_add_field("field_data_$field", $field_description, $description_sepc);
db_add_field("field_revision_$field", $field_display, $display_sepc);
db_add_field("field_revision_$field", $field_description, $description_sepc); 

// Can we delete the old columns in these tables?
// My installation had no problem with it, but check your tables for any 
// non-null values just in case.
db_drop_field("field_data_$field", $field.'_title');
db_drop_field("field_data_$field", $field.'_data');
db_drop_field("field_revision_$field", $field.'_title');
db_drop_field("field_revision_$field", $field.'_data');

// Add the data to config
$result = db_update('field_config')
  ->fields(array(
    'type' => 'file',
    'module' => 'file',
  ))
  ->condition('field_name', $field)
  ->execute();
	;
}
dave reid’s picture

Status: Active » Closed (duplicate)

Realizing we have two issues open for the same thing, so I need to mark this as a duplicate of #1349058: Convert 'Media' fields to 'File' fields that use the 'Media file selector' widget. I'm going to cross-reference it to ensure we don't lose the code that's been provided here if it can be useful.

dave reid’s picture

Issue summary: View changes

Minor formatting changes