Score:0

Is there a way to pass a value to a hidden field to multiple blocks printed from a template file?

ng flag

The scenario is a template (tpl) file outputting a list of contact forms where each one is unique to a term ID in it section on the page. I need to generate a custom options list per printed Webform block using a tid passed from the section it's in in the template. So far I don't see any way to do this.

sample.tpl.php

<?php
   $block = module_invoke('webform', 'block_view', 'my-block-3581');
   print render($block['content']);
?>

The above is printed for each section (node) on the page and I have access to the tid where the Webform block is printed but I can't figure out any way to pass it to the form.

Score:1
cn flag

you can always use drupal_get_form function to retrieve a form in drupal. this function allows you to send any needed arguments for example you can call it like this:

$form = drupal_get_form('SOME_FORM_ID', $argument1);

in this case $argument1 value is passed and can be accessed in hook_form_SOME_FORM_ID_alter as follow:

if(isset($form_state['build_info']['args'][0]) {
  $form['SOME_ELEMENT']['#default_value'] = $form_state['build_info']['args'][0]; //as $arguments1 is passed as first parameter
}

now having this in mind, to get a webform using drupal_get_form you need this code snippet (better to use hook_preprocess_HOOK to add webform to your render array):

$nid = ; //YOUR WEBFORM NODE ID
$node = node_load($nid);
$submission = (object) array();
$enabled = TRUE;
$preview = FALSE;
$webform = drupal_get_form('webform_client_form_' . $nid, $node, $submission, $enabled, $preview, $YOUR_ADDITIONAL_ARGUMENT);

and then in your template file you can simply print your $webform:

print drupal_render($webform);

as I said you can use hook_form_alter to set default value of your hidden element using sent parameter.

Juraj Nemec avatar
in flag
I think that it is useful to say, that it is not a good idea to call `drupal_get_form` in template files. Ideally, OP will need to use one of the preprocess functions in template.php, or pass $webform as a parameter to that template file (if the `theme()` function is called directly, for example from a custom module).
Alireza Tabatabaeian avatar
cn flag
@JurajNemec and I think that is a good idea to say that it was already mentioned in the answer
quantumized avatar
ng flag
Thank you! One more question. How can I access the argument in the Webform once it's sent via drupal_get_form()? I'd like to populate a hidden field, if possible.
Alireza Tabatabaeian avatar
cn flag
You can use hook_form_alter and then you can access your argument $form_state['build_info']['args'][4]
Juraj Nemec avatar
in flag
@AlirezaTabatabaeian Well, I wrote my comment because for me it is not clearly stated in you answer. You wrote it as a side note only in the parentheses, so it seems like that there is only a small weight on that expression. But nevermind, I just wanted to help the OP with best practices...
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.