I have come across a problem with nested items in the theme_item_list function (part of the theme.inc include of Drupal 5.1)
This function basically takes an array of items as input and outputs them in a list. A patch (added last year) allows with Drupal 5 to create nested lists. To use this functionality you have to include the nested items (so childrens) as part of an array using 'children' as key. Then the function uses recursion to create the nested lists.
Now I have noticed that if you input an array which consists of first a number of normal items and then an array with children, the last item of the normal item list is returned twice in the output. This is due to the fact that the "$data" variable is not reset for each item you provide as input. In the case of a normal item this isn't a problem (because the "$data" variable gets overrided each iteration) but in case of children it is a problem because in that case output is just appended to the "$data" variable, the "$data" variable is not overrided.
The solution for this is quite simple (as far as I can tell off course): the "$data" variable needs to be reset either at the beginning of the foreach ($items) loop or else before the recursive call for the nested items (after the (if count(children) > 0), or the nested function could also override the "$data" variable instead of appending to it. I have taken the first option and now everything seems to work as I would expect it to.
Can someone confirm that this is indeed a bug (I don't see any reason why this behaviour should be expected) and provide a fix (I am willing to provide a patch myself - if someone can point me to an easy description of how to create a patch file)
Comments
Comment #1
stevenpatzhttp://drupal.org/patch/create
Comment #2
fullflavedave commentedI believe that the problem is in this line in theme_item_list():
"$data .= theme_item_list($children, NULL, $type, $attributes); // Render nested list"
I fixed the behavior by changing the append operation to an assignment operation (by removing the "."):
"$data = theme_item_list($children, NULL, $type, $attributes); // Render nested list"
After changing this line, the function seems to work as expected now.
Here is my version of the fully patched function:
function theme_item_list($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
$output = '
if (isset($title)) {
$output .= '
'. $title .'
';
}
if (!empty($items)) {
$output .= "<$type" . drupal_attributes($attributes) . '>';
foreach ($items as $item) {
$attributes = array();
$children = array();
if (is_array($item)) {
foreach ($item as $key => $value) {
if ($key == 'data') {
$data = $value;
}
elseif ($key == 'children') {
$children = $value;
}
else {
$attributes[$key] = $value;
}
}
}
else {
$data = $item;
}
if (count($children) > 0) {
$data = theme_item_list($children, NULL, $type, $attributes); // Render nested list
}
$output .= '
';
}
$output .= "";
}
$output .= '
';
return $output;
}
Comment #3
fullflavedave commentedAfter rereading the original post, I realized my post was a little redundant, since it just detailed method two of patching the function. I'll check to see if there has been a patch submitted.
Comment #4
tim.plunkettD5 not supported, working patch for newer versions in #774040: theme_item_list function duplicates last $data value when dealing with children.