The taxonomy_dhtml.module exports a block for each vocabulary which has a link for each term. These links may nest arbitrarily deep, just like our taxonomy.module. The theme_item_list() function is great for displaying a set of items, but it assumes all items are at the same depth level. This request is for theme_item_list() to support nested items.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

I think that the admin/sitemap page has logic for ouputting an unordered list from an array of items with depth attribute. This logic could be re-used for this feature request.

JonBob’s picture

The sitemap feature uses the heirarchy of the menu data to build the lists, not a depth attribute. Which is preferable? In the former case, we need to specify the data structure that the theme function would take (perhaps that output by taxonomy_get_tree?); in the latter, we need to pass depths along as well. When I needed this in a module before, I followed the second approach. The module added:

function theme_hierarchical_list ($items, $depths, $title = NULL)
{
  $output = "";

  if (isset($title)) {
    $output .= "<b>$title</b><br />";
  }

  if (isset($items)) {
    for ($i = 0; $i < count ($items); $i++) {
      $output .= _taxonomy_depth ($depths[$i] + 1, '-&nbsp;');
      $output .= $items[$i] . "<br />";
    }
  }

  return $output;
}

This was before the full transition to CSS; I overrode this in themes to provide a CSS-based solution anyway.

moshe weitzman’s picture

Title: Support arbitrary nesting in theme_item_list() » Output an unordered list with arbitrary nesting in theme_item_list()
Component: themes » theme system

Thanks JohnBob. However, the desired output is for an HTML unordered list, whereas your code produces a list using spaces as a'visual depth indicator'. I've updated the title of this request so this is clearer.

moshe weitzman’s picture

Boris Mann submitted a brief patch which enables this. Please consider incorporating his patch into CVS.

I think Boris' approach is better than expecting a list as currently produced by taxonomy API (i.e. the 'depth' parameter).

moshe weitzman’s picture

Kjartan’s picture

The patch isn't prefect though. There are a few minor coding standard issues that need to be cleaned up, and it has to call theme('item_list') to make sure everything is styled properly. Also I am not sure of the array structure, is that really necessary?

moshe weitzman’s picture

Assigned: Unassigned » moshe weitzman

Here is an update of Boris' patch which applies cleanly to HEAD. Also, nested lists are properly themed and a few double quotes changed to single quotes.

You may test this patch with a custom php block containing:


$items1 = array("apple","banana", "orange");
$items1a['list'] = $items1;
$items1a['type'] = "ol";

$items2 = array("pork","beef",$items1a,"chicken","fish");

$output = theme('item_list', $items1, "Ordered List", "ol");
$output .= theme('item_list', $items2, "Nested", "ul");
return $output;

Again, this will benefit the contributed taxonomy modules, weblinks.module and probably several others.

moshe weitzman’s picture

FileSize
1.05 KB

this time with patch attached.

Dries’s picture

The patch does not apply.

Also, Kjartan pointed out that $item["list"] might not be initialized. I suggest we use $list_type instead. For sake of consistency, $theme["list"] should probably be theme["items"]. Lastly, the patch should be updated to respect Drupal's coding style.

moshe weitzman’s picture

moshe weitzman’s picture

Assigned: moshe weitzman » Unassigned

anyone interested in writing this? would help clean up some contrib modules.

moshe weitzman’s picture

Status: Active » Needs work
David Lesieur’s picture

Status: Needs work » Needs review
FileSize
1.16 KB

Here is my attempt at this. It will be useful for this issue (and maybe others).

moshe weitzman’s picture

Status: Needs review » Needs work

does not apply anymore and theme_item_list() has changed slightly. please resubmit if possible.

David Lesieur’s picture

Status: Needs work » Needs review
FileSize
1.67 KB

Here is an updated patch.

mathieu’s picture

To the best of my knowledge, this patch respects the coding style and is simple enough. I just tested it out on drupal head and it still applies, with a -42 lines offset though.

+1.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

thanks mathieu.

drumm’s picture

Status: Reviewed & tested by the community » Needs work
+      if ($children) {

needs to be more explicit. How about count($children) > 0?

David Lesieur’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
1.68 KB

New patch following drumm's comment. Since that was the only comment, I'm putting this back to its previous status "ready to be commited".

drumm’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD.

Anonymous’s picture

Status: Fixed » Closed (fixed)