When to use hook_menu vs hook_menu_alter??

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
wipeout_dude's picture

Hi,

Can someone tell me when its correct to use hook_menu vs hook_menu_alter??

Quite simply I am wanting to create a local action on a view to add content.. In the past I have used Page Manager to do this but looking at the code it doesn't seem logical to load a whole module when this is all I want to do.. I am just not sure on the correct way to do it..

Any help appreciated..

Thanks..

Comments

Well, hook menu is used when

vaibhavjain's picture

Well, hook menu is used when we create or define a new menu item or callback. if we want to alter any existing menu item, without hacking the core or contributed modules, we use hook menu alter.

Vaibhav Jain

Great, so really its hook

wipeout_dude's picture

Great, so really its hook menu I am after because I am creating a new MENU_LOCAL_ACTION??

Thanks..

General Note

greta_drupal's picture

This might not apply to your project, but I would like to point out as a matter of education for others (e.g., other "newbies"):

Even if you are the best damn coder in Dodge, if you are building projects to be used by clients (non-developers), you should be using available contributed modules. Hard-wiring things into code and even creating custom modules (where contributed modules exist), hurts the client. And, doing it intentionally to make oneself "indispensable" to the client is unconsciounable, IMO.

I have had many clients come to me when their web developer abandoned the project -- even in mid-production, and even days before scheduled launch.

One should always make a Drupal site as easy as possible to maintain -- for the non-tech client as well as future web admins or developers. (This also means not hosting on a VPS or dedicated server if a shared hosting account will do.)

I absolutely agree on all

wipeout_dude's picture

I absolutely agree on all your points..

This requirement I have is simply to add a "+ Add new page" menu link at the top of pages of a specific content type.. It seems a bit of an overkill to load the whole Page Manager module for about 5 lines of code..

At the same time I think you are probably right that having it maintainable from a module UI is probably worth the overhead of loading the additional module which will mostly likely come in useful for other features on the site..

Plus there are contrib mods

greta_drupal's picture

Plus there are contrib mods to do that. Check out one of my favs (standards) to pretty much that: http://drupal.org/project/addanother . It is configurable per content type -- on the content type (edit) form, you will have the choice to enable.

Or, you could create a custom node template file for that content type. But, again, keeping it simple for the client to maintain and administer, use a module -- such as the above.

I think because of your exuberance, you are unnecessarily overcomplicating your project. Even if it is just for you, at this point, it is a bad habit to start. I think it is great that you understand how that would be coded. But building in Drupal isn't about coding. Coding should be left to module developers, else you defeat the purpose (e.g., flexibility, scalability) of Drupal legos.

Programmers are probably the worst site builders because they are just predisposed to code from scratch. They seldom default to client UI.

Another related example: I just posted an installation issue in a contrib module queue. The developer responded, 'I only use Drush, so I wouldn't know anything about that.' Well, ya know, he should. My barely-web-savvy clients sure as hell aren't going to use Drush. But, they certainly can check and box and click and UPDATE button (D7) to maintain their modules.

The moral of the story, as you get started in Drupal, is to keep useability foremost in your mind.

Now back to your regularly scheduled program.

NOTE: Edited to evidence that English is my first language, and amended...just because.