Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
3.8k views
in Q2A Core by
When writing a filter plugin to filter line breaks I stumbled over the format of content. See qa_posts, field format. It is either '' or 'html'.

Everywhere on my forum I am using CKEditor, even for comments. However, text is sometimes not saved as 'html' but empty. I can see the pure text in 'content' field. Having no html, content is parsed with nl2br by q2a.

How comes that the content is sometimes saved as TEXT?

Shouldn't it be always HTML as we use CKEditor?
Q2A version: 1.5.4
by
Same for this post. I was writing with CKEditor, however, it seems to be saved as only text. You see it in the source code. All line breaks are <br /> and no extra <p> tags, that are normally set by CKEditor for new lines.

2 Answers

+1 vote
by

If there's no formatting added by CKEditor, it's saved as plain text. See function read_post(...) in qa-plugin/wysiwyg-editor/qa-wysiwyg-editor.php for the logic applied.

by
Thanks. But how comes that some users with mobile devices (where CKEditor does not work and textarea is shown) enter text which is saved with 'html'? ... I will ask my users again for better testing.
by
How weird, if I click on "hide" (to hide the post) the correct line breaks are shown. And if I re-show the post, the wrong text (without linebreaks) appears again.

Do you have any idea where could be the problem / bug?

PS: In function read_post() I changed one line so that for mobiles the "html" post gets saved as text only:
// new: if mobile then format=''
$formatsave = qa_is_mobile_probably() ? '' : 'html';
return array(
    'format' => '',
    'content' => $viewer->get_text($html, $formatsave, array())
);

So it stores the line break, but does not show it frontend? ... still investigating.
by
edited by
EDIT: It is indeed strange, when the post is hidden, the line breaks are converted to <br />. If the post is not hidden, the line breaks don't show up. In database both are always $format = '' (plain text)
by
Okay, I finally found out what is going on:

1. With my hack above, the posted text (by mobile) is saved with line-breaks as plain text.
2. When checking the post in my browser, I saw a one liner.
3. Problem was: CKEditor4 is loaded in the new "inline editing" mode and parses the content as HTML. So that's why I got no linebreaks.
4. Loading the same post as anonymous visitor shows me the correct line breaks.

Done! :)
0 votes
by

Solution:

1. Open qa-wysiwyg-editor.php and change appropriate lines to:

            else { // convert to text
                $viewer=qa_load_module('viewer', '');

                // hack: changed format so that posts with mobiles (no ckeditor, plain text) will keep linebreaks
                $formatsave = qa_is_mobile_probably() ? '' : 'html';

                
                return array(
                    'format' => '',
                    'content' => $viewer->get_text($html, $formatsave, array())
                );
            }
 

by
Note: This solution is problematic because - I *guess* - an iPad is identified by qa_is_mobile_probably() as mobile, but it displays the CKEditor, so the HTML inserted is displayed as text. Still there is no clean solution.

I really hope that the team around CKEditor can release an editor that works on *all* Android phones.
...