1. Upgrading the module and converting image fields into file fields

Last updated on
7 July 2020

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

First, upgrade the Media module by replacing the code with the latest release from the 2.x branch. As described in the wiki, the File Entity module has now become an independent project and it is required by the Media 7.x-2.x.

Required module:

Deprecated:

  • File Entity module included in Media 7.x-1.x. Make sure to remove it from the Media module directory

Because of the way the module handles files between the two branches, the fields created by Media 7.x-1.x need to be converted to File Entity fields. Unfortunately neither the Media module nor File Entity module provide an upgrade path so you need to convert the fields somehow.

The Helper module makes the conversion relatively simple. After enabling the helper module, create a custom module where you execute the following method:

FieldChangeHelper::changeType('[field_name]', 'file');

where [field_name] is the name of a Media field which you want to convert to a file field. This can be executed in an update hook or any function of your choice that you will call to do the conversion.

Sample code:

function media_upgrade_convert_fields() {

  // Get a list of all fields
  $fields = field_info_field_map();

  // Obtain the machine name of all image fields and store them in an array
  foreach ($fields as $name=>$field) {
    if ($field['type'] == 'image') {
      $image_fields[$name] = $field;
    }
  }

  // 3. Loop through the list and convert them into file field
  if (!empty($image_fields)) {
    foreach ($image_fields as $name => $field) {
      $results[$name] = FieldChangeHelper::changeType($name, 'file');
    }
  }
}

Help improve this page

Page status: No known problems

You can: