Menu description field in node/add form

A simple solution how to add a description field to your node/add forms.

What is that for?

We are building a website that has landing pages. Structure could be like:

  • About Us (landing page)
    • Who we are?
    • What we do?
  • Locations (landing page)
    • United Kingdom
    • Ireland

The site structure is based on a menu, thus each page has its menu and most importantly parent menu item if it is not landing page. When visitor is viewing a landing page, the page consists of a text from landing page node and a list of first level sub-menu items with its description (views can't do this afaik).

Here is the landing page layout:

landing page

The only problem is, that in node/add form menu fieldset doesn't have description field. So user adding or editing a new content has to after content creation visit menu administration, select the menu item and edit description. Not a good workflow eh? :)

So first we need to use form_alter to add a description field to a menu fieldset:

<?php
/**
* Implementation of hook_form_alter(). Adds menu item fields to the node form.
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if (isset(
$form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
   
$form['menu']['description'] = array(
     
'#type' => 'textarea',
     
'#title' => t('Description'),
     
'#default_value' => isset($form['menu']['options']['#value']['attributes']['title']) ? $form['menu']['options']['#value']['attributes']['title'] : '',
     
'#rows' => 1,
     
'#description' => t('The description displayed when hovering over a menu item.'),
    );
   
$form['#submit'][] = 'mymodule_node_form_submit';
  }
}
?>

then, as you can see in the code above, we have our own form submit callback. Let's add that too:

<?php
function mymodule_node_form_submit($form, &$form_state) {
 
$form_state['values']['menu']['customized'] = TRUE;
 
$form_state['values']['menu']['options']['attributes']['title'] = $form_state['values']['menu']['description'];
}
?>

And that's it. Now in node/add form we have in menu fieldset a description field too. :)




Need Drupal Theming? Hire us