Show Menu Description Field in Node Add/Edit Form
Many components in Drupal have an optional title or description field. This text usually displays when a user hovers over an item. We had an interesting request: a client wanted to have a box that displayed text which would change every time you roll over a menu item. This can be accomplished using just a small amount of jQuery, and Drupal’s built-in menu handling. The menu system in Drupal can handle descriptions on each menu item, however you have to do this through the menu admin. The problem is that the client needed to be able to edit these descriptions at will.
We’ll use Drupal’s hook_form_alter() function to add the node’s menu description field (really the title attribute) right into the node’s add/edit form. We must also use the node_form_submit() function to submit the value.
Unsure how to use this code, or where to put it? Perhaps you need to look at Creating a Custom Module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/** * Implementation of hook_form_alter() */ function mymodule_form_alter(&$form, $form_state, $form_id) { if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) { if (isset($form['menu']['options']['#value']['attributes']['title'])) { $defaultVal = $form['menu']['options']['#value']['attributes']['title']; } else { $defaultVal = ''; } $form['menu']['description'] = array( '#type' => 'textarea', '#title' => t('Description'), '#default_value' => $defaultVal, '#rows' => 1, '#description' => t('The description displayed when hovering over a menu item.'), ); $form['#submit'][] = 'mymodule_node_form_submit'; } } /** * Implementation of node_form_submit() */ 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']; } |