You are here

function vertical_tabs_add_vertical_tabs in Vertical Tabs 6

Add a vertical tab form element to a form.

Parameters

$form: A form array to be altered.

$config: An array of fieldsets to use in the vertical tabs. If no array is provided, all the fieldsets in the $form array will be used.

Return value

TRUE if the vertical tabs were added to the form, otherwise FALSE.

1 call to vertical_tabs_add_vertical_tabs()
vertical_tabs_form_pre_render in ./vertical_tabs.module
Form pre-render callback; add vertical tabs to the form.

File

./vertical_tabs.module, line 316
Provides vertical tabs capability for fieldsets in forms.

Code

function vertical_tabs_add_vertical_tabs(&$form, $config = array()) {
  global $theme;
  $settings = array();
  $weight = $delta = 0;

  // Iterate through the form's fieldset elements.
  $elements = vertical_tabs_get_form_elements($form);
  foreach (array_keys($elements) as $key) {
    $element =& $elements[$key];
    $element += array(
      '#group' => variable_get('vertical_tabs_default', 1),
    );

    // If there is a specific config set, override the default group setting.
    if (isset($config[$key]) && (bool) $config[$key] != (bool) $element['#group']) {
      $element['#group'] = $config[$key];
    }

    // Skip any non-grouped elements.
    if (empty($element['#group'])) {
      continue;
    }

    // Process the element.
    vertical_tabs_process_element($element, $key);
    vertical_tabs_process_attached($element);
    $settings[$key] = array(
      'name' => $element['#title'],
      'weight' => isset($element['#weight']) ? $element['#weight'] : 0,
      'callback' => isset($element['#summary_callback']) ? $element['#summary_callback'] : $key,
      'args' => isset($element['#summary_arguments']) ? $element['#summary_arguments'] : array(),
    );

    // Track the maximum weight for the vertical tabs element.
    $weight = max($weight, $settings[$key]['weight']);
    $settings[$key]['weight'] += 0.001 * $delta++;
  }

  // The JavaScript and CSS specific for this form.
  if (count($settings) >= variable_get('vertical_tabs_minimum', 1)) {
    $js = $css = array();

    // Add theme-specific CSS.
    if (isset($theme)) {
      $theme_stylesheets = variable_get('color_' . $theme . '_stylesheets', array());
      if (!$theme_stylesheets || !module_exists('color')) {

        // The theme-specific CSS will be only included by drupal_get_css() if
        // it exists so we do not need to check file_exists() here.
        $css[] = drupal_get_path('module', 'vertical_tabs') . '/' . $theme . '/vertical_tabs.' . $theme . '.css';
      }
      else {
        foreach ($theme_stylesheets as $path) {
          if (strpos($path, 'vertical_tabs.' . $theme . '.css') !== FALSE) {
            $css[] = $path;
          }
        }
      }

      // Ensure the CSS files actually exist.
      $css = array_filter($css, 'file_exists');
    }

    // User sort orders by the "weight" key.
    uasort($settings, '_user_sort');
    $form['vertical_tabs'] = array(
      '#type' => 'markup',
      '#value' => '',
      '#theme' => 'vertical_tabs',
      '#attributes' => array(
        'class' => 'vertical-tabs clear-block',
      ),
      '#weight' => $weight,
      '#attached' => array(
        'js' => $js,
        'css' => $css,
      ),
    );
    $form['vertical_tabs']['#attached']['js'][] = array(
      'data' => array(
        'verticalTabs' => $settings,
      ),
      'type' => 'setting',
    );

    // Resort the form since we've added a new element after it's been sorted.
    uasort($form, 'element_sort');
    return TRUE;
  }
}