I have a Webform that appears as a block on various node pages. When a user submits the Webform I want a hidden field to be automatically populated with the title of the current node to which the Webform block is attached. I tried using the %title token in my Webform's hidden field. Unfortunately this returns the Webform's which seems illogical. Shouldn't the title returned be coming from the current node and not the block?

How can I achieve this simple task?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nodecode’s picture

Title: Getting tokens from context with Webform in a block » Getting node tokens into Webform block
ambientdrup’s picture

I'd like to do this as well. See my similar issue post here:

http://drupal.org/node/1498650

-Trevor

quicksketch’s picture

@ambientdrup: Thanks for finding this issue. I've marked your other issue duplicate, since it's the same as this one.

@nodecode: This currently isn't possible. #1001798: Rewrite token replacement system to use D7 tokens would make this possible through something like [current-page:title]

ambientdrup’s picture

Is this possible in Drupal 6? That's why I opened the other issue since my specific question is for Drupal 6.

-Trevor

quicksketch’s picture

@ambientdrup: The only way you can do that in Drupal 6 is to use hook_form_alter() to set a default value on the form. That approach would work for D7 right now also. Drupal 6 will probably never get that support as a built-in option.

ambientdrup’s picture

Not sure exactly what you mean by setting a default value on the form?

So you mean programming in the default value as a token via hook_form_alter() correct? But I'm still not clear on how you'd do this to pull in the node that the block is embedded in? Sorry if I'm being dense here but I'm just not following.

Best-

Trevor

quicksketch’s picture

Well since I couldn't find an answer by searching or Googling either, here's a snippet that can be used in a custom module to set a default value based on the current node if the webform is in a block. This approach requires you have a custom module named "mymodule" and that your webform NID is 123456 and that you have a hidden field on the form with a form key of "my_hidden_field". Use the devel module if needed and use dsm($form) to inspect your form and form_id if needed.

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'webform_client_form_123456') {
    if ($node = menu_get_object()) {
      $form['submitted']['my_hidden_field']['#value'] = $node->title;
    }
  }
}
ambientdrup’s picture

This custom module works but it's still not doing what I need - I think there's still some general misunderstanding as to what the goal is here. I don't want to call in the node title of the embedded Web form. I want to call in the node title of the node that the Webform block is embedded in. So when someone is on node/142 for example and I have my Webform embedded on node/142 (but the webform is actually node/214) when the form is submitted I want the node/142 to be called into the hidden field as the value. This allows us to know which node the person was on when they submitted the form. We don't need to know the Webform node id. We need to know the node id of the node the form was embedded into. So I have this code as you explain thoroughly in #7 working but it's still not doing exactly what we need. here's my code per your #7:

<?php
function webform_ref_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'webform_client_form_214') {
    if ($node = menu_get_object()) {
      $form['submitted']['node_referenced']['#value'] = $node->title;
    }
  }
}
?>

So how can we swap in the value of the node that the form is embedded in? In the code above it's passing in the title of the Webform node. We don't want that. we want the title of the node that the Webform block is embedded in to be passed in as the value.

Best-

Trevor

ambientdrup’s picture

... and actually the custom module code per #7 already works by just using a %title token in the hidden field in the Webform itself. So you don't really need a custom module for that behavior to work.

-Trevor

quicksketch’s picture

I don't want to call in the node title of the embedded Web form. I want to call in the node title of the node that the Webform block is embedded in.

I'm very confused now. The %title token gives you the Webform node title. The code I gave you above gives you the node title of the node whose page you are visiting and not the Webform node. But if the %title token gives you what you want, then that's great.

ambientdrup’s picture

You are correct. %title gives you the Webform node title.

However when I use the code you shared, I still get the Webform node title. I don't get the title of the node whose page you are visiting. That does not work.

Did you test this and does it work on your site?

Not sure what I'm doing wrong but this code (that follows) only prints out the title of the Webform, and not the title of the node that the Webform is embedded on.

<?php
function webform_ref_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'webform_client_form_214') {
    if ($node = menu_get_object()) {
      $form['submitted']['node_referenced']['#value'] = $node->title;
    }
  }
}
?>
ambientdrup’s picture

