Created
June 5, 2015 19:20
-
-
Save manumilou/ac5d71a1688d5f3ed5b1 to your computer and use it in GitHub Desktop.
Add an option to a menu item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_form_FORM_ID_alter(). | |
* | |
* @ingroup forms | |
* @see pp_menu_customization_form_menu_edit_item_submit() | |
*/ | |
function pp_menu_customization_form_menu_edit_item_alter(&$form, $form_state, $form_id) { | |
// Add an option to open the link in a new window/tab | |
$form['target'] = array( | |
'#type' => 'select', | |
'#title' => 'Cible (target)', | |
'#options' => array( | |
'' => '<non défini>', | |
'_blank' => 'Nouvelle fenêtre', | |
), | |
'#default_value' => isset($form['options']['#value']['attributes']['target']) ? $form['options']['#value']['attributes']['target'] : '', | |
); | |
// Put our custom submit before the default one so that our changes are saved | |
array_unshift($form['#submit'], 'pp_menu_customization_form_menu_edit_item_submit'); | |
} | |
function pp_menu_customization_form_menu_edit_item_submit($form, &$form_state) { | |
// Set the target attribute if we have a value, or remove it if we don't | |
if (!empty($form_state['values']['target'])) { | |
$form_state['values']['options']['attributes']['target'] = $form_state['values']['target']; | |
} | |
else { | |
unset($form_state['values']['options']['attributes']['target']); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment