Toolbar module (and others) should properly build its menu slice

Damien Tournoud - November 2, 2009 - 02:45
Project:Drupal
Version:7.x-dev
Component:menu system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Issue tags:D7 API clean-up
Description

The toolbar module has been implemented with insufficient knowledge and understanding of how the menu system works. The current code is ugly and very inefficient (both in term of processing power and in term of memory usage), introduces some code duplication and a maintenance hell.

We need to fix that. Now.

This issue is the first step: introduce a clean way to build a slice of a menu.

#1

Damien Tournoud - November 2, 2009 - 02:48
Status:active» needs review

This patch:

- factor out the common tree-building logic of menu_tree_all_data() and menu_tree_page_data() into a menu_build_tree() function
- uses this new function in the toolbar module

AttachmentSizeStatusTest resultOperations
620618-toolbar-menu-slice.patch15.41 KBIdleFailed: 14441 passes, 56 fails, 22 exceptionsView details | Re-test

#2

System Message - November 2, 2009 - 03:05
Status:needs review» needs work

The last submitted patch failed testing.

#3

makara - November 2, 2009 - 10:54

<?php
cache_clear_all
('links:' . $menu_name . ':', 'cache_menu', TRUE);
?>

in function
<?php
menu_cache_clear
()
?>

either remove the ':', or edit your cache cid.

#4

Damien Tournoud - November 2, 2009 - 12:00
Status:needs work» needs review

@Makara: you are the best.

Same patch, with a simpler, and better tree cache ID.

AttachmentSizeStatusTest resultOperations
620618-toolbar-menu-slice.patch14.99 KBIdleFailed: 14497 passes, 197 fails, 37 exceptionsView details | Re-test

#5

System Message - November 2, 2009 - 12:10
Status:needs review» needs work

The last submitted patch failed testing.

#6

sun - November 2, 2009 - 20:31

This is a very interesting alpha-quality patch, which actually maps nicely to what I outlined in #618530: System module's "listing" pages (and blocks) should use menu_build_tree().

#7

sun - November 2, 2009 - 23:51

+++ modules/toolbar/toolbar.module
@@ -134,12 +134,13 @@ function toolbar_get_menu_tree() {
   $admin_link = db_query("SELECT * FROM {menu_links} WHERE menu_name = 'management' AND module = 'system' AND link_path = 'admin'")->fetchAssoc();
...
+    $data = menu_build_tree('management', array(
+      'parents' => array($admin_link['mlid']),
+      'min_depth' => 2,
+      'max_depth' => 2,
+    ));

Instead of a hard-coded 2, we want to use $admin_link['depth'] here. :)

+++ modules/toolbar/toolbar.module
@@ -134,12 +134,13 @@ function toolbar_get_menu_tree() {
+    menu_tree_check_access($data['tree'], $data['node_links']);
+    $tree = $data['tree'];

Do we really want to put this into the caller's responsibility?

How about going with this menu_build_tree(), but perhaps make it private, i.e. _menu_build_tree(), and make menu_build_tree() invoke _menu_build_tree() + additionally invoke menu_tree_check_access() and return the resulting tree data? :)

This review is powered by Dreditor.

#8

makara - November 3, 2009 - 03:46

now the cid is too long...

#9

sun - November 5, 2009 - 18:53
Title:The toolbar module should properly build its menu slice» Toolbar module (and others) should properly build its menu slice
Component:base system» menu system
Status:needs work» needs review

This should work.

AttachmentSizeStatusTest resultOperations
drupal.menu-build-tree.8.patch16.15 KBIdleFailed: Invalid PHP syntax in modules/toolbar/toolbar.module.View details | Re-test

#10

sun - November 5, 2009 - 18:55

Sorry, left a stale parameter in there.

AttachmentSizeStatusTest resultOperations
drupal.menu-build-tree.9.patch16.09 KBIdlePassed: 14749 passes, 0 fails, 0 exceptionsView details | Re-test

#11

