I am updating nodes on my site using my new custom module, and I've run into something annoying. Almost all these nodes have a body field that was originally either hand-coded or with some old Drupal WYSIWYG module that saved line breaks as <br> tags. Their format now has CKE5 enabled and the display (full node display) runs all the paragraphs together even though the break tags are in there.

If I manually open a node, I can see that there is a banner asking me to click a link to enable CKE5, but even then, the paragraphs do not display properly. E.g.

Looking at the HTML source window, it is clear that the new <p> tags are wrapped around the whole thing.

<p>
   I am updating nodes on my site using my new custom module, and I've run into something annoying. Almost all these nodes have a body field that was originally either hand-coded or with some old Drupal WYSIWYG module that saved line breaks as <code class="php">&lt;br&gt;</code> tags. Their format now has CKE5 enabled and the display (full node display) runs all the paragraphs together even though the break tags are in there.<br>
   <br>
   <a href="https://www.authorssite.com/single_product.cfm?product_id=13">See and hear samples on the author's Website</a><br>
   <br>
   &nbsp;
</p>

The strange thing is that visually, it looks correct in the CKE5 window:

So, is there a way for me to fix this from code? Two things to be done:

  1. Making the output respect the <br> tags. Or converting them into <p> tags, or something ...
  2. Enabling CKE5 for the field ... though maybe this is not essential. Idk.

I don't fancy manually editing a couple thousand nodes....

Accepted answer

With the help of the Backdrop office hours crew, I was able to figure out a solution that works for me.

I just added an explicit format setting to the long-text field before saving it, something like this:

// set $body_value to updated value replacing <br> tags with <p> tags
$wrapper->body = array('value' => $body_value, 'format'=>1, 'safe_value' => '');
$node->save();

... and now it works! (CKE5 will have to be enabled when someone goes in to edit it manually, but the display is correct.)

Comments

I found a (sort of) solution: explicitly add <br> to the list of allowed tags in the "Limit allowed HTML tags" filter settings.

But ultimately I would like to do a cleaner solution and convert the old <br> tags over to <p> tags using code. I love regexps so it's no problem to massage the HTML to what I want; however, I then get the <p> tags showing on the node display, even though (of course) those tags are allowed in the format.

Example:

But once I open the node for edit and enable CKE5 and re-save, it works fine.

So how does it know that CKE isn't enabled? And is there a way to do it without having to manually open and save each node?

Thanks.

With the help of the Backdrop office hours crew, I was able to figure out a solution that works for me.

I just added an explicit format setting to the long-text field before saving it, something like this:

// set $body_value to updated value replacing <br> tags with <p> tags
$wrapper->body = array('value' => $body_value, 'format'=>1, 'safe_value' => '');
$node->save();

... and now it works! (CKE5 will have to be enabled when someone goes in to edit it manually, but the display is correct.)