My issue:
I have a content type called "store". The website has 14 total stores which all of them should have a contact form. I created a webform and used the block option so i wouldn't need to create 14 webforms for each form. The webform must have a dynamic value on the field store to identify which store is being contacted. Since the node title is the name of the store I am using the node title to identify the store.
The webform component $form['submitted']['loja'] is no editable so I used the default value to achieve this requirement. I also have some code commented out if i happen to need to change the email address destination for each store as well.
I used the following code for that in a custom module I created for the website:
function mymodule_custom_form_webform_client_form_84_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_client_form_84') {
//dsm($form);
//Get the current path of the page/node being displayed
$q = $_GET['q'];
$q = explode('/',$q);
$qid = (int)$q[1];
$node = node_load($qid);
//dsm($node);
//Get the alias of the current path using the drupal API
//use this for validation because the form should only run this code on lojas/*/[node-title]
//$path = $_GET['q'];
//$alias = drupal_lookup_path('alias', $path);
//dsm($alias);
//$email_loja = $node->field_email_lojas['und'][0]['email'];
//$webform = node_load(84);
//$webform->webform['emails'][1]['email'] = $email_loja;
$form['submitted']['loja']['#default_value'] = $node->title;
}
}
Let me know if you know a better way of doing this or if you find any security issues here.
Comments
Comment #1
quicksketchThis question falls squarely in the realm of an unsupported request. From the submission guidelines:
As mentioned in the submission guidelines, I don't provide help with custom coding:
Please refer to other resources such as IRC or Drupal StackExchange to ask your questions.
That said, the code looks fine to me. You may want to keep an eye on #1001798: Rewrite token replacement system to use D7 tokens, which would provide you a mechanism to do this with tokens, thus not needing any custom coding.
Comment #2
utneon commentedQuicksketch I do intend do make a contributed module regarding this functionality. I haven't found yet any module with this function yet! :)
Comment #3
quicksketchThe 4.x branch has switched to using the new D7 token system, so if #919760: Add a [current-page:object:?] dynamic token were to be committed to the Token project, you could add a hidden field and set the default value to [current-page:object:author:mail], which would be pretty impressive.
I'm marking this issue fixed since it's as close to completion as you'll get in the issue queue. Like I mentioned above we don't provide coding support in the issue queue.