0

I have got a form defined that is displayed multiple times on a single page. For each instance of the form, different values (node ids) are inserted into the hidden fields, which seems to be there once the form is rendered.

However, when I submit any of the forms on the page, the submit handler always gets the values that have been inserted for the first instance and I can't seem to get hold of why this is happening.

One thing that I noticed though is that all of my forms have the same form_id, form_token and only the form_build_id changes.

Any idea why this is happening, I can't find a solution to this problem anywhere.

Thanks a lot.

Code:

I'm attaching the form to the nodes in hook_node_view like this:

$node->processed['add_collection_form'] = drupal_get_form('add_to_collection_form', $node->nid);

when I try to do it in hook_node_load drupal always complains about the memory limit in PHP even though I have set it to a generous 1GB, which is pretty weird.

The form on the page are rendered the standard way:

print drupal_render($node->processed['add_collection_form']);

in node--.tpl.php

4
  • Would need to see how you are building the page and rendering the form. I have built landing pages for marketing that have 3, 4 and sometimes 10 forms on a single page - never had this happen. If you can post some example code, it would easier to answer your question.
    – Brady
    Commented Jan 1, 2017 at 20:05
  • Hey, I've edited my post adding code samples.
    – crzpiot
    Commented Jan 1, 2017 at 20:13
  • Ah, you are doing this in Node templates. I have always done this with a custom module.
    – Brady
    Commented Jan 1, 2017 at 20:38
  • But the problem is they all have the same ID.
    – Brady
    Commented Jan 1, 2017 at 20:40

1 Answer 1

2

To use multiple instances of the same form on a page you need to implement hook_forms(), which allows you to essentially use 'aliases' for your forms.

function hook_forms($form_id, $args)
{
  $forms = array();

  // Check if the form id is of the pattern add_to_collection_form-[NUMBER]
  if(preg_match('/^add_to_collection_form-\d+$/', $form_id)
  {
    $forms[$form_id]['callback'] = 'add_to_collection_form';
  }

  return $forms;
}

The above code tells Drupal that if a form ID has the pattern add_to_collection_form-[NUMBER], to use the add_to_collection_form form callback.

Now you can call the forms on your page like this:

drupal_get_form('add_to_collection_form-1', $node->nid);
drupal_get_form('add_to_collection_form-2', $node->nid);
drupal_get_form('add_to_collection_form-3', $node->nid);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.