hi there

when the webform node was loaded
hook_form_alter is always called twice.

but with no difference in the $form/ $form_state objects..

so its not possible to check if the hook_form_alter
was allready called first or second time!

this is a problem in my scenario:

i call the webform from a link on a node.


<a href="myform/nid=123">call the webform</a>

now i want to get the node title from the refered node
into a textfield component.

yes, i know, i could write the node titel into the webform-link:

<a href="myform/title=this+is+the+node+title">call the webform</a>

and trace it via webform
%get[title]

but that does not fit here for me
(IMHO: it is not a good practice to handle with titles in an url instead of id's)

so:what i want is to use the nodeID
instead the node title
in the url

i set as default value the %get[id] into my text field component.
(now available in $form["submitted"]["my_form_component"]["#default_value"])

hook_form_alter()
i fetch the node->title by
by loading the node :

//default_value is the node->id
//example 123
$node = node_load($form["submitted"]["my_form_component"]["#default_value"]);

and then getting the node title and set it as default value:

$form["submitted"]["seminar"]["#default_value"]=$node->title; 

so, good thinking.

BUT

for whatever reason:
webform is called twice!

with following effect:

//default_value is now allready the node->title
//example "this drive me crazy"
$node = node_load($form["submitted"]["my_form_component"]["#default_value"]);

and of course that causes an error!

what is the correct way to do this?
or:
why webform is calling 2 times
and how can i prevent this double hook_form_alter?

// here my complete code
function mymodule_form_webform_client_form_ID_alter(&$form, &$form_state) {

   $node = node_load($form["submitted"]["my_form_component"]["#default_value"]);
  $form["submitted"]["seminar"]["#default_value"]=$node->title;

}

thankfull for any advice!!

Comments

quicksketch’s picture

Title: webform is always called twice » Webform is always called twice
Status: Active » Closed (won't fix)

Hi @1kubik, we specifically state in the submission guidelines that we don't help with custom coding:

Any issues regarding "how do I code ..." or "how do I theme ..." will not be answered. Please look elsewhere for coding resources.

Please refer to other resources such as IRC or Drupal StackExchange to ask your questions.

The double-form-alter problem you're experiencing might be caused by any number of reasons depending on how your site has been built, so I can't really speak to why it's happening. Webform builds the form when node_view() is called, so the only likely cause is that something is calling this function twice. I suggest using debug_backtrace() or a debugger to determine where/how this is getting called twice.

1kubik’s picture

dear quicksketch
thanks for your answer!
and apologize that I overlooked your guidelines..

debug_backtrace() is a very good hint!

best regards