I am trying to resave a number of nodes to apply the text format changes to the body field. I’ve written a drush command to do this. The command does update the text format, but the text in the body field remains unprocessed.

public function saveNode() {
    $result = $this->entityTypeManager->getStorage('node');
    foreach ($result as $node) {

If I manually edit and save the node the text in the body field is correctly processed, but not when saved programmatically.

Notes: By "unprocessed" I mean the body field is still showing faulty HTML. If I manually edit and save the node the faulty HTML is fixed.

  • What do you mean by "unprocessed"? Text field value is not processed until the field is rendered, so if you are doing something unusual like saving rendered field output, you need to render the field first. Commented Aug 10, 2022 at 15:18
  • Changing the text format won't update the text in the database, if that's what you're expecting. Filters are applied on render, not for storage
    – Clive
    Commented Aug 10, 2022 at 15:19
  • For an example of applying the renderer, see this answer: drupal.stackexchange.com/a/293716/5147 Commented Aug 10, 2022 at 15:20

2 Answers 2


I’m not sure that I understand what you code is doing. Something like this might work.

$html_format = 'full_html';
$type = 'some_type';

$nodes = \Drupal::entityTypeManager()
                'type' => $type,

foreach ($nodes as $node) {

    $current_body_data = $node->get('body')->getValue();
    $current_body_value = $current_body_data[0]['value'];

    $new_body_data = [
            'value' => $current_body_value,
            'format' => $html_format,
    $node->set('body', $new_body_data);



Above solutions give errors in drupal 9 / 10, If you want to achieve this then do the following in a update hook.

$node->set('body', $node->get('body')->value); // Set the new value or existing value here.
$node->body->format = 'full_html'; // Set your format here.
$node->save(); // Save the node.

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.