sun - November 5, 2009 - 20:45

So what's left here?

This works beautifully. Aside from that:

1) A bit wonky is the "parents" tree parameter (list). When would I want to pass more than one plid? (Though we can as well ignore that, because it equals the current code.)

2) uhm, nothing. I'll check whether we could use this cool thingy also for contextual links now ;) (separate issue)

#12

Damien Tournoud - November 5, 2009 - 20:52

+++ includes/menu.inc 5 Nov 2009 18:54:56 -0000
@@ -1212,10 +1131,115 @@ function menu_tree_page_data($menu_name,
+  $tree_cid = 'links:' . $menu_name . ':tree-data:' . $GLOBALS['language_interface']->language . ':' . md5(serialize($parameters));

I'm not comfortable with this. There is no guarantee that the tree ID is unique for a set of parameters. The risk of conflict is small, but real.

I'm on crack. Are you, too?

#13

sun - November 6, 2009 - 00:23

That, I don't get. How can the tree be different for the same parameters?

Access checking happens later only, so I really don't see how this would be possible.

I also don't see a ->random() in there. ;)

It would make me really worry about this function. The result of a function should be the same under the same conditions. I'd consider it a critical bug if a function doesn't return the same for the same arguments.

So can you clarify?

#14

sun - November 6, 2009 - 17:54

Tagging (and updating the other issue accordingly)

#15

sun - November 7, 2009 - 17:05

Can we proceed here?

In the meantime, I looked into whether this function could be used for contextual links as well. It probably could be, if the menu system would store a proper plid, depth, and pX columns for dynamic paths/local tasks... which it is currently not. That's surely a separate issue (perhaps related to the existing #550254: Menu links are sometimes not properly re-parented), but this patch would be a major step towards a better solution.

#16

Damien Tournoud - November 7, 2009 - 17:07

That, I don't get. How can the tree be different for the same parameters?

That's not what I'm saying. I'm saying that md5(serialize($parameters)) can be the same for several $parameters. But it's not probably that common, given that $parameters is typically small.

#17

sun - November 7, 2009 - 20:34

Fixed inline comments.

AttachmentSizeStatusTest resultOperations
drupal.menu-build-tree.17.patch16.47 KBIdlePassed: 14696 passes, 0 fails, 0 exceptionsView details | Re-test

#18

sun - November 7, 2009 - 20:50

In $tree_parameters,

- renamed 'parents' to 'expanded'

- renamed 'active_link' to 'active_trail'

because I think that's much more understandable for people who want to use this tree builder.

AttachmentSizeStatusTest resultOperations
drupal.menu-build-tree.18.patch16.5 KBIdleFailed: Invalid PHP syntax in modules/toolbar/toolbar.module.View details | Re-test

#19

System Message - November 7, 2009 - 21:00
Status:needs review» needs work

The last submitted patch failed testing.

#20

sun - November 7, 2009 - 21:38
Status:needs work» needs review

Test slave #44 produces non-reliable results.

AttachmentSizeStatusTest resultOperations
drupal.menu-build-tree.20.patch16.5 KBIdlePassed: 14701 passes, 0 fails, 0 exceptionsView details | Re-test

#21

chx - November 8, 2009 - 07:14
Status:needs review» reviewed & tested by the community

I RTBC this on the condition that in a followup patch we will review how menu build tree and menu tree data could be merged.

#22

Dries - November 8, 2009 - 09:23

I think this is a great clean-up that should be committed.

One comment though: in the original description, DamZ complains about unnecessary duplication but this patch adds more lines than it removes ... Also, in the original description, DamZ complains that the original code is inefficient (both in term of processing power and in term of memory usage), but I don't see performance numbers on the new code? Do we want to test whether it is actually faster? (It should be.)

#23

webchick - November 8, 2009 - 09:58
Status:reviewed & tested by the community» needs review

That sounds like a 'needs review' to me. :)

 
 

Drupal is a registered trademark of Dries Buytaert.