12

I have described the form, but every element there is located under the previous one. I need to describe the form where all elements will be placed horizontally but not vertically. This is my form:

function contact_register_form($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('Sign up to be notified when your community launches:'),
  );

  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Add me',
  );    

  return $form;
}
17

You can use code similar to the following one, used by the Node module in node_filter_form().

  // Build the 'Update options' form.
  $form['options'] = array(
    '#type' => 'fieldset', 
    '#title' => t('Update options'), 
    '#attributes' => array('class' => array('container-inline')), 
    '#access' => $admin_access,
  );

  // ...

  $form['options']['operation'] = array(
    '#type' => 'select', 
    '#title' => t('Operation'), 
    '#title_display' => 'invisible', 
    '#options' => $options, 
    '#default_value' => 'approve',
  );

The key is the line settings the "#attributes" attribute to "container-inline."

That code is for Drupal 7; the equivalent code for Drupal 6 starts with the following code:

  $form['options'] = array(
    '#type' => 'fieldset', 
    '#title' => t('Update options'), 
    '#prefix' => '<div class="container-inline">', 
    '#suffix' => '</div>',
  );

Supposing you are using Drupal 6, then your code should be changed to something similar to the following one:

function contact_register_form($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('Sign up to be notified when your community launches:'),
  );

  $form['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#prefix' => '<div class="container-inline">', 
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Add me',
    '#suffix' => '</div>',
  );    

  return $form;
}

I didn't put the description inline, as it would not be correctly rendered because it is using a "item" form field. I also find that inlining the description would cause the form to take too much space. (Imagine what would happen if the inlined description would be longer, and placed in a single line.)

The CSS styles that Drupal 7 adds to container-inline elements are the following.

/**
 * Inline items.
 */
.container-inline div,
.container-inline label {
  display: inline;
}
/* Fieldset contents always need to be rendered as block. */
.container-inline .fieldset-wrapper {
  display: block;
}

They are added from system.base.css.

  • 1
    And don't forget to apply a float in CSS to the container-inline class. – tostinni Jul 27 '11 at 17:09
  • Is there a way to do this without code? I have very good control on forms display, but except for a few columns in DS extra, but nothing granular like the webform layout module for form input data – Bruno Vincent Oct 23 '18 at 6:23

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.