0

I create a module like this:

<?php
function mymodule_menu(){
    $items['mymodule/edit']=array(
        'title'=>t('Edit item'),
        'type'=>MENU_CALLBACK,
        'page callback'=>'mymodule_edit_view',
    );
    return $items;
}

function mymodule_edit_view(){
    //...
    $html.='<form '.$form_attr.'>';
    //...
    $html.='</form>';
    //...
    return $html;
}

function mymodule_form_alter(&$form, $form_state, $form_id){
    //...
}

I create a form in mymodule_edit_view, but when I access mymodelu/edit, the drupal did not call mymodule_form_alter. I do not want to create the form with drupal_get_form(), So how should I create the form?

  • You really, really should be using drupal_get_form(). Why don't you want to use it? – Web Assistant Dec 4 '12 at 12:00
  • What is hook_form_alert()? – Alex Gill Dec 4 '12 at 12:03
  • @AlexGill I should be mymodule_form_alter – solomon_wzs Dec 4 '12 at 12:05
  • 1
    Surely this shouldnt work anyway by not using form api or doesnt that matter? – Alex Gill Dec 4 '12 at 12:14
  • 1
    For your reference and to get a clear idea about how drupal forms working check form_example module in drupal.org/project/examples . All possible format for forms is described in this module – Anoop Joseph Dec 4 '12 at 12:38
1

This shouldn't work as @Alex said, because you are not using Drupal's Form API. You should implement hook_form($node, &$form_state), to create a form.

Sample code which creates a form with a single text field:

function MODULE_form() {
  $form = array();
  $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => t('Field description goes here...'),
    '#required' => TRUE, 
  );
  return $form;
}
  • If I use the form api, how should I render the form by myself? – solomon_wzs Dec 4 '12 at 12:26
  • Basically, you need to recreate your form to one that goes through the Drupal form API. Then you can render the form into HTML using render(drupal_get_form('FORM_NAME')); – Web Assistant Dec 4 '12 at 12:32
  • So where has the document to tell me how to set the field of $form, I mean what fields I could set and what value I could set – solomon_wzs Dec 4 '12 at 12:50
  • 1
    drupal.org/node/751826 and api.drupal.org/api/drupal/… should be all you need. – Web Assistant Dec 4 '12 at 12:53
  • 1
    @solomon_wzs Drupal's API is flexible enough, that you could do almost anything you can think of. Just refer to the Examples module to see the example of form API, also see the link in the answer above for further information on Form API. – AjitS Dec 4 '12 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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