By joshua.howell on
I would like to add some code to the jQuery Tabs module which "hides a tab if the content inside that tab is empty". Here is the code which i think needs to be altered;
/**
* Process a tabset prior to rendering.
*/
function tabs_process_tabset($element) {
static $names = array();
// Ensure name is unique.
$i = 0;
// Assign a name, reading from the first parent (the key of this tabset element).
$name = $element['#tabset_name'] = ($element['#tabset_name'] ? $element['#tabset_name'] : (isset($element['#parents']) && count($element['#parents']) ? $element['#parents'][0] : 'tabset'));
// In case we have duplicate names...
while (in_array($element['#tabset_name'], $names)) {
$element['#tabset_name'] = $name . $i;
$i++;
}
$names[] = $element['#tabset_name'];
// Add class.
if (!isset($element['#attributes'])) {
$element['#attributes'] = array();
}
$element['#attributes']['class'] = (isset($element['#attributes']['class']) ? $element['#attributes']['class'] .' ' : '') .'drupal-tabs'. (isset($element['#tabs_navigation']) && $element['#tabs_navigation'] ? ' tabs-navigation' : '');
$index = 1;
// Sort the elements by weight.
// Because uasort doesn't keep the original order for equal values,
// we assign a nominal weight to all elements.
foreach (element_children($element) as $count => $key) {
if (!isset($element[$key]['#weight']) || $element[$key]['#weight'] == 0) {
$element[$key]['#weight'] = $count/1000;
}
}
uasort($element, 'element_sort');
foreach (element_children($element) as $key) {
if (isset($element[$key]['#type']) && $element[$key]['#type'] == 'tabpage') {
// Display any #description before the #content.
$element[$key]['#content'] = ($element[$key]['#description'] ? '<div class="description">'. $element[$key]['#description'] .'</div>' : '') . $element[$key]['#content'];
$element[$key]['#tabset_name'] = $element['#tabset_name'];
$element[$key]['#index'] = $index++;
}
}
return $element;
}
/**
* Return rendered tabset.
*
* @themable
*/
function theme_tabset($element) {
$output = '<div id="tabs-'. $element['#tabset_name'] .'"'. drupal_attributes($element['#attributes']) .'>';
$output .= '<ul>';
foreach (element_children($element) as $key) {
if ($element[$key]['#type'] && $element[$key]['#type'] == 'tabpage') {
$output .= '<li'. drupal_attributes($element[$key]['#attributes']) .'><a href="#tabs-'. $element['#tabset_name'] .'-'. $element[$key]['#index'] .'">'. $element[$key]['#title'] .'</a></li>';
}
}
$output .= '</ul>';
$output .= $element['#children'];
$output .= '</div>';
return $output;
}
/**
* Return rendered content of a tab.
*
* @themable
*/
function theme_tabpage($element) {
$output ='<div id="tabs-'. $element['#tabset_name'] .'-'. $element['#index'] .'">';
$output .= '<h2 class="drupal-tabs-title">'. $element['#title'] .'</h2>';
$output .= $element['#content'] . $element['#children'];
$output .='</div>';
return $output;
}
I tried testing if "isset($element['#content'])" in the foreach loop of the "theme_tabset($element)" function but without any luck. I think this is the right function to alter but if ayone with more PHP experience could offer some advice that would be greatly appeciated.
Comments
Applied a fix
See #537798: Hide tab if empty.