User login

Adding non-menu things to primary links menu items with Drupal 7's page alter hook

The request was to stick search in a menu. I said "I bet that's easy with hook_page_alter()!"

Not quite.

The primary links menu is output directly by the theme, and so is one of the very very few things that occurs after hook_page_alter().

You can tell the theme to stop outputting it directly, and put it in a block that is displayed in a region, or you can add it to the page rendering array in code with hook_page_build().

When exposed this way, as in the usual block system, menu links do in fact show up as wonderful, discrete, modifiable elements that you can stick your own elements of your own into- or drag in the search form to have it rendered there, if you really insist.

Also, when using Devel's kpr() to print something (including when checking off show page alter), Command+F search in your browser does not see text in collapsed elements. At least in Firefox 6. Not sure why, but be warned.

Searched words: 
drupal 7 page alteration altering menu links to add a search form main menu

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.