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.
On a barebones site, I'm trying to get a hidden field to be populated by the NID. I think I should be able to use the node token %nid in the field to do this automatically, but when I do it I just get a literal '%nid' in the form submission.
I've worked around this using URL arguments - now I have links that point to node/78?node=78 . I have no problem pulling that out with get[node] .
In testing this, I found I also get a literal submission using %title , so it seems to be just Node tokens affected.
Comment | File | Size | Author |
---|---|---|---|
#33 | webform_filter_node-d6.patch | 16.71 KB | quicksketch |
#33 | webform_filter_node-d7.patch | 16.97 KB | quicksketch |
#16 | webform-nodetokens-1207374-16.patch | 15.19 KB | imclean |
#9 | nodetokens-828566-9.patch | 2.02 KB | imclean |
#7 | nodetokens-828566-7.patch | 2.01 KB | imclean |
Comments
Comment #1
MurzExpiring same problem and can't find the solution, subscribe.
Comment #2
choosewisely CreditAttribution: choosewisely commentedI am experiencing exactly the same issue. Subscribing.
Comment #3
P.Selfin CreditAttribution: P.Selfin commentedSubscribing
Comment #4
sapox CreditAttribution: sapox commentedI need too the %nid token, but it is not working.
Comment #5
jibranSubscribing
Comment #6
ecksley CreditAttribution: ecksley commentedSubscribing... noting this is also an issue for 7.
Comment #7
imclean CreditAttribution: imclean commentedThe node object needs to be passed to _webform_filter_values() for these to work. Looking at the render functions for the component types hidden and textfield this doesn't seem to be happening. Here's a patch to fix it for those types by simply loading the node again and passing it on.
I wonder if a better way would be to pass the node object from _webform_client_form_add_component() to the component's render function somehow, although this may change the API.
Comment #8
imclean CreditAttribution: imclean commentedThis only provides the webform node object itself. If you're using webform in a block it may not be so helpful.
Comment #9
imclean CreditAttribution: imclean commentedTry this instead, it grabs the nid from the URL so works with blocks. Should components be providing this information?
Comment #10
quicksketchThe first patch is "correct", the node tokens are not intended to reflect whatever node page you are on.
Comment #11
elgandoz CreditAttribution: elgandoz commentedsubbing.
the node tokens are not intended to reflect whatever node page you are on.
I do not understand this. It's extremely useful use the tokens in that way (provide the current node).
It would be extremely easy in this way put the classic "product name" in the "more information form", usually a block.
Why don't provide this behaviour by default?
Comment #12
quicksketchTokens are always intended to reflect the node on which they are used. I agree that having "current node" tokens may be useful, but that's not the expected or typical behavior of tokens.
Comment #13
quicksketchUpdating title. Node tokens do work in other places (such as in e-mails).
Comment #14
jaymallison CreditAttribution: jaymallison commentedActually, they don't work. %title won't render in emails on 7.x-3.12 (I was sent here from the 7.x issue since it was closed)
Comment #15
koppie CreditAttribution: koppie commentedI had the same problem. I have a webform in a block and I need it to pull an email address from a View and mail to that address. I'm using Webform PHP, in spite of the developer's grumbling, because it really is the most expeditious way to do what I need to do. In terms of it being a bad idea to expose raw PHP on a Drupal site, that horse left the barn a long time ago (at least on my site). The real key is only giving access to trustworthy users.
Anyway I'm using Webform PHP to load the View, get the email address, and send the email. Webform still has trouble getting the NID when it's embedded as a block, so I created a hidden Webform field and used javascript to populate it with the nid.
Hope that helps.
Comment #16
imclean CreditAttribution: imclean commentedPatch based on #7 for all components where it makes sense to me. This also includes calls to _webform_filter_descriptions() which I missed the first time round.
I haven't tested but it looks ok.
Comment #17
imclean CreditAttribution: imclean commentedAlternatively, something like this:
Comment #18
eflage CreditAttribution: eflage commentedJust tried the patch from #9, it worked beautifully for what I'm trying to accomplish. Thanks!
Comment #19
jfine CreditAttribution: jfine commentedJust ran into this issue myself. I am using a Webform block and trying to pass URL (or similar) in a hidden field since the block is on multiple pages and we're interested in seeing which page the user submitted the form from.
Comment #20
jfine CreditAttribution: jfine commentedI understand limiting access to %server due to caching of GET pages though there are a few %server variables that would be handy to have and shouldn't cause an issue with cached pages. For instance in my case %server[url].
For those looking for an immediate solution, you can simply use jQuery to set the value of your hidden field.
Comment #21
filsterjisah CreditAttribution: filsterjisah commented@patchers, better use menu_get_object() to load current node object instead of checking args
Comment #22
imclean CreditAttribution: imclean commentedOr better yet, use neither. See #10.
This would be good to get resolved properly. I'm leaning towards the method in #17 to use the existing node object, it'd be nice to get some feedback.
Comment #23
AnybodyI'm having the same issue and it would be great if this could be fixed generally. Is there still work in progress to get this into the next release perhaps?
Comment #24
micke793 CreditAttribution: micke793 commentedDo you have any patch for default value like: %profile[field_XXX] who work in D7 too? :-)
Comment #25
quicksketchI've marked #1224130: %title token in Webform duplicate, but it contains a lot of other helpful posts also.
Comment #26
Marko B CreditAttribution: Marko B commentedI would like to use cck fileds as tokens. I want to have cck field tokens from current node so i can access it in webform block. I have an email address in node cck file and want to use it in webform block form, should i just use webform php for this? i tried pateched 9 and 16 but they didnt help or maybe i am accessing it wrong tried "%node[field_email]" ?
Comment #27
quicksketchSee these other issues:
#1001798: Rewrite token replacement system to use D7 tokens
#428982: New hooks for additional token replacements
Comment #28
Marko B CreditAttribution: Marko B commentedthanx quick
Comment #29
nodecode CreditAttribution: nodecode commentedWhat needs to happen for this issue to move forward and get a patch committed? I need this for D7.
Comment #30
quicksketchimclean's #16 looks really good. There are a couple minor spacing issues (a space after commas), but really I can take care of that when I apply the patch. My concerned are around the number of extra calls to node_load() (once per field), but since that is static cached, I don't think it should really be that bad of an issue.
This patch is necessary even if #1001798: Rewrite token replacement system to use D7 tokens is completed, so I don' think I can delay the requirement of it. I'm just a bit leery about it because loading the node instead of passing it in as a parameter doesn't seem "right", but it will clearly work. Without breaking the existing APIs of the 3.x version I think this is probably about as good a solution as we can get.
So in short, I think this is good to go. Further testing is always appreciated by anyone who is interested in this before (or after) it's committed.
Comment #31
imclean CreditAttribution: imclean commentedConsidering the tokens don't currently work, would my suggestion in #17 break the API in any meaningful way? This passes the node as a parameter within the $component array.
Comment #32
quicksketchNo that wouldn't cause any API breakage immediately. However that gets us into an uncomfortable position where $component sometimes contains some stuff that is there some of the time and not others. Right now a $component is a $component, and it's always the same everywhere (thought as this patch demonstrates, sometime things are NULL, like 'nid' when you're creating a new node). Entire properties being there some of the time is prone to error. So even though the approach in #17 would also work, I'd prefer #16.
Comment #33
quicksketchThe patches from #16 didn't apply very cleanly to the 3.16 version, since we rewrote the file component for Drupal 7, turned the "email" field into an HTML5 field, and introduced the "number" component. These patches apply the changes to all the new components and ports the patch to both versions.
Comment #34
quicksketchCommitted to both branches. Thanks @imclean for the patches (and feedback)!
I still have every intention of finishing up #1001798: Rewrite token replacement system to use D7 tokens, which complements this issue (so you could use any node property you liked, rather than just %nid and %title). It may also be able to solve people's requests for "the current node" tokens, which token module could at least theoretically provide, even if it's not available by default.
Comment #35
imclean CreditAttribution: imclean commentedAh yes, it probably isn't a good idea to hijack it for other purposes. Good to see this issue resolved!