Search code examples
drupaldrupal-6

drupal 6 add html to web forms


I am creating a custom module. I have a form that I would like to add some HTML to, but I don't know what the best way to do this is. In this example, I have a page with a textbox, dropdown list, and text area. I want to add a a div between the dropdown list and text area. But, I'm not sure how to add raw html to a web form. Here is what I have:

function myModule_add_form($form_state){
    try{
        $form = array();

        $form['myModule_title'] = array(
            '#title' => 'Title',
            '#type' => 'textfield',
            '#size' => '30',
            '#weight'=>1,
        );

        $form['myModule_type_list']=array(
            '#type'=>'select',
            '#title' => 'Type List',
            '#options' => $someArray,
            '#multiple' => false,
            '#attributes'=>array('size'=>1),
            '#weight'=>2,
        );


        $form['myModule_description'] = array(
            '#title' => 'Description',
            '#type' => 'textarea',
            '#size' => '255',
            '#weight'=>3,
        );

        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => 'Submit',
            '#weight'=>4,
        );

        return $form;
    }catch(Exception $e){
        $errrmsg = "Error with creating form: " .$e->getMessage();
        throw New Exception($errrmsg);
    }


}

Thanks jason


Solution

  • Here is the reference for this: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/6

    I think the best option is the markup form type (This is the default type) so if you do not set the 'type' it will be markup, so you can simply insert something like this wherever you want:

    $form['some_div'] = array(
      '#value' => '<div>The div</div>',
    );
    

    The other option is to use prefix or suffix as also mentioned in the form API, e.g.:

    $form['myModule_type_list']=array(
      '#type'=>'select',
      '#title' => 'Type List',
      '#options' => $someArray,
      '#multiple' => false,
      '#attributes'=>array('size'=>1),
      '#weight'=>2,
      '#suffix' => '<div>The div</div>',
    );
    

    I added the div as a suffix of your list, or it could be a prefix of the text area.

    Another way of doing the first option which you may prefer for readability is like this:

    $form['some_div'] = array(
      '#type' => 'markup',
      '#prefix' => '<div>',
      '#value' => 'The div content',
      '#suffix' => '</div>',
    );