There are a lot of situations where tokens are supported within WYSIWYG-enabled fields. Unfortunately, Token's current inserting support doesn't work with WYSIWYGs, it only works with plain textfields and textareas. I'd suggest that we adapt the code from Insert module to Token, since it's already solved this problem as well.

Files: 
CommentFileSizeAuthor
#10 token-wysiwyg-2235581-10.patch3.96 KBpolynya
#7 token-click-insert-to-wysiwyd-2235581-7.patch7.22 KBthmnhat
PASSED: [[SimpleTest]]: [MySQL] 347 pass(es). View
#6 token_wysiwyg-2235581.patch5.84 KBquicksketch
PASSED: [[SimpleTest]]: [MySQL] 347 pass(es). View

Comments

Dave Reid’s picture

Isn't this a use case covered by Token filter and Token insert already?

Dave Reid’s picture

Category: Task » Feature request
Status: Active » Postponed (maintainer needs more info)
quicksketch’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
5.81 KB
PASSED: [[SimpleTest]]: [MySQL] 347 pass(es). View

Here's a patch that adopts Insert module's approach for Token (slightly updated to reflect current WYSIWYG situation, we don't need FCKEditor support anymore). It acts the same as before for textfields and textareas, adding in support for WYSIWYGs.

The task of keeping track of which field is active is slightly more difficult, since both WYSIWYGs and Token module will keep track of their "active" field. To help prevent inserting into the wrong field, textfields/areas are tracked separately from WYSIWYGs. If a normal field has focus when a dialog is opened, it keeps focus unless another textfield is clicked on (just like today). However if a textfield does not have focus (likely because the cursor is in a WYSIWYG instead), the last active WYSIWYG gets inserted content. This approach is the only thing I could think of that was less elaborate than implementing blur/focus handlers for all WYSIWYG libraries as well.

quicksketch’s picture

Isn't this a use case covered by Token filter and Token insert already?

There are a lot of situations where tokens may be used but aren't necessarily enabled by Token Filter. i.e. In Webform we have confirmation messages and markup fields that support tokens, so the replacement is done after the normal filtering system.

Regarding Token Insert... That is similar to what Token module provides already, but it just works specifically for WYSIWYGs. I suppose it's a question of what you consider appropriate for Token module. Most sites have Token module, most sites have WYSIWYGs. Very few sites comparatively go through the hassle of making a WYSIWYG-specific configuration for tokens. Additionally, it looks like Token Insert module uses a completely different UI from Token's built-in dialogs. For the sake of consistency, I wouldn't use the approach provided by Token Insert even I wanted to take the time to set it up.

quicksketch’s picture

After having looked at Token Insert a little more, I'd also say that Token module already provides 50% of what Token Insert module does. I imagine Token Insert was created before Token module started providing its own JS inserting mechanism, and was further deprecated by the introduction of the Token dialog. Now all of Token Insert's plain textfield handling is obsolete, and only the WYSIWYG component remains as something valuable. Rather than looking at this from the perspective of "another module already does this", I think it'd make sense to approach it from "let's make it so no one needs another module".

quicksketch’s picture

FileSize
5.84 KB
PASSED: [[SimpleTest]]: [MySQL] 347 pass(es). View

Although I don't know if the first patch is even acceptable or not, here's one that doesn't throw JS errors if a field hasn't yet been clicked in when the dialog opens.

thmnhat’s picture

FileSize
7.22 KB
PASSED: [[SimpleTest]]: [MySQL] 347 pass(es). View

Please review my patch. It supports CKEditor 3.5+ and TinyMCE 3.x. It doesn't require CKEditor or WYSIWYG module, just pure JS.

The original of this patch comes from https://drupal.org/project/advanced_text_formatter which needs inserting token to wysiwyg editors.

John Franklin’s picture

I've tried the patch in #6, and it seems to work.

jhedstrom’s picture

Version: 7.x-1.5 » 8.x-1.x-dev
Status: Needs review » Needs work

This appears to still be an issue in 8.x.

polynya’s picture

The previous patches are 3 years old and do not apply correctly. I have created a new patch by taking the essential lines from patch #6. It includes code for TinyMCE but I have only tested it with the core CKEditor.

polynya’s picture

Status: Needs work » Needs review