Splitting off from #484820: Initial D7UX admin header

You currently can't get a slice of a menu down to a certain depth, only all items below a certain point in the tree - it might make sense to add a $depth param to menu_tree_all_data() to just be able to get certain levels of the tree at any one time.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pwolanin’s picture

Title: Add $depth parameter to menu_tree_all_data() » APi fixes for menu_tree_data() including depth param

We shoudl pull in some of the straigh menu APi fixes from #483614: Better breadcrumbs for callbacks, dynamic paths, tabs to make that issue simpler to deal with.

pwolanin’s picture

Status: Active » Needs review
FileSize
5.72 KB

first pass here - passing an array into function menu_tree_data() since that makes the functions more re-usable

pwolanin’s picture

first pass here - passing an array into function menu_tree_data() since that makes the functions more re-usable

Status: Needs review » Needs work

The last submitted patch failed testing.

Damien Tournoud’s picture

+  $result = array_reverse($result);
[...]
+  while ($item = array_pop($result)) {
+  }

I suggest walking the array in the normal order and using unset().

pwolanin’s picture

@Damien - but pop is such a nice function name and does just what we want :)

I'm really not such how do do what you suggest in as simple a way. To me this seems ugly and much harder to understand by comparison:

while ($data = each($result)) {
unset($result[$data[['key']);
$item = $data['value'];
}

I chose a reverse and pop because array_unshift() is a rather more expensive operation than pop.

Damien Tournoud’s picture

Why not:

foreach ($result as $key => $item) {
  unset($result[$key]);
  // ...
}
pwolanin’s picture

@Damien - still rather ugly imho - in either case we need to pass the array by reference when we do the recursion. What happens when we come out of the recursive call and we have unset the the array element that the current foreach loop thinks is the current element? I hope PHP handles that gracefully?

pwolanin’s picture

Status: Needs work » Needs review
FileSize
8.56 KB

A better pass - allows for radicial simplification of _menu_tree_data()

Status: Needs review » Needs work

The last submitted patch failed testing.

pwolanin’s picture

oops - forgot to use --no-prefix

pwolanin’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch failed testing.

pwolanin’s picture

Status: Needs work » Needs review
FileSize
9.57 KB

fixed book and menu module.

Status: Needs review » Needs work

The last submitted patch failed testing.

catch’s picture

+++ includes/menu.inc
@@ -843,13 +844,16 @@ function menu_tree_output($tree) {
+function menu_tree_all_data($menu_name, $item = NULL, $max_depth = NULL) {

Missing documentation for the $max_depth param.

+++ includes/menu.inc
@@ -939,13 +951,16 @@ function menu_tree_all_data($menu_name, $item = NULL) {
+function menu_tree_page_data($menu_name, $max_depth = NULL) {

Also missing @param documentation.

Otherwise looks good apart from that one fail, I don't fully get all the changes here, especially not this evening, but the gnarliest bit is at least a lot less gnarly than what's in there now.

This review is powered by Dreditor.

pwolanin’s picture

Status: Needs work » Needs review
FileSize
14.16 KB

I think the problem was that book_menu_subtree_data() had a bug- failed to rename a variable.

pwolanin’s picture

remove unneeded $result->setFetchMode() calls, fix comment

sun’s picture

If those menu tree data functions would additionally allow to specify a starting menu link instead of a menu name, that would heavily trivialize admin_menu as well as http://drupal.org/project/menu_block (which contains some raw menu_*() forks that should be in core)

pwolanin’s picture

more feedback from chx, leading to improved and expanded code comments to clarify the algorithm.

pwolanin’s picture

slight additional clarification

chx’s picture

Status: Needs review » Reviewed & tested by the community

Very nice code cleanup! I am surprised this could be simplified so much. I got a headache when coded the original, however, it's not an easy thing, this tree traversal because of the way it is. Some time and a "new" coder helped it :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch failed testing.

webchick’s picture

Status: Needs work » Reviewed & tested by the community

Oh, testing bot...

Dries’s picture

+  $links = array();
+  foreach ($result as $item) {
+    $links[] = $item;
+  }
+  $tree = menu_tree_data($links);

What am I missing here? It seems like we're just making a copy of $result?

pwolanin’s picture

@Dries - $reslt is a query result object that implements the Iterator interface so we can call foreach() one it (but not other array functions). By doing the foreach first, we allow the menu_tree_data() function to take a more generic array as a parameter.

Status: Reviewed & tested by the community » Needs work

The last submitted patch failed testing.

pwolanin’s picture

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

fixed a very minor 1-line merge conflict in book.test (where I am fixing a typo in the test that was failing above).

pwolanin’s picture

minor doxygen changes per Dries and added type hinting to the arguments of menu_tree_data()

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs documentation

I think this PHPDoc is sufficient. While I too found this confusing, it's not the job of menu API code to document DBTNG, and the docs that talk about a query result object are enough of a breadcrumb for me to go off searching if I need to for further clarification.

This looks like a nice clean-up to make the menu API more comprehensible to mere mortals. :)

Committed to HEAD. Needs updating in the documentation.

samuelsov’s picture

Status: Needs work » Fixed
Issue tags: -Needs documentation

menu_tree_data is the only function which needs a conversion between Drupal 6 and Drupal 7 because the depth param is optional.
Documented in http://drupal.org/update/modules/6/7#menu_tree_data

#d7csmtl

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.