For this code to work, should the Webform be redirecting (after submission) back to the node that it was embedded on? Maybe that's the issue? Right now I just have it redirecting to its confirmation page.

-Trevor

ambientdrup’s picture

That doesn't work either. It's still showing the title of the Webform.

-Trevor

kevinquillen’s picture

Heres what I did which might help, since I wanted to embed a value based on the entity the form was submitted from (webform is in a block). The following field is a hidden webform field. I also had to expose it as a regular hidden field, instead of 'Secure'.


function mymodule_form_alter(&$form, &$form_state, $form_id) {
	if ($form_id == 'webform_client_form_12') {
		$entity = end(entity_load('your_entity', array(arg(1))));
		$form['submitted']['my_hidden_field']['#default_value'] = $entity->property;
		$form['submitted']['my_hidden_field']['#webform_component']['value'] = $entity->property;
	}
}

This worked for me. Be sure to replace '5' with your component ID, you can set the value to whatever you want from the entity, provided it loads one. This was the only way I could find to embed some data about the entity the webform block was located on. YMMV.

What I was looking for was %entity_id, instead of %nid, or some way of grabbing data from the viewed entity. I didn't see a quick way to do that. Are there alternatives?

bogdanru’s picture

#7 works fine, just don't forget to check 'Show all webform pages in block ' from block configuration.

ambientdrup’s picture

Awesome!! That was it. I did not have the "Show all Webform pages in block" checkbox checked. That's good to know and fixes the issue.

Thanks so much for pointing that out!

-Trevor

carro2007’s picture

I want to create "hidden" field with token "title".
But isnt works! I want to reicev title of product page by email.
What I can do with this bug? Why module is not work correctly?

ambientdrup’s picture

Refer to #7 and #16. They ended up working for me.

-Trevor

Olafski’s picture

Title: Getting node tokens into Webform block » Getting node tokens into Webform block or referenced Webform

Thank you for the helpful code in #7 to get a value of a current node title for a webform in a block.

I'm trying to use the code in a similar case: My D6-webform is not in a block on the current node, but it is embedded there as a rendered node reference. In this case, I can see the node title in the relevant (not hidden) field of the embedded form, so far it's perfect. But the node value is not sent: Instead of sendig the value of the current node, Webform sends the title of the webform. I guess, that Webform tries to send the value of the relevant field after submission, but in this moment there are only the values on the Redirect location.

If anyone has an idea, how to change the code and/or how to configure the Webform Redirect location settings for the described use case, I would be very glad to hear your proposals.

nodecode’s picture

Title: Getting node tokens into Webform block or referenced Webform » Getting node tokens into Webform block

I sincerely appreciate everyone's efforts but I'm having no luck with the combination of #7 and #16 at the moment. However, I feel i may be missing something important because i can't use dsm($form) from within my function to begin with.

@ambientdrup: Since you seem to be quite enthusiastic about #7/#16, can you post the ACTUAL module code you're using so i may determine whether or not i'm implementing it correctly? Thank you.

@Olafski: Please create a separate thread for your issue.

aerodub’s picture

Here is a possible workaround using rules module.
You can load from the argument in the url the nid,
then load the nid using rules
then populate webform using rules too.

Hope that helps

Olafski’s picture

@ nodecode: Proposal #7/#16 works for me (with Webform blocks*). I attach my custom module, which you may use or study, if you want. I tested it with Drupal 6.25 and Webform 6.x-3.17.

In addition to #7, this custom module populates the Webform not only with the current node title, but also with an CCK field. If you don't need this function, simply delete the relevant part of the code. See README.txt for other configuration instructions.

[*] Concerning a Webform as rendered node reference, I created a separate thread: #1528714: Getting current node values saved and sent by referenced Webform

pcworxnet’s picture

In d7 I managed to get these values using hidden fields with token values (remove the ""):

Page Reference:
- key "pageref"
- default value "%get[q]"
- (secure)

URL:
- key "url"
- default value "%server[REQUEST_URI]"
- (secure)

