7

In Drupal 8, I set up a plain text field and now I have no way to change it back to rich text editing.

I could take the approach of just setting up a new field and copy the field data from the old one to the new one manually, but I have hundreds of records. What are some other solutions?

enter image description here

enter image description here

1 Answer 1

11

There is no official solution. String and text fields have different structure (text has an additional format column), you could try to convert it by hand, but you also need to update Drupal's internal information about the field and resulting database schema.

Your easiest option is probably to create a new field and then write a simple script that loads all nodes of that type and assigns the value of the old field to the new. If there are too many nodes to load for a single run, you'll need to split it up or do a batch.

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['type' => 'yourtype']);
foreach ($nodes as $node) {
  $node->newfield->value = $node->oldfield->value;
  $node->newfield->format = 'plain_text';
  $node->save();
} 
2
  • 'plain_text' rather than 'text_plain'
    – paul-m
    Commented Nov 6, 2018 at 19:30
  • Where can I use this script? In a hook ? Commented Mar 31, 2023 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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