If you have a plain text field in Drupal that uses the wysiwyg (ckeditor) widget, no text will appear and when you attempt to save the text entered in ckeditor will not save.

This is because of how CKEDITOR.replace() works. it will use the innerHTML() of any DOM element, http://docs.ckeditor.com/#!/api/CKEDITOR-method-replace. To get around this it might be possible to clone the original textfield before calling CKEDITOR.replace, replacing the textfield with a textarea. Then when the CKEDITOR instance is destroyed, the original textfield may replace the textarea that was created.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

svajlenka’s picture

Version: 7.x-2.2 » 7.x-2.x-dev
FileSize
1.48 KB

This also affects 2.x-dev, I'm also supplying a patch made against dev.

svajlenka’s picture

Status: Active » Needs review
TwoD’s picture

Component: Code » Editor - CKEditor

Thanks for the patch!
I've been meaning to write something like this for a long time, see #971632: CKeditor breaks on text field (not on field type Long Text) but I've just not gotten around to it.

I see one potential problem with this specific implementation though. What happens if another script has references to the original field or attaches events to that element? Since it's being cloned and replaced, I'm assuming those events would get lost.

What if we instead just hide the original field and create the new textarea next to it, then sync contents to the textfield after detaching?
That'll keep all references/events intact (and things like onchange should trigger when the contents do change).
Do we need to consider newlines? A quick experiment seems to indicate that newlines will simply be ignored when put into a textfield.

Maybe we should make it so that the editor implementations do not have to worry about this at all and always see textareas by moving this into wysiwyg.js (maybe into Drupal.wysiwyg[De|At]tach()). That would take care of FCKeditor as well (yes, I think some are still using it.) I don't remember if all other supported editors work with textfields. If they do, maybe just copy this change to FCKEditor...

jojonaloha’s picture

The patch in #1 fixed the issue for me using Wysiwyg 7.x-2.2.

sadashiv’s picture

#1 worked for me.

Thanks,
Sadashiv.

blake.thompson’s picture

The patch in #1 kept the data when changing text formats but didn't save the changes when the input was a textarea. Updated patch fixed that issue for me.

VeniaminLopatin’s picture

The problem is still exists in 7.x-2.3 version.
Is here any solution for that?

Stefan Lehmann’s picture

I just reapplied the patch from #7 to the 7.x-2.3 version and it still seems to work, when put into the correct place. Might fix the issue for people until somebody comes around with a real fix.

JamesOakley’s picture

I'd gladly submit an amended patch, but I don't understand how this particular module works. So I'll report the problem and someone else can comment on how easy it would be to fix.

The patch at #7 fixes the problem on a test site I tried it on. However, the ckeditor text area that was created was collapsed into minimum height, so that (even though a text field had content in it), it still looked empty when the rich text editor was enabled. I therefore almost thought the patch hadn't worked, until I dragged the resize handle down, then I could see the content in the editor.

Is there any way to set the starting height for the rich text editor to be one line high?

TwoD’s picture

I'm not very happy about replacing the original field completely because we may destroy event handlers attached to it, and we'll have no way to add them back.

I think if we on attach instead hide the original field, insert a textarea with some unique id, and then attach the editor to that.
On detach we detach the editor, sync the contents to the original field, and then remove the textarea.
One issue is that the editor will need to attach to a different id than what the client has been passed settings for so that part will be out of sync. "Outwards" will also have to give the appearance of attaching to the original textfield using the original id, or 3rd party code will not find the correct editor instance.

Stefan Lehmann’s picture

@JamesOakley:

Is there any way to set the starting height for the rich text editor to be one line high?

You could probably try to add a style attribute on the textarea with a minimum height value or so ..

@TwoD:
Yeah I thought so too. However it's an interim solution for people who have clients strangling their necks. :-) Before that issue I didn't even know that one can assign a WYSIWYG to a normal textfield.

sgalindo2388’s picture

Removed '.txt' from file name to be able to apply the patch using drush make.

Sardis’s picture

What should one do if he uses 4.6.2.20af917 version? I've applied the patch, but it doesn't do any good.
Much appreciate the help.

dlriley’s picture

I've made another patch from #12 that works with CKEditor 4.9.2

wesruv’s picture

Using "4.6.2" #14 works great for me!

nplowman’s picture

Attaching a re-rolled patch that will work with the latest version (7.x-2.5).

gsquirrel’s picture

Worked well for me

steinmb’s picture

Status: Needs review » Needs work

Cannot find comment from the maintainer in #10 addressed in the latest patches, back to NW.