0

I have a list of items. By clicking on an item I open a form at /items_form/123 where 123 is the itemid. When filling the form I use the arg() function to get the 123 and read some data from the database. No problem.

My problem is, when submitting the form, the default submit handler is not called. When I open the form without the arg e.g. /items_form and submit this form, the submit handler is called. Where is my missing link? Here is my hook_menu:

$items['items_form'] = array(
'title' => 'Item Form',
'page callback' => 'drupal_get_form',
'page arguments' => array('get_item_form'),
'access arguments' => array('access items_form content'),
'type' => MENU_NORMAL_ITEM, //Will appear in Navigation menu.
);

Any ideas?

9
  • You should use 'page callback' => 'drupal_get_form' in your code. Commented Oct 21, 2013 at 8:25
  • Hi! Thank you. I just realized my fault and edited it. The page callback is now 'drupal_get_form' and the 'page arguments' is the name of my function. I cleared the caches, but the submit doesn't work. ;-(
    – schulle877
    Commented Oct 21, 2013 at 8:41
  • Please add completed code for the form also. Commented Oct 21, 2013 at 8:51
  • Hi! Here is an example of the function used as submit handler function get_item_form_submit($form_id, $form_state) { echo "Hallo"; }
    – schulle877
    Commented Oct 21, 2013 at 8:58
  • When you submit the form, it will redirect to current form if you have not added redirection. Try 'function get_item_form_submit($form_id, $form_state) { echo "Hallo"; die; } Commented Oct 21, 2013 at 8:59

1 Answer 1

1

First, if you want to get an argument from the URL, you should declare it in hook_menu, as such:

$items['items_form/%'] = array(
  'title' => 'Item Form',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('get_item_form', 1),
  'access arguments' => array('access items_form content'),
  'type' => MENU_NORMAL_ITEM, //Will appear in Navigation menu.
);

Drupal's menu system will then pass in the itemid to drupal_get_form(), which will then pass it along to your form declaration function. You will need to add an argument to your function definition to access it, suggest something like this:

function get_item_form($form, $form_state, $itemid = NULL){
  $form = array();
    ...
}

Please look at the documentation for hook_menu for more information about how to handle URL arguments. I would caution against using arg() in your code.

The reason this is important is what happens when you submit your form with a URL argument. By default, if you do not override with the #action parameter in your form, it submits a POST request to the present URL. By not declaring that your URL has an argument in it, Drupal actually doesn't know how to handle a request to *items_form/123*. The only reason you see anything but a 404 when you go to *items_form/123* is that Drupal attempts to find the nearest path. I suspect that since it doesn't know explicitly what to do when a request is made to *items_form/123* it breaks the form submission handling.

I should also mention that you'll need to declare both *items_form* and *items_form/%* menu paths if you wish them both to work. They're distinct paths in the eyes of Drupal's menu system.

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.