lpa

Howto change Textarea field to CKeditor field and keep formatting

Recommended Posts

I have some fields defined as basic Textarea fields with htmlspecialchars, Newlines to XHTML Line Breaks and Markdown/Parsedown Extra textformatters. The content is maintained as plain text with line breaks and some simple Markdown coding.

Now the editors would like to use CKeditor for those fields. What should I do to convert the current newlines and Markdown formatting to HTML formatting shown properly in CKeditor?

Share this post


Link to post
Share on other sites

If there are many such fields, consider changing the textarea values via API + regex. e.g. with https://gist.github.com/jbroadway/2836900

Just take care about when using / removing / adding which textformatters in the whole process.

Perhaps do a test with only a handful of fields first, and see if it works out OK?

  • Like 1

Share this post


Link to post
Share on other sites

For each field that you want to change to CKEditor, add a new CKEditor field to the template. Then use the API to set the formatted value of the textarea field to the CKEditor field. When you have done the API operations and checked that everything is okay you can delete the old textarea field.

See here:

 

  • Like 2

Share this post


Link to post
Share on other sites
37 minutes ago, Robin S said:

Then use the API to set the formatted value of the textarea field to the CKEditor field.

If you're not comfortable doing this manually, take a look at this action in the AdminActions module:

Copy Content to Other Field
This action copies the content from one field to another field on all pages that use the selected template.

This can be useful if you decide you need to split one field into two to allow different settings on different templates. It also makes it easy to move content from one field type to another one that is incompatible.

  • Like 2

Share this post


Link to post
Share on other sites

if you don't want to add the textformatter to the field, you can also do something like this..

$markdown = $this->wire('modules')->get("TextformatterMarkdownExtra");
$htmlVersion = $markdown->markdown($page->textarea_field,2);

 

  • Like 1

Share this post


Link to post
Share on other sites
2 hours ago, adrian said:

If you're not comfortable doing this manually, take a look at this action in the AdminActions module:

Copy Content to Other Field
This action copies the content from one field to another field on all pages that use the selected template.

This is a good suggestion, but the action code would need to be modified in order to work in this case. By default that action copies the unformatted value from one field to another, but in this case the formatted value is needed in order for the newlines/markdown to be converted to HTML.

  • Like 2

Share this post


Link to post
Share on other sites

So based on your suggestions the change in any case needs a conversion from Markdown to HTML by copying the content to another field and then using the new fields. Thanks for this. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.