Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#27 | clicktoinsert-codemirror.patch | 1.29 KB | optimusprime619 |
#10 | token-wysiwyg-2235581-10.patch | 3.96 KB | polynya |
#7 | token-click-insert-to-wysiwyd-2235581-7.patch | 7.22 KB | thmnhat |
#6 | token_wysiwyg-2235581.patch | 5.84 KB | quicksketch |
Comments
Comment #1
Dave ReidIsn't this a use case covered by Token filter and Token insert already?
Comment #2
Dave ReidComment #3
quicksketchHere'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.
Comment #4
quicksketchThere 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.
Comment #5
quicksketchAfter 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".
Comment #6
quicksketchAlthough 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.
Comment #7
thmnhat CreditAttribution: thmnhat commentedPlease 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.
Comment #8
John Franklin CreditAttribution: John Franklin commentedI've tried the patch in #6, and it seems to work.
Comment #9
jhedstromThis appears to still be an issue in 8.x.
Comment #10
polynya CreditAttribution: polynya at Eurostar commentedThe 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.
Comment #11
polynya CreditAttribution: polynya at Eurostar commentedComment #12
Andy_D CreditAttribution: Andy_D at Investis Digital commentedTested on 8.3.5 with Token 8.x-1.0 and confirmed working.
Comment #13
jongapul CreditAttribution: jongapul commentedI have a new patch with two changes from patch #10. It works for me.
Comment #15
markhalliwellPatch 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".
Comment #16
BerdirLooks like the patch is corrupt and can't be applied with git apply?
Comment #17
Martijn de Wit@Berdir Which path did you try/use ? #10 or #13 ??
I used #10 and worked fine by using composer.
Comment #18
markhalliwellUse #10.
Comment #20
BerdirFair enough, committed.
Comment #21
ThibHi,
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
Comment #22
BerdirThis 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.
Comment #23
ThibThanks you for the clarification
Comment #25
michel.g CreditAttribution: michel.g at Randstad Digital for Government of Flanders commentedPatch for Drupal 7 has never been committed.
Comment #26
optimusprime619 CreditAttribution: optimusprime619 commentedRequesting 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
anddrupalSettings.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 conditionif (drupalSettings.tokenFocusedField && (drupalSettings.tokenFocusedField.tokenDialogFocus || drupalSettings.tokenFocusedField.tokenHasFocus)) {
which were part of the earlier behaviorDrupal.behaviors.tokenDialog
become obselete and removed?Comment #27
optimusprime619 CreditAttribution: optimusprime619 commentedIn 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.
Comment #28
colan#27: This issue is closed. Please open a new one, unless it exists already.