nodecode’s picture

@Olafski
thanks for posting your code. My module was nearly identical and i still cannot tell why it was not working. Either way, I got it working with your code. Tested with Drupal 7.12 core and Webform 7.x-3.x-dev. Thank you.

So this is solved for me right now. I'll leave this as an active issue, however, in case the maintainers care to implement this functionality in the actual module in some way.

quicksketch’s picture

Status: Active » Fixed

I'm glad you've found a solution. Long-term, this will be handled natively via Drupal tokens, which could pull the current page node and access any of its properties through #1001798: Rewrite token replacement system to use D7 tokens.

MrPeanut’s picture

@pcworxnet I'm trying to do the same as you. The field from the node that I want to capture is field_email (it will then get emailed to that field). I have a hidden field on my webform with the default value of %get[field_email].

Alternatively, I tried this code in the module from #7:

function webform_nodevalues_form_alter(&$form, $form_state, $form_id) {
  // 1. Webform ID
  if ($form_id == 'webform_client_form_237') {
    if ($node = menu_get_object()) {
      // 2. Webform field for the node title
	  $form['submitted']['title']['#value'] = $node->title;
	  // 3. Webform field for a CCK field
      $form['submitted']['email']['#value'] = $node->field_email[0]['value'];
MrPeanut’s picture

@quicksketch Is the code I posted in #26 valid for Drupal 7, or Drupal 6 only (it's from comment #7)?

EDIT: If anyone stumbles across this and needs the answer, here it is.

mlncn’s picture

Is it possible to add custom tokens with Webform's current token system ("is there a hook for that"), or does that too need #1001798: Rewrite token replacement system to use D7 tokens ?

UPDATE: Real need posted in http://drupal.org/node/1221756#comment-5923682
The patch in tokenizing webform is working.
and combining it with http://drupal.org/project/webform_default_fields meets our need.

Status: Fixed » Closed (fixed)

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

hockey2112’s picture

I set up the page reference node as pcworxnet specified in #23, but it returns the node ID of the correct page, instead of the name of the correct page. I don't have Clean URLs enabled yet... could that be the issue? Otherwise, is there any other way to pull in the correct page's title in lieu of %get[q] ?

What I am trying to do is have a content type of "Careers". Each one will be a job available at the company (there will only be a few here and there, so I thought something like Recruiter or Drupal Recruit would be overkill). I create a Career content item and a generic "Application" webform is then inserted onto the content item's page via node reference. When the person submits their application via that page, I'd like the hidden value to pass along the name of that content item.

Peacog’s picture

I'm trying to do something similar (use a node field in a webform that's displayed in a block on the node page) and I found a great solution here: http://drupal.org/node/1544044#comment-6362572. It's a custom module that makes page node tokens (as opposed to webform node tokens) available to the webform. It works with Webform 4.x.

hockey2112’s picture

I just installed that custom module, but it did not create any new tokens in the webform "edit component" token area. All I need are Basic, Node, and Special tokens. Am I looking in the wrong place?

Peacog’s picture

FileSize
34.47 KB

Are you using Webform 4.x? I've attached a screenshot of what you should see on the Edit component page.

On a related issue, the module is giving me intermittent error messages on admin pages like this:
Fatal error: __clone method called on non-object in xxx/sites/all/modules/custom/page_node_token/page_node_token.module on line 80.

I've changed to code slightly to counter this:

function page_node_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  $langcode = isset($options['language']) ? $options['language']->language : NULL;

  // Page-Node tokens
  if ($type == 'page-node') {
    // page node type
    $entity_type = 'node';
    
    if ($node = menu_get_object()) {
      $entity = $node;
      unset($entity->_field_view_prepared);

       ...

      // Remove the cloned object from memory.
      unset($entity);
    }
  }

  return $replacements;
}
hockey2112’s picture

Ahhh...... I am using Webform 7.x-3.18, so I guess that must be the issue, right? The custom module only works with Webform 7.x-4.0-alpha6, then?

jasom’s picture

I have killed few hour with looking for a way how to get node ID where the webform block is embedded into submition data in drupal 7.

Webform 7.3.x tokens:

  • Hidden filed with %get[q] - receive node url of webform (not url of node where webform block is embedded in).
  • Hidden filed with %nid (%title) - same result

I was trying it through regular block, block template with module invoke, views block.. still same result.

Webform 7.4.x tokens:

I don't remember which exactly, but also not working this way. I miss tokens such as [current-page-title], [current-page-url] and mostly [current-page-node-id].

This is my dream scenario:

  1. You create webform node with desired form (reservation of apartments) and set up it as block.
  2. Block will set up to show on nodetype apartment as reservation form.
  3. On submission it will log node-id of node which it was submitted from.
  4. In received email I will see node ID (title or url) of apartment which reservation was submitted from.
  5. Through views (views with argument node ID) I will be able to see block of submission history for this facility when logged in as admin.

It works on drupal 6

There is working solution for drupal 6 #22 - webform_nodevalues-6.x-1.dev_.zip. So I have downgrade entire site to drupal 6 (Olafski module didn't work for drupal 7 after soft edit).

Notes:

emilflatz’s picture

It works for me with #7 and #16 solution on Drupal 7.18 and Webform 3.18.

E.

kerrycurtain’s picture

Thank you all for your posts, they have been invaluable help. #7 & #16 work perfectly but for those who may be stuck please remember to activate the custom module "my module". Also for those that are new to this you will also need an .info file for the module.
That's it!

achauhan’s picture

This custom module will work when you will check 'Show all webform pages in block ' from block configuration.
By default multi-page webforms redirect to the node page for all pages after the first one. If checked, all pages will be shown in the block instead.

function webform_nodevalues_form_alter(&$form, $form_state, $form_id) {
    // 1. Webform ID
    if ($form_id == 'webform-client-form-5') {
        if ($node = menu_get_object()) { //print_r($node->title);exit;
            // 2. Webform field for the node title
            $form['submitted']['product_name']['#value'] = $node->title;
            // 3. Webform field for a CCK field
            $form['submitted']['my_webform_field_key']['#value'] = $node->my_cck_field[0]['value'];
        }
    }
}
svenA’s picture

Thanks for this module!

Works fine in D7 also but I can't send custom fields, just the node title.

Have a form with a hidden field, the key: "myxxx", and a custom text field in my node: "field_customfield"

This is the line. It returns: Undefined offset: 0 .............
$form['submitted']['myxxx']['#value'] = $node->field_customfield[0]['value'];

I have tried this too:
$form['submitted']['myxxx']['#value'] = $node->field_customfield;
No errors, no values in the email.

Suggestions?

svenA’s picture

I did it myself!
$form['submitted']['myxxx']['#value'] = $node->field_customfield['und'][0]['value'];

Works Great!

TravisJohnston’s picture

Issue summary: View changes

Trying this now with no luck. Newest version of Webform (since Webform Block is built into 7.3x)

Currently using the following code

function ces_custom_form_alter(&$form, $form_state, $form_id) {
	if ($form_id == 'webform_client_form_1531') {
	  if ($node = menu_get_object()) {
		$form['submitted']['email_to']['#value'] = $node->field_resume_contact['und'][0]['value'];
		$form['submitted']['position_title']['#value'] = $node->title;
	  }
	}
  }

I have a contact field in the node that needs to be used as the Email To field in the webform. I have the block set to load all webform pages, as suggested, though no errors. I added the ['und'][0]['value'] to the custom field based on what was mentioned in #40, but still nothing.

svenA’s picture

This function works for me.

Maybe ' ' round 0 is important.

function webform_nodevalues_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'webform_client_form_2') {
    if ($node = menu_get_object()) {
    	$form['submitted']['produkt']['#value'] = $node->title;
    	$form['submitted']['artikelnummer']['#value'] = $node->field_artikelnummer['und']['0']['value'];
    	$form['submitted']['pris']['#value'] = $node->field_pris['und']['0']['value'];
	}
  }
}
TravisJohnston’s picture

