0

In my custom_apps.module file I'm trying to implement the same form multiple times with different ID's and for that in a loop I call for:

drupal_render(drupal_get_form('driver_route_'.$delivery_nid, $args));

I have a hook_forms function:

function custom_apps_forms ($form_id, $args) {
    // If driver route form
    if (0 == strpos($form_id, 'driver_route_')) {
        // Call one of the multiple forms
        $forms = array();
        return $forms[$form_id] = array(
            'callback' => 'driver_route_form',
            'callback arguments' => array($form_id, $args),
        );
    }
    return array();
}

And the driver_route_form() function itself:

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

However for some reason the driver_route_form never gets called and instead I get this notices/warnings:

Notice: Undefined index: driver_route_55 in drupal_retrieve_form() (line 806 of /var/www/website/public_html/includes/form.inc).
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'driver_route_55' not found or invalid function name in drupal_retrieve_form() (line 841 of /var/www/website/public_html/includes/form.inc).

It seems as if it's still trying to look for a function with the form_id not the one defined in my callback. What could be the casue of this? I have made sure the inside of custom_apps_form where the form is returned is reached.

4
  • Did you clear cache after writing your hook_forms implementation? Jun 13, 2016 at 13:02
  • Yes, multiple times. Maybe I have a typo somewhere in the code I can not see? Jun 13, 2016 at 13:05
  • Ah, that return isn't right Jun 13, 2016 at 13:10
  • You just have a simple error in your PHP; currently you're returning an un-keyed array, you need to return a keyed array
    – Clive
    Jun 13, 2016 at 13:13

1 Answer 1

1

Instead of:

return $forms[$form_id] = array(
            'callback' => 'driver_route_form',
            'callback arguments' => array($form_id, $args),
        );

You should have:

$forms[$form_id] = array(
            'callback' => 'driver_route_form',
            'callback arguments' => array($form_id, $args),
        );
return $forms;
2
  • Should I not return anything unless my hook changes something, so remove the return from outside of the array? It seems that with the outside return my other form renders break, but without it they're fine, I'm not sure what the correct drupal way is.. Is it ok to have a hook function not return anything? Jun 13, 2016 at 14:03
  • I just noticed I need a strict operator for the if :) Jun 13, 2016 at 14:10

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