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.

Support from Acquia helps fund testing for Drupal Acquia logo

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

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

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

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
Andy_D’s picture

Status: Needs review » Reviewed & tested by the community

Tested on 8.3.5 with Token 8.x-1.0 and confirmed working.

jongapul’s picture

I have a new patch with two changes from patch #10. It works for me.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 13: token-insert-text-2235581-13.patch, failed testing. View results

markhalliwell’s picture

Status: Needs work » Reviewed & tested by the community

Patch in #10 applies cleanly and works. Please commit, this is a very odd "bug" when one does what the alert says "please select a textbox".

Berdir’s picture

Status: Reviewed & tested by the community » Needs work

Looks like the patch is corrupt and can't be applied with git apply?

Martijn de Wit’s picture

@Berdir Which path did you try/use ? #10 or #13 ??

I used #10 and worked fine by using composer.

markhalliwell’s picture

Status: Needs work » Reviewed & tested by the community

Use #10.

  • Berdir committed e6fcd1e on 8.x-1.x authored by polynya
    Issue #2235581 by quicksketch, polynya, jongapul, thmnhat: Make Token...
Berdir’s picture

Status: Reviewed & tested by the community » Fixed

Fair enough, committed.

Thib’s picture

Hi,
Thanks for this feature !!
But could you explain how it works.
I disable Token Filter module and my token, in block textarea, is no longer supported.
Thanks

Berdir’s picture

Category: Feature request » Bug report

This is not really a feature, it's a bugfix, so changing status.

This does not do token replacements, all it does is allow to insert tokens by clicking on them. You still need token filter if that textfield is formatted text.

Thib’s picture

Thanks you for the clarification

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

michel.g’s picture

Version: 8.x-1.x-dev » 7.x-1.x-dev
Issue tags: +Needs backport to D7

Patch for Drupal 7 has never been committed.

optimusprime619’s picture

Requesting clarification on one minor aspect regarding the js change, and apologies in case I have misinterpreted something here.

The 8.x-1.1 branch of token module's js file does not seem to have the properties drupalSettings.tokenFocusedField.tokenHasFocus and drupalSettings.tokenFocusedField.tokenDialogFocus which I believe were defined in earlier iteration of the js, Refer the patch in #4 of this issue. In that case shouldn't one of if the condition if (drupalSettings.tokenFocusedField && (drupalSettings.tokenFocusedField.tokenDialogFocus || drupalSettings.tokenFocusedField.tokenHasFocus)) { which were part of the earlier behavior Drupal.behaviors.tokenDialog become obselete and removed?

optimusprime619’s picture

In addition to the above, there exists an issue in the webform module where click-to-insert feature was disabled owing to it not working for WYSIWYG editors which has been addressed here. However while the updated token.js works for tinyMCE and CKEditor in their js only or drupal based modes, the D8 webform module makes use of CodeMirror js library.

Attaching a patch of the changes I tried to get the codemirror click-to-insert working for review and feedback.

Note: Could not update the previous comment due to my workplace firewall restrictions and had to add a new comment for the same. Regret the inconvenience.

colan’s picture

#27: This issue is closed. Please open a new one, unless it exists already.