如何在Drupal表单API上填充字段

时间:2010-12-15 23:42:45

标签: drupal

我有一个

形式的drupal API
  function contactus(){
    return drupal_get_form('myform_contact');
  }

function myform_contact($form_state){
    $form['field'] = array(
    '#type' => 'fieldset',
    '#title' => 'Contact Us Form',
    '#collapsible' => true,
    '#collapsed'=> false,
    '#description' => t('Enter your feedback'),
    );

    $form['field']['email']  = array(
    '#type'=>'textfield',
    '#title'=> 'E-mail Address',
    '#required' => '0',
    '#maxlength' => 127,
    '#required' => 0,
    );

    $form['field']['comment'] = array(
    '#type'=> 'textarea',
    '#rows'=> 5,
    '#title' => 'Comment / Question? ',
    '#required' =>1,     
    );

    $form['field']['captcha'] = array(
    '#type' => 'captcha',
    '#captcha_type' => 'image_captcha/Image',

    );

    $form['field']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#validate' => array('form_validate'),
    '#weight' => 1
    );
    $form['field']['edit'] = array(
    '#type'=> 'button',
    '#value' => 'Edit',
    '#validate'=> array('form_edit'),
    '#weight' => 10,
    );

    return $form;
}

function myform_contact_submit($form, &$form_state){

    $txt = $form_state['values']['comment'];
    $email = $form_state['values']['email'];
    $txt = wordwrap($txt,70);
    mail("$email", "Comment / Questions ?", $txt);
    $result = db_query("insert into contactus values('$email','$txt')");
    $node = new stdClass();
    $node-> title = $form_state['values']['email'];
    $node-> body = $form_state['values']['comment'];
    $node->type = 'contactus';
    node_save($node);
    drupal_set_message(t('Form Successfully submitted with nid of '. $node->nid));
    drupal_set_message(t('Values inserted into Database'));
}

function form_edit($form,&$form_state){
   $nid = $form_state['values']['nid'];
   $node = node_load(49);
   drupal_set_message('edit mode with title '. $node->title. ' and comment as '. $node->body);
   $form_state['values']['email'] =  $node->title;
   $form_state['values']['comment'] = $node->body;     
}

在编辑时,我想用值填充表单字段。但是,它没有填充。任何想法??

1 个答案:

答案 0 :(得分:1)

据我所知,FAPI没有form_edit类型的钩子。通常有表单定义函数,验证和提交函数。如果您还没有,请查看文档here

在我看来,您需要将“#default_value”属性添加到要填充的表单元素中。您还需要将加载节点的任何代码移动到myform_contact函数中。