Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
In the Webform queue we regularly have requests to have a token that provides the value of the $_SERVER['HTTP_REFERER'] value. We used to have this ability before we switched to the official token system, but now there doesn't seem to be an equivalent using the normal token system. I couldn't find any modules that provide this token either, and given it's general-purpose nature I though it might be a good candidate for including directly in the the token.module.
Comments
Comment #1
acHuge +1 for this
Comment #2
Vacilando CreditAttribution: Vacilando commentedhttps://drupal.org/project/google_analytics_referrer provides a tag that displays a list of referrers for the given page. If just one referrer is needed, for the given page, the syntax is [gar||1].
If there is interest, a real token could be added — just add an request in the module's issue queue.
[EDIT] On a second thought, I realize that for the given use case the referer needs to be available immediately while the fetching from Google Analytics (in the GAR module) takes time.
Comment #3
liquidcms CreditAttribution: liquidcms commentedsomewhat related.. i posted this a while back: https://www.drupal.org/node/2010898#comment-9231971
Comment #4
dehacker CreditAttribution: dehacker commentedSupport for php variables %server[HTTP_REFERER] and %server[HTTP_USER_AGENT] were extremely useful in webform 3.
Some of our systems using Webform 3 can not be simply upgraded to Webform 4 because they rely on these variables for workflow in both helpdesk forms and applicant tracking. Webform 4 is dependent on the Token module to accomplish what server variables used to. As Webform is one of the most popular contrib modules it would be more robust if functionality were supported across Drupal 7 when updates are made to major modules with dependencies. Please add tokens for these server variables.
Comment #5
Chewits CreditAttribution: Chewits commentedI'm seeking for replacement of %server[HTTP_REFERER] in Webform too..
Comment #6
helmo CreditAttribution: helmo at Initfour websolutions commentedHere's a bit of code to add such tokens.... These two hooks can be added to your own custom module by replacing 'mytoken_custom' with your module name.
Comment #7
jiv_e CreditAttribution: jiv_e at LilDrop Consulting commentedI believe this solves the issue. Please reopen if you feel this is not the case!
Comment #8
bisonbleu CreditAttribution: bisonbleu commented@helmo, once the suggested code in #6 is implemented in a custom module, how can I access these new tokens in e.g. Rules or elsewhere ?
Comment #9
helmo CreditAttribution: helmo at Initfour websolutions commentedThat would be [server:HTTP_REFERER] and [server:HTTP_USER_AGENT]
Comment #10
tremor CreditAttribution: tremor commentedI have tried this from #6 but it doesn't seem to be working.. is there any reason why it's just returning nothing?
Comment #11
mitchtollerud CreditAttribution: mitchtollerud commented#6 works for me in webform results. Thanks for the solution.
Comment #12
Yuri CreditAttribution: Yuri commented#6 is working fine! Thanks!
Comment #13
HitbyHi, I've made a custom module referer_token containing the code above with the functions renamed
function referer_token_token_info() {
and
function referer_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
then placed [server:HTTP_REFERER] in the redirect field on the webform. When I complete the form though I'm just left on the form page. Will this code work with webform-7.x-4.16?
As an aside, my use case is using Webform Private File Gateway to forward links on a page to the webform to collect an email address. The user then receives an email with their download link and I'd like them to be redirected back to the page on which they clicked the download link.
Cheers!
Comment #14
kristindev CreditAttribution: kristindev as a volunteer commentedAnother up vote for #6
Comment #15
wlofgren CreditAttribution: wlofgren commentedI've made a custom module that adds the referrer url to be able to use in Rules.
page_referrer.info
page_referrer.module
Comment #16
pmoz CreditAttribution: pmoz as a volunteer commentedJust in case anyone else stumbles upon this.
Add and enable the custom module that @helmo was kind enough to share in #6.
[submission:values:referring_page:nolabel] and hit save.
Now when you view the form you should see the referring page in the disabled text filed. Disabling the field instead of hiding it makes it a bit easier to troubleshoot in the future.
You'll need to go through both steps for every form that you want to apply it to.
Overall I agree that this should be a part of webform but at least there is a workaround.
Comment #17
cguidog CreditAttribution: cguidog commentedHi @pmoz,
I followed your instructions and I am halfway there.
My form is located in an url that looks like this: https://example.com/category/article
After adding the module this is what I'm getting: https://example.com/category/
So basically the article slug is not being included.
Any ideas what could be missing?
Thanks,
Comment #18
jamesfk CreditAttribution: jamesfk at Website Express Ltd. commentedHi everyone,
Thanks, @helmo for the code, which worked perfectly.
We needed this functionality for a couple of clients, so made a quick module that might be easier for anyone else needing this:
https://www.drupal.org/project/referrer_token
All the best,
James