This project is not covered by Drupal’s security advisory policy.

This a utility/developer-only module for converting fields from one type to another. E.g., for changing plain text (textarea) fields to filtered text (WYSIWYG), or short text fields to long text. Existing field data is preserved as long as the new field type schema has columns with the same names as the old schema (e.g., "value").

This module is based on the approach outlined in this documentation.

Warnings

Supported field types

The script does not take care of differences in column names between the old field schema and the new, so there is potential for data loss depending on the field types being converted from → to. More info regarding the handling of database schema:

  • Columns that exist in both old and new field schemas are preserved.
  • New columns that didn't exist before are created.
  • Old columns that don't exist in the new schema are simply deleted.

Large databases

On very large databases or brittle infrastructure, the database queries to change table columns can timeout, resulting in data loss. Be sure to test this in a staging environment first, and back-up the production database before executing the script. For a low-risk alternative, see the Plain Text as Formatted module.

Usage

Implement hook_post_update_NAME() and call FieldTypeConverter::processBatch(). E.g., to convert a plain-text node field named field_foo to long filtered text (i.e., WYSIWYG):

function hook_post_update_NAME(&$sandbox) {
  $field_map['node'] = [
    'field_foo' => 'text_long',
  ];
  return \Drupal\field_type_converter\FieldTypeConverter::processBatch($sandbox, $field_map);
}

Then:

  1. Execute the database updates (docs).
  2. Visit the "Manage form display" and "Manage display" tabs for the affected entity bundles, and simply re-save the displays. This updates the configuration to match the new field type.
  3. Export the configuration changes (docs).
  4. Commit the changes to your code repository.

After deploying the new code to upstream environments, be sure to execute database updates before importing config.

Next steps

Consider adding a UI to support changing field types by site builders and other non-developers.

Supporting organizations: 
Sponsored development

Project information

Releases