--- views_embed_form.module	Mon Nov 03 16:36:58 2008
+++ views_embed_form.module	Thu Nov 13 12:43:35 2008
@@ -36,6 +36,100 @@
 }
 
 /**
+ * Implementation of hook_forms()
+ *
+ * http://www.gtrlabs.org/blog/dayre/drupal_5_how_to_process_multiple_instances_of_the_same_form_on_the_same_page
+ *
+ * How does drupal_get_form() know where to get the form data from ?  For example,
+ * our view() function may call drupal_get_form() with a form id of
+ * mymodule_thing_form27.  We don't have a function called mymodule_thing_form27(),
+ * so the forms API will call this form hook to get the name of a function that
+ * can provide form data.  This is what prevents us from duplicating our _form()
+ * and _submit() functions for each unique form instance.
+ *
+ */
+function views_embed_form_forms() {
+
+  // the form id Drupal plans on using will be the first
+  // element of the first array.
+  $args = func_get_args();
+  $form_id = $args[0];
+
+  // Given a form_id like mymodule_thing_form27 or mymodule_thing_form13,
+  // we look for familiar text to tell we are dealing with the correct
+  // form.  Other functions will call this function as part of their form
+  // rendering (e.g. search form rendering) so we need to tell them apart.
+  $forms = array();
+
+  // Here we are telling the forms API that for any form ids starting
+  // with mymodule_thing_formX (X could be any unique id), callback to the
+  // function mymodule_thing_form to get the form data.
+  if ($callback = views_embed_form_is_embed_form($form_id)) {
+    $forms[$form_id] = array('callback' => $callback);
+  }
+
+  return $forms;
+}
+
+/**
+ * Implementation of hook_form_alter()
+ *
+ * Automatically repair callbacks to validate and submit form handlers
+ * if they are implemented
+ */
+function views_embed_form_form_alter(&$form, $form_state, $form_id) {
+
+  if ($callback = views_embed_form_is_embed_form($form_id)) {
+    if (function_exists($callback . '_submit')) {
+      $form['#submit'] = array($callback . '_submit');
+    }
+    if (function_exists($callback . '_validate')) {
+      $form['#validate'] = array($callback . '_validate');
+    }
+  }
+}
+
+/**
+ * Test if the form_id belongs to embed form.
+ *
+ * @param string $form_id
+ * @return form_callback | FALSE
+ */
+function views_embed_form_is_embed_form($form_id) {
+  $embed_forms = views_embed_form_get_forms();
+
+  // maybe we can use rtrim and array_key_exists(), to avoid this iteration
+  foreach ($embed_forms as $key => &$val) {
+    if (strpos($form_id,$key) === 0) {
+      return $key;
+    }
+  }
+
+  return FALSE;
+}
+
+/**
+ * Returns all embedded forms accessible for current user.
+ * Uses static caching.
+ *
+ * @return array
+ */
+function views_embed_form_get_forms() {
+  static $forms = array();
+
+  if (!count($forms)) {
+    foreach (module_implements('views_embed_form') as $module) {
+      $function = $module . '_views_embed_form';
+      $form_definiton = $function();
+      $forms = array_merge($forms,$form_definiton);
+    }
+  }
+
+  return $forms;
+}
+
+
+/**
  * Test form for Views Embed Form. Doesn't do anything, just print a Node ID.
  */
 
--- views_handler_field_views_embed_form.inc	Mon Nov 03 16:36:58 2008
+++ views_handler_field_views_embed_form.inc	Thu Nov 13 12:28:09 2008
@@ -2,6 +2,17 @@
 // $Id: views_handler_field_views_embed_form.inc,v 1.4 2008/11/03 15:36:58 meba Exp $
 
 class views_handler_field_views_embed_form extends views_handler_field {
+
+  /**
+   * Helper counter for generating unique form ids
+   *
+   * @return int
+   */
+  static function form_counter() {
+    static $counter = 0;
+    return $counter++;
+  }
+
   function option_definition() {
     $options = parent::option_definition();
     $options['embedded_form'] = array('default' => FALSE);
@@ -37,7 +48,7 @@
       $function = $module . '_views_embed_form';
       $form_name = $function();
       if (isset($form_name) && key($form_name) === $this->options['embedded_form'] && function_exists($this->options['embedded_form'])) {
-        return drupal_get_form($this->options['embedded_form'], $values);
+        return drupal_get_form($this->options['embedded_form'] . self::form_counter(), $values);
       }
     }
     return t('You do not have permission to access embedded form.');
