I noticed that tabs were not being rendered in the order I coded them:
$form['example1']['tab1'] = array('#type' => 'tabpage',
'#title' => t('My Groups'),
'#content' => t($mygroups_view)
);
$form['example1']['tab2'] = array('#type' => 'tabpage',
'#title' => t('My Photos'),
'#content' => $mygallery_view
);
$form['example1']['tab3'] = array('#type' => 'tabpage',
'#title' => t('My Posts'),
'#content' => t($myactivity_view)
);
It seems that the culprit is lines 76 and 77 in tabs.module:
// Sort the elements by weight.
uasort($element, "_element_sort");
Without commenting out the sort in the module, is there an argument I can pass though the tabs_render() function to disable the sorting?
Comments
Comment #1
nedjoTry assigning explicit #weight attributes to each tab page.
Comment #2
ebeyrent commentedThat did the trick - thanks!
Comment #3
nedjoI'm not sure why the uasort is reordering items that don't have an explicit weight set. A problem with how I implemented it?
Comment #4
nedjoComment #5
nedjoReopening and marking http://drupal.org/node/172848 a duplicate.
Looking for ideas on why uasort is reordering. Here are the relevant lines in tabs.module:
And here is
_element_sort()from common.inc:Comment #6
robloachRemoving the sort fixed the issue for me as most applications of the jsTools Tabs module don't implement #weight and use their own sorting methods. Panels Tabs, for example, just puts the items in the order in which they should be displayed.
Could you try this in your situation? If it works for other applications, I'll make a patch for it.
Comment #7
jamesJonas commentedThis code only applies to using the Tab module inside of JStools (not Panels Tab). This is how I fixed the issue:
I set the weight for both the first and second set of tabs.
Comment #8
nedjoI implemented support for explicit #weight because in some cases we need it. This follows what we do with other form elements. The challenge is to identify the bug in the implementation, as I noted above (#5).
Comment #9
reg commentedCommenting out uasort will break routines that rely on it. Try this:
foreach ($element AS $value)
if (array_key_exists($value, '#weight')) {
uasort($element, "_element_sort");
break;
}
This will call the sort routine only if a '#weight' tag is found in the right place. NOTE: there is a '#weight' tag at the root level of the array otherwise we could do this without a loop.
Comment #10
reg commentedCommenting out uasort will break routines that rely on it. Try this:
This will call the sort routine only if a '#weight' tag is found in the right place. NOTE: there is a '#weight' tag at the root level of the array otherwise we could do this without a loop.
Comment #11
reg commentedFound I had to add a little more checking after installing more modules. My guess is that some of them aren't quite as stringent as they should be on what they pass. In any case, here is the code updated:
Comment #12
nedjoThanks for the troubleshooting. I've applied an attempted fix to HEAD and the DRUPAL_5 branch, which is simply to assign the elements a default #weight value in the
hook_elements()implementation. Untested. Anyone want to test and mark this issue accordingly?Comment #13
robloachAlthough I haven't tested it yet, Wim added the #weight to Panels Tabs here and here. It should be good now.
Comment #14
nedjoThe issue was that
uasort()changes the order of array elements even if there is no reason to reorder them.I've added code to give all unweighted tabs an explicit incremental weight.
Comment #15
(not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.