Horizontal tabs in Drupal 7 form API field group snippet

Here's how to use the field group module and form API in Drupal 7 to create a form with horizontal tabs.
<?php
/**
 * Example horizontal tabs form.
 */
function example_horizontal_tabs_form($form) {
  // Attaches css and js from the field group module.
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'field_group') . '/horizontal-tabs/horizontal-tabs.css',
  );
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'field_group') . '/horizontal-tabs/horizontal-tabs.js',
  );
 
  // Defines the main tabs element.
  $form['tabs'] = array(
    '#type' => 'horizontal_tabs',
    '#tree' => TRUE,
  );
 
  // Defines tab one.
  $form['tabs']['something_tab_one'] = array(
    '#type' => 'fieldset',
    '#title' => t('Something tab one'),
  );
 
  // Defines a sample field.
  $form['tabs']['something_tab_one']['something'] = array(
    '#type' => 'something',
    '#title' => t('something'),
    '#options' => array(
      'something' => 'something',
    ),
    '#default_value' => (isset($form['tabs']['something_tab_one']['something'])) ? $form['tabs']['something_tab_one']['something'] : 'something',
    '#description' => t('Choose something 1.'),
  );
 
  // Defines tab two.
  $form['tabs']['something_tab_two'] = array(
    '#type' => 'fieldset',
    '#title' => t('Something tab 1'),
  );
 
  // Defines a sample field.
  $form['tabs']['something_tab_two']['something'] = array(
    '#type' => 'something',
    '#title' => t('something'),
    '#options' => array(
      'something' => 'something',
    ),
    '#default_value' => (isset($form['tabs']['something_tab_two']['something'])) ? $form['tabs']['something_tab_two']['something'] : 'something',
    '#description' => t('Choose something 1.'),
  );
  return $form;
}

Tags

Internal References

External References

Article Type

General