Unless I am mistaken the Forms API needs to be used from within a module. I have a least twice now answered peoples questiion of their form not working when the code is provided as the content of a node. Logically that should not work since any form callback would not exist when the form iis processed after being submitted (the callbaclks would only exist after the node is loaded which happens after the form is processed.

So my suggestion for both the Quickstart Guide and Reference some where at the start add a note about the context in which the Form API can be used.

Thanks

Comments

rszrama’s picture

Actually, I'm pretty sure you can use the forms API from within the body of a node using the PHP filter. I've done it before for testing the API. You just name your functions the same way you would if it were a module. I posted an example at http://livetest.ubercart.org/node/64 with the following node body:


function test_form() {
  $form['message'] = array(
    '#type' => 'textfield',
    '#title' => t('Message'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

function test_form_submit($form_id, $form_values) {
  drupal_set_message(check_plain($form_values['message']));
}

print drupal_get_form('test_form');

Granted there will be some breakdown, but I'm pretty sure even hooks like hook_form_alter() will work fine with this. I have no clue why it works. ; )

----------------------
Current Drupal project: http://www.ubercart.org

criznach’s picture

Actually, I believe form processing only happens after your eval'd php calls drupal_get_form. I don't think there is any form processing before a page callback is called. So in the case of nodes, the node/nid callback calls node_page_view, then node_show/node_view renders the content and evals the code.

So when you submit:

  • menu handler calls node_page_view.
  • the node re-loads.
  • the php filter executes and evals the code. This brings your callbacks into scope.
  • drupal_get_form is called again and detects the "form_build_id" parameter from the submitted form.
  • drupal_process_form is called to process the submission, which calls drupal_validate_form and drupal_submit_form.
  • The form is redirected to it's destination (or the same page)

Can you tell I can't sleep?