You are here

function tabs_pre_render_tabset in Tabs (jQuery UI tabs) 6

Process a tabset prior to rendering.

Passes the tabset element through drupal_alter() so that modules can implement hook_tabs_alter().

1 string reference to 'tabs_pre_render_tabset'
tabs_elements in ./tabs.module
Implementation of hook_elements().

File

./tabs.module, line 49
API for creating tabbed pages.

Code

function tabs_pre_render_tabset($element) {
  tabs_load();

  // Assign a name, reading from the first parent (the key of this tabset element).
  $name = $element['#tabset_name'] = form_clean_id(isset($element['#tabset_name']) && $element['#tabset_name'] ? $element['#tabset_name'] : (isset($element['#parents']) && count($element['#parents']) ? $element['#parents'][0] : 'tabset'));

  // Add class.
  if (!isset($element['#attributes'])) {
    $element['#attributes'] = array();
  }
  $element['#attributes']['class'] = (isset($element['#attributes']['class']) ? $element['#attributes']['class'] . ' ' : '') . 'drupal-tabs js-hide' . (isset($element['#tabs_navigation']) && $element['#tabs_navigation'] ? ' tabs-navigation' : '');

  // Allow other modules to alter the tabset before further processing.
  drupal_alter('tabs', $element);
  $children = element_children($element);
  foreach ($children as $index => $key) {
    if (isset($element[$key]['#type']) && $element[$key]['#type'] == 'tabpage') {
      if (!isset($element[$key]['#content'])) {
        $element[$key]['#content'] = '';
      }
      if (isset($element[$key]['#description']) && $element[$key]['#description']) {
        $element[$key]['#content'] = '<div class="description">' . $element[$key]['#description'] . '</div>' . $element[$key]['#content'];
        unset($element[$key]['#description']);
      }
      $is_ajax = isset($element[$key]['#ajax_url']) && $element[$key]['#ajax_url'];

      // Unset any empty tabs.
      $content = trim($element[$key]['#content']);
      if (!$is_ajax && empty($content) && !element_children($element[$key])) {
        unset($element[$key]);
        continue;
      }
      $element[$key]['#tabset_name'] = $element['#tabset_name'];

      // If no tab_name is set and we use descriptive titles, use one.
      if ((!isset($element[$key]['#tab_name']) || empty($element[$key]['#tab_name'])) && variable_get('tabs_descriptive_urls', 0)) {

        // Clean out invalid characters.
        $clean_title = preg_replace('/[^\\sa-zA-Z0-9\\ ]+/ ', ' ', $element[$key]['#title']);
        $element[$key]['#tab_name'] = strtolower($clean_title);
      }

      // If the tab_name is still empty, set one.
      if (!isset($element[$key]['#tab_name']) || empty($element[$key]['#tab_name'])) {
        $element[$key]['#tab_name'] = $element['#tabset_name'] . '-tab-' . $index;
      }
      $element[$key]['#tab_name'] = form_clean_id($element[$key]['#tab_name']);
      if (!isset($element[$key]['#attributes'])) {
        $element[$key]['#attributes'] = array();
      }

      // If we are loading via AJAX we need to add a title attribute and set the url to the AJAX path.
      if ($is_ajax) {
        $element[$key]['#attributes']['#title'] = $element[$key]['#tab_name'];
        $element[$key]['#url'] = $element[$key]['#ajax_url'];
      }
      else {
        $element[$key]['#url'] = '#' . $element[$key]['#tab_name'];
      }
      $element[$key]['#attributes']['class'] = (isset($element[$key]['#attributes']['class']) ? $element[$key]['#attributes']['class'] . ' ' : '') . $element[$key]['#tab_name'];

      // Add the ui-tabs-selected class if this tab is selected.
      if (isset($element[$key]['#selected']) && $element[$key]['#selected']) {
        $element[$key]['#attributes']['class'] .= ' ui-tabs-selected';
      }
      $element[$key]['#index'] = $index;
    }
  }
  return $element;
}