APi fixes for menu_tree_data() including depth param

catch - July 3, 2009 - 18:03
Project:Drupal
Version:7.x-dev
Component:menu system
Category:task
Priority:normal
Assigned:Unassigned
Status:needs work
Issue tags:Needs Documentation
Description

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.

#1

pwolanin - August 5, 2009 - 01:53
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.

#2

pwolanin - August 5, 2009 - 02:44
Status:active» needs review

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-2.patch5.72 KBIdleFailed: Failed to run tests.View details | Re-test

#3

pwolanin - August 5, 2009 - 03:23

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-2.patch5.72 KBIdleFailed: Failed to run tests.View details | Re-test

#4

System Message - August 5, 2009 - 04:00
Status:needs review» needs work

The last submitted patch failed testing.

#5

Damien Tournoud - August 5, 2009 - 07:47

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

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

#6

pwolanin - August 5, 2009 - 13:00

@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.

#7

Damien Tournoud - August 5, 2009 - 13:03

Why not:

<?php
foreach ($result as $key => $item) {
  unset(
$result[$key]);
 
// ...
}
?>

#8

pwolanin - August 5, 2009 - 15:31

@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?

#9

pwolanin - August 10, 2009 - 04:08
Status:needs work» needs review

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-9.patch8.56 KBIdleFailed: Failed to apply patch.View details | Re-test

#10

System Message - August 10, 2009 - 04:17
Status:needs review» needs work

The last submitted patch failed testing.

#11

pwolanin - August 10, 2009 - 13:10

oops - forgot to use --no-prefix

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-11.patch8.55 KBIdleFailed: 12025 passes, 40 fails, 88 exceptionsView details | Re-test

#12

pwolanin - August 10, 2009 - 13:11
Status:needs work» needs review

#13

System Message - August 10, 2009 - 13:28
Status:needs review» needs work

The last submitted patch failed testing.

#14

pwolanin - August 16, 2009 - 19:48
Status:needs work» needs review

fixed book and menu module.

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-14.patch9.57 KBIdleFailed: 12113 passes, 1 fail, 0 exceptionsView details | Re-test

#15

System Message - August 16, 2009 - 20:00
Status:needs review» needs work

The last submitted patch failed testing.

#16

catch - August 16, 2009 - 21:20

+++ 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.

#17

pwolanin - August 19, 2009 - 16:18
Status:needs work» needs review

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-17.patch14.16 KBIdleFailed: Failed to apply patch.View details | Re-test

#18

pwolanin - August 19, 2009 - 18:38

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-18.patch13.94 KBIdleFailed: Failed to apply patch.View details | Re-test

#19

sun - August 19, 2009 - 19:29

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)

#20

pwolanin - August 19, 2009 - 19:34

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-19.patch14.31 KBIdleFailed: 13069 passes, 15 fails, 4 exceptionsView details | Re-test

#21

pwolanin - August 19, 2009 - 19:37

slight additional clarification

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-20.patch14.35 KBIdleFailed: Failed to apply patch.View details | Re-test

#22

chx - August 19, 2009 - 19:50
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 :)

#23

System Message - August 20, 2009 - 07:38
Status:reviewed & tested by the community» needs work

The last submitted patch failed testing.

#24

webchick - August 20, 2009 - 07:42
Status:needs work» reviewed & tested by the community

Oh, testing bot...

#25

Dries - August 20, 2009 - 10:55

<?php
$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?

#26

pwolanin - August 20, 2009 - 12:27

@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.

#27

System Message - August 23, 2009 - 09:40
Status:reviewed & tested by the community» needs work

The last submitted patch failed testing.

#28

pwolanin - August 23, 2009 - 12:57
Status:needs work» reviewed & tested by the community

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-28.patch14.37 KBIdlePassed: 12401 passes, 0 fails, 0 exceptionsView details | Re-test

#29

pwolanin - August 23, 2009 - 13:31

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

AttachmentSizeStatusTest resultOperations
menu-api-fixes-509584-29.patch15.05 KBIdlePassed: 12415 passes, 0 fails, 0 exceptionsView details | Re-test

#30

webchick - August 24, 2009 - 01:51
Status:reviewed & tested by the community» needs work

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.

 
 

Drupal is a registered trademark of Dries Buytaert.