Skip to content

Instantly share code, notes, and snippets.

@opi
Created February 6, 2013 15:19
Show Gist options
  • Select an option

  • Save opi/4723213 to your computer and use it in GitHub Desktop.

Select an option

Save opi/4723213 to your computer and use it in GitHub Desktop.
Drupal: Mimic menu_attributes behavior to add a "CSS classes" textfield to menu item form.
<?php
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds menu 'class' attribute options to the edit menu item form.
*/
function mymodule_form_menu_edit_item_alter(&$form, $form_state) {
// Get item
$item = $form['original_item']['#value'];
// Get class default value
if (isset($item['options']['attributes']) && isset($item['options']['attributes']['class'])) {
$default_value = $item['options']['attributes']['class'];
}
else {$default_value = '';}
// Change 'options' type
$form['options'] = array(
'#type' => 'item',
'#tree' => TRUE,
'#weight' => 50
);
// Create attributes item
$form['options']['attributes'] = array('#type' => 'item', '#tree' => TRUE);
// Add class textfield & item
$form['options']['attributes']['class'] = array(
'#type' => 'textfield',
'#title' => t("CSS classes"),
'#default_value' => $default_value,
'#description' => t("Enter CSS classes, separated by spaces.")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment