Add a way to set the format when changing to long_text

Created on 11 April 2024, 6 months ago

Problem/Motivation

Steps to reproduce

After converting a text field to text_long, when inspecting the database you will find NULL values inside the field format column which will cause troubles when updating the content as you will need to select the format for each textearea.

Proposed resolution

For the moment, we added a new hook_post_update_NAME() to handle this by updating the database.

Here is the code used :

/**
 * Update field_caption format to restricted_html.
 */
function MODULE_post_update_caption_format(&$sandbox) {
  $field_map = [];
  $field_map['media'] = [
    'field_caption' => 'text_long',
  ];
  $field_map['paragraph'] = [
    'field_caption' => 'text_long',
  ];

  $database = \Drupal::database();

  $field_map = [];
  $field_map['media'] = [
    'field_caption' => 'text_long',
  ];
  $field_map['paragraph'] = [
    'field_caption' => 'text_long',
  ];

  $updated = '';
  foreach($field_map as $entity_type => $fields) {
    foreach($fields as $field_name => $field_type) {
      $num_updated = $database->update("{$entity_type}__{$field_name}")
        ->fields([
          'field_caption_format' => 'restricted_html',
        ])
        ->execute();
        $updated .= "{$entity_type}__{$field_name}: ". $num_updated ." fields updated.\n";

      // Update revision table too.
      $num_updated = $database->update("{$entity_type}_revision__{$field_name}")
        ->fields([
          'field_caption_format' => 'restricted_html',
        ])
        ->execute();
        $updated .= "{$entity_type}_revision__{$field_name}: ". $num_updated ." fields updated.\n";
    }
  }

  return $updated;
}

Remaining tasks

Implement this in FieldTypeConverter class.

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇫🇷France tostinni

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024