Andy
Andy

Reputation: 2140

CkEditor - how to parse html from database into formatting

I'm using Ckeditor version 3.1 within CakePHP 2.x

I can get the editor to load and replace the textarea. The HTML is saved to the database with HTML formatting tags like this:

<p>
<strong>&lt;p&gt; This is a test&lt;/p&gt; </strong></p>

When I insert that into the CkEditor I get the following appearing in the editor screen:

<p> <strong>&lt;p&gt; This is a test&lt;/p&gt; </strong></p>

If I use html_entity_decode prior to setting up the CkEditor I get this in the editor screen:

<p> <strong><p> This is a test</p> </strong></p>

This is how I am loading the editor in the edit.ctp Cake view:

$bio = $this->data['Club']['bio'];  
echo $this->Cksource->ckeditor('Club.bio', array('value'=>$bio) );

How can I force CkEditor to parse the incoming HTML so that it is formatted and not displaying the HTML tags?

Edit: I have manually overwritten the Cake Helper so that the Javascript being output looks like this:

CKEDITOR.replace('data[Club][bio]',
                    {
                        entities: false,
                        basicEntities: false,
                        entities_greek: false,
                        entities_latin: false,
                        htmlDecodeOutput: true
                    }
                );

The appropriate form field is being converted to a ckeditor instance so the first parameter is correct, I assume. I've also tried to update config.js without any luck.

Upvotes: 0

Views: 4063

Answers (2)

Andy
Andy

Reputation: 2140

Thanks Harry - you helped with the setup. I had to tweak the Cake stuff to get the Javascript looking like you said it should.

Anyway here's something that is working for me:

$bio = $this->data['Club']['bio'];  
$bio = html_entity_decode($bio);
$bio = preg_replace( '/\s+/', ' ', $bio );

$events['instanceReady'] = "function (ev) {
            console.log(ev.editor);
            ev.editor.setData( '$bio' );
        }";

echo $this->Cksource->ckeditor('Club.bio', array(
                    'value'=>$bio, 
                    'config'=>array(
                        'entities'=>false,
                        'basicEntities'=>false,
                        'entities_greek'=>false,
                        'entities_latin'=>false, 
                            'htmlDecodeOutput'=>false), 
                    'events'=>$events
                    ) 
                );

I would imagine it is not really necessary to put the value in when echoing out the editor.

I kept getting a Javascript error when trying to setData ("unexpected ILLEGAL tag") which is why I strip out excess whitespace - which fixes that issue.

Upvotes: 0

harry
harry

Reputation: 1522

    CKEDITOR.replace(elemId,
    {
        entities: false,
        basicEntities: false,
        entities_greek: false,
        entities_latin: false,
        htmlDecodeOutput:true,
    }
    );

Hope you have added this to your code. Else please try this.

Upvotes: 1

Related Questions