Forum

Thread tagged as: CKEditor

Change to PerchForm.class.php that fixes CKEditor plugins

For years, I've been tearing my hair out trying to figure out why I couldn't get certain CKEditor plugins to work with Perch. Often I'm able to install the CKE plugin, use it to create content, save the content in Perch and view it successfully on the frontend, but then when I go back to edit the content in Perch it won't display correctly.

Today, I was having this problem with the Code Snippets plugin, and I finally found a solution. In /perch/core/lib/PerchForm.class.php line 910 (the textarea function), I changed $this->html($this->value($value)) to $this->html($this->value(htmlspecialchars($value))) and the plugin started working. Then I went back and tried other plugins that haven't worked before and they all work now.

Can this change be made to Perch core without messing anything else up? Everything is working fine as far as I can tell.

Shane Lenzen

Shane Lenzen 18 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You're double encoding the HTML entities there, which is doing to cause problems. You'd be better to implement your own field type than to hack at the core. Modifying Perch core leaves you without support or updates.

Right as usual, Drew. Turns out I just needed to add config.htmlEncodeOutput = true; to CKE config. Perhaps that switch should be added to the Perch CKE addon download?

Spoke too soon. That causes the HTML to be output to the page encoded.

Drew, I'm at a loss here. I've tried every combination of CKE settings I can find (allowedContent, htmlEncodeOutput, entities, basicEntities) as well as Perch flags (html=true, encode=false) but the only thing that works is editing Perch core as I did in OP. I've seen in several CKE forums that htmlspecialchars() is the solution. Are you saying I need to create my own fieldtype to be used specifically with CKE?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you saying I need to create my own fieldtype to be used specifically with CKE?

Yes, if you want to change how a field type works, create a new one. If you extend the original class you only need implement the methods you change.

Drew, I've been over the field type docs but I'm afraid some of the PHP terminology is over my head. Any chance you could point me in the right direction in terms of extending the textarea field type? All I need is to run htmlspecialchars() on the $value when CKE is loaded so I'll finally be able to use all these plugins that will make my life infinitely easier. Please...save me from clients who need inline YouTube videos. :)

If anyone could point me in the right direction, I would greatly appreciate it! I need to extend the textarea fieldtype to run htmlspecialchars() on the $value when CKE is loaded. Drew makes it sound like it should be easy, but I'm not familiar enough with PHP.