Tried still nothing. The email sends, just those 2 fields are not populated.

function ces_custom_form_alter(&$form, $form_state, $form_id) {
	if ($form_id == 'webform_client_form_1531') {
	  if ($node = menu_get_object()) {
		$form['submitted']['email_to']['#value'] = $node->field_resume_contact['und']['0']['value'];
		$form['submitted']['position_title']['#value'] = $node->title;	
	  }
	}
  }
TravisJohnston’s picture

I ended up trying out this : https://drupal.org/project/webform_node_value which worked great but it seems to only allow you to set it for 1 field. So I can get the email that I need from the parent node, but the title is still not available since I can't set it.

ytsejam’s picture

#7 worked for me, thanks!
I really wish there was a more friendly way to do this.

baxpace’s picture

This worked great! Only issue I ran into was that the submission values for the field I created are still taking the title of the webform, and not the node the block was added on. Does anyone know how to capture and store the title of the node and replace the default submission values with said title?

bisonbleu’s picture

My usecase was exactly as in #7: populate a hidden text field of the form with the title of the node the webform is embedded in (via an entityreference).

@quicksketch's solution is both simple and totally functional.

TravisJohnston’s picture

This still doesn't work for me, the code I am using is included in my comment in #43...

But I just had a thought, I am using this as an application submission form on some job postings on my site. These jobs do not live in the menu, so would that cause the $node = menu_get_object to not work? Since the node doesn't actually live in the menu, its just a node that is accessed through a page view.

