History: (see: http://drupal.org/node/14120 and the discussion that followed for the full history)

A while back Steven Dondley (aka nysus) proposed some changes to the book.module so that it would create individual blocks for individual books. It was agreed that the suggested functionality was desirable, but not the solution provided.

The preferred solution would have been to have the book.module use the menu system to create the individual blocks for the individual books.

I wrote some code to test the idea using the MENU_CUSTOM_MENU and MENU_CUSTOM_ITEM menu types as a proof of concept. It worked. However, I didn't think it would be desirable for the book.module to be creating menus of this type, because these menus are not (by any definition) custom. I had suggested that some new menu types be added to menu.inc - something along the lines of MENU_BOOK_MENU and MENU_BOOK_ITEM. This idea was not well received because it was felt that it would open the door to all module developers to ask for their own menu type. Still, it seemed that a more generic menu type that any module could use might be acceptable.

Proposal: (see patch for full details)

As an alternative I would now suggest that two new menu type flags be created, along with two new menu types.

define('MENU_MODIFIED_BY_MODULE', 0x0400);
define('MENU_CREATED_BY_MODULE', 0x0800);
/*
*....
*/
define('MENU_MODULE_MENU', MENU_IS_ROOT | MENU_VISIBLE_IN_TREE | MENU_CREATED_BY_MODULE);
define('MENU_MODULE_ITEM', MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB | MENU_CREATED_BY_MODULE);

note there are also two other minor changes in the file to handle the inclusion of these menu types in the $_Menu array for display

In the case of the book.module, I wrote some code that would essentially replace the verbose block creation in the module by instead building menus (stored in the menu table) that maintained the book hierarchies using these new menu types. The result was the book.module automatically updated the menu system to display changes in the book hierarchy. And since the block module already has a mechanism for showing 'custom' menus as blocks, each book (and its associated menu) automatically had their own block created as well. (This is a separate patch and discussion, but I bring it up to show why the change might be desirable in the menu.inc file - and indeed necessary for me to submit the book.module patch).

The second proposal would be to add a 'module' field to the menu table. In the case of the book.module, prior to inserting the updated book hierarchy into the menu table, I delete the existing hierarchy. If separate modules are going to add/edit/delete their own menus in the menu table, it isn't enough to know that they are of type MENU_MODULE_MENU, but you would also need to know which module created them so that each module could only change its own menus and not ALL module menus. Essentially I am proposing the same thing that already exists in the block table.

The proposed changes would in no way affect how drupal works, it would just allow added flexibility for modules to create their own menus using the menu system.

I would appreciate any feedback and am happy to entertain questions regarding this proposal. I will submit an official feature request along with the patch if feedback is positive, (along with patches to the book module, database and update.inc).

Thanks
andre

Comments

boris mann’s picture

Without deep inner knowledge of the mysteries of menu.module, this approach sounds good.

I want to point out the potential use of this for primary/secondary links/menu by PHPTemplate. This requires an additional change to menu to allow for full http links as opposed to only Drupal-relative paths. Title attributes could be stored separately (since mainly of interest for those main links), but might also be considered for menu.

dries’s picture

If separate modules are going to add/edit/delete their own menus in the menu table, it isn't enough to know that they are of type MENU_MODULE_MENU, but you would also need to know which module created them so that each module could only change its own menus and not ALL module menus.

Doesn't the book module know what menu items it added? I'm not fully convinced this extra column is needed.

The PHPdoc comments explaining the defines are rather brief. I'd like to see a short description of their behavior so I know _when_ to use these defines (or when not to use them). Right now, it takes some guess work.

Other than that, the patch looks clean.

andremolnar’s picture

Thanks for the feedback.

Doesn't the book module know what menu items it added?

Book.module could be made aware of what menus it created by checking its own node hierarchy against what is in the menu table, but I thought it would be far more efficient to do a single DELETE WHERE module='book' AND type=module and a single multi-row insert operation. 2 lines of code vs. 15 - and only one traverse of its hierarchy to build the insert vs. a comparison between the two hierarchies and then still having to build the insert/update.

I suppose this will make more sense when I submit my book patch.

I will go ahead and update the PHPdoc comments to try and make it a little more clear. Once again - seeing it in action with the book patch might make it a little more clear.

andre

media girl’s picture

Doesn't this kind of echo what is already possible with the book navigation block? I know it isn't visible all the time, but I would think that could be changed rather easily, if desired. Maybe I'm misunderstanding you, but what I feel like I have what you're describing already.

But I can see how the change to the menu table might be handy for future flexibility for development. Is this something that would dovetail with menu on the fly?

--
mediagirl.org

andremolnar’s picture

The changes to book module would be so that each 'book' created with book module would have its own menu block that administrators could show or hide anywhere in the site regardless of whether a person was viewing a book page. The idea is to give administrators more control over individual books and when and where they show up- and to give them the option of creating dynamic menu systems based on the book module.

As for menu on the fly - I haven't looked at it, so I cannot comment on how this would fit in. But, since this patch to menu.inc wouldn't change how the menu system works, I can't see it hurting different development projects.

andre

boris mann’s picture

The "dynamic menu systems based on the book module" is the big one -- this allows one to use book module to put content on a site into sections, and get automatic primary and secondary navigation.

andremolnar’s picture

This change woulnd't do that directly (i.e. handle primary and secondary links), but would allow a small custom module that works with the menu system to create those links. It would be very simple - and not just limited to menus generated by book, but any module that creates menus.

Which incidently would be another reason to include the module field in the menu table. Say you wanted to generate primary and secondary links based on menus created by one, some, or all modules - you would need only write a query like SELECT FROM {menu} WHERE type=MENU_MODULE_MENU and (module='whatevermodule' or module='someothermodule').

andre

tangent’s picture

I would also like to see this happen if it would allow dynamic menus to be built using taxonomy vocabularies. Existing modules create their own tree structures since there seems to be no way to leverage menu.module to do this.

Perhaps this is the same thing everyone else wants though. I don't know how book nodes are structured but if it is taxonomy based then more than just book.module would benefit.

Actually, taxonomy_menu.module is able to leverage menus but only so long as the menu is displayed in the navigation block and this is far too limiting.

andremolnar’s picture

I have uploaded a patch for book: http://drupal.org/node/14120 (scroll to the bottom - includes a full description) - Makes use of this menu change.

Comments welcome.

re: taxonomy menus - the same sort of approach i used in book could be applied to taxonomy - there someone could write code to either create a full menu including links to nodes (which could be too large for practicle purposes) - or just the taxonomy terms' hierarchy - updated when taxonomy changes.

andre