0

In a User Tab there are multiple node forms of the same type.

To do this I implemented hook_forms. Multiple forms are shown and they work (sometimes). But if the user adds some (or delete) nodes and calls his page again he gets the following error:

(Lets say the user owns node 1 and 2)

Notice: Undefined index: mymodule_a_form_1 in drupal_retrieve_form() (Zeile 738 von /htdocs/includes/form.inc).

Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'mymodule_a_form_1' was given in drupal_retrieve_form() (Zeile 773 von /htdocs/includes/form.inc).

Notice: Undefined index: mymodule_a_form_2 in drupal_retrieve_form() (Zeile 738 von /htdocs/includes/form.inc). Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'mymodule_a_form_2' was given in drupal_retrieve_form() (Zeile 773 von /htdocs/includes/form.inc).

The forms work again if I clear all caches.

So what is wrong with this (sample) code?:

<?php

function mymodule_user_profile_tab_content($uid) {
  $out = array();
  $users_node_ids = _get_user_node_ids();
  foreach($users_node_ids as $nid) {
    $node = node_load($nid);
    $out[] = drupal_get_form('mymodule_a_form_'.$nid, array('node' => $node));
  }
  return $out;
}

/**
 * Implementation of hook_forms()
 */
function mymodule_forms($form_id, $args) {
  $forms = array();
  if (strpos($form_id, 'mymodule_a_form_') === 0) {
    $forms[$form_id] = array(
      'callback' => 'mymodule_a_form',
    );
  }
  return $forms;
}

closed as too broad by kiamlaluno Nov 29 '18 at 14:25

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

I had the same problem and it seemed to be resolved after correcting an issue with the function definition. Make sure that $form_state is a reference.

function mymodule_a_form($form_id, &$form_state, $args) {
...
}

One of the other things that I noticed is that the cached module_implements no longer had a reference to my module, so when Drupal was looping through the implements and looking for a function called mymodule_forms(), it couldn't find it. You can check to see if this is the issue by comparing the cached implements to the static version.

    $cache_implements = cache_get('module_implements', 'cache_bootstrap')->data['forms'];
    ksort($cache_implements);
    dpm($cache_implements, 'CACHED FORM IMPLEMENTS');

    $static_implements = drupal_static('module_implements');
    ksort($static_implements);
    dpm($static_implements['forms'], 'STATIC FORM IMPLEMENTS');

If that is the issue and your form declaration is correct then maybe try doing an include on the page that references your dynamic forms.

module_load_include('admin', 'mymodule', 'inc');  

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