bisonbleu’s picture

Couple of thoughts.

  1. From your code in #43, the name of you module has to be: ces_custom. Yes?
  2. Try changing ces_custom_form_alter(&$form, $form_state, $form_id) to ces_custom_form_alter(&$form, &$form_state, $form_id) (the '&' is missing in front of $form_state)
  3. First, try populating just the position_title field (i.e. remove or comment-out the previous line)
  4. Test

Get that part working first. Then build on it.

p.s. All that being said, in my case the form is embedded in the node currently viewed. So you may be right about the $node = menu_get_object() returning empty. Try using drupal_set_message() in your code to see what's happening. Or enable devel and use the all mighty dpm().

TravisJohnston’s picture

Thanks for your quick response bisonbleu,

That fixed it! Thanks a bunch!

Thomas Schuh’s picture

Thanks for this discussion!

I used another hook from webform api which allow me to set the hidden field to 'Secure'. This was for me important because I use the email from the node where the webformblock ist embadded. In my case it should not be displayed in html to prefent spambots to scan the emails.

/**
 * Implements hook_webform_submission_presave()
 */
function MYMODULE_webform_submission_presave($node, &$submission) {
 
if ($submission->nid == 715) { // 715 = nid of the webform node
  if ($node = menu_get_object()) {
      /**
       * data[4] = hidden email Field
       */
      $submission->data[4]['value'][0] = $node->field_email['und'][0]['email']; 
    }      
  }
}
ezoulou’s picture

#51 : Thanks for the usefull tip!
I got it working with a slight change (likely due to a newer webform api - I currently use webform 7.x-4.3) :

/**
 * Implements hook_webform_submission_presave()
 */
function MYMODULE_webform_submission_presave($node, &$submission) {
if ($submission->nid == 715) { // 715 = nid of the webform node
  if ($node = menu_get_object()) {
      /**
       * data[4] = hidden email Field
       */
      $submission->data[4][0] = $node->field_email['und'][0]['email']; 
    }      
  }
}
halth’s picture

I can confirm #15/#16 did the trick for me! Thank you :)

millionleaves’s picture

I know this is already marked as fixed, but if you apply the patch in #15 in the Token issue below, you'll get access to node tokens for the current node in your webform, which means you won't need to create a custom module as outlined in #7.

https://www.drupal.org/node/919760#comment-9973163

I used it to set the default value of a hidden field to the value of a node field so I could set the visibility of a subsequent field in the webform using conditionals.

The format of the field token I used is:

[current-page:node:field-my-field-name]

[edited for clarity]

Marko B’s picture

Shouldnt this be solved with version 7-4.x or is it still not working ?

ggarry’s picture

[current-page:title] if anyone comes across this. D7.