Here is some code to add a dropdown login/register form to navigation menu.
Inspiration came from http://mifsud.me/adding-dropdown-login-form-bootstraps-navbar
It won't be part of Bootstrap theme but it can help people who need this ;)
This code is part of my custom bootstrap_config module.
/**
* Implements hook_block_info().
*/
function bootstrap_config_block_info() {
$blocks = array();
$blocks['bootstrap-login'] = array(
'info' => t('Bootstrap Login/Register Menu'),
'status' => TRUE,
'region' => 'navigation',
'visibility' => BLOCK_VISIBILITY_NOTLISTED,
'pages' => '',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function bootstrap_config_block_view($delta = '') {
switch ($delta) {
case 'bootstrap-login':
return user_is_logged_in() ? NULL : _bootstrap_config_login_block_view();
}
return NULL;
}
function _bootstrap_config_login_block_view() {
$content = '<ul class="nav pull-right">';
$content .= '<li class="dropdown">';
$content .= '<a class="dropdown-toggle" href="#" data-toggle="dropdown">' . t('Sign in') . ' <strong class="caret"></strong></a>';
$content .= '<div class="dropdown-menu embedded-form">' . drupal_render(drupal_get_form('user_login')) . '</div>';
$content .= '</li>';
if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
$content .= '<li class="dropdown">';
$content .= '<a class="dropdown-toggle" href="#" data-toggle="dropdown">' . t('Register') . ' <strong class="caret"></strong></a>';
$content .= '<div class="dropdown-menu embedded-form">' . drupal_render(drupal_get_form('user_register_form')) . '</div>';
$content .= '</li>';
}
$content .= '</ul>';
return array(
'subject' => NULL,
'content' => $content,
);
}
/**
* Implements hook_form_alter().
*/
function bootstrap_config_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_login':
case 'user_register_form':
// Add ajax wrapper to form.
$html_id = str_replace('_', '-', $form_id);
$form['#prefix'] = '<div id="ajax-' . $html_id . '-wrapper">';
$form['#suffix'] = '</div>';
// Add ajax functionality to form submit button.
$form['actions']['submit']['#ajax'] = array(
'callback' => '_bootstrap_config_ajax_callback',
'wrapper' => 'ajax-' . $html_id . '-wrapper',
'event' => 'click',
);
break;
}
}
function _bootstrap_config_ajax_callback($form, $form_state) {
ctools_include('ajax');
ctools_add_js('ajax-responder');
if (form_get_errors()) {
return $form; // Reload form if it didn't pass validation.
}
if ($form['#id'] === 'user-login') {
drupal_set_message(check_plain('Successful login'));
}
$commands = array();
$commands[] = ctools_ajax_command_reload();
return array(
'#type' => 'ajax',
'#commands' => $commands
);
}
Comments
Comment #1
frankbaele commentedthx
Comment #2
mrupsidownThanks!
Comment #3
TheJoker commentedComment #4
woop_light commentedSeems pretty cool -- where should I add this code?
Comment #5
TheJoker commentedMissed a comma in the code on line '# commands' => $ commands,
Comment #6
christian le fournis commentedGreat module, need to fix the following error:
Strict warning: Only variables should be passed by reference in _bootstrap_config_login_block_view() (line 38 ...
The following code will prevent the warning.
Comment #7
Anticosti commentedWhere could I copy/paste this code in my bootstrap subtheme?
Thanks in advance ;-)
Comment #8
woop_light commentedinterested to know where this code goes as well.
Comment #9
joeystern commentedOne more for hearing about how to implement this.
Comment #10
cthiebault commentedThis Bootstrap config module is a sub module of Mica:
See specific source code here:
http://drupalcode.org/project/mica.git/tree/refs/heads/7.x-7.x:/extensio...
Bootstrap sub theme is in Mica Distribution:
http://drupalcode.org/project/mica_distribution.git/tree/refs/heads/7.x-...
Comment #10.0
cthiebault commentedAdd info about custom module
Comment #11
stopshinal commentedWhat needs to be adapted for this to work with the bootstrap 3 version?
Comment #12
aialves commentedThanks for this great module.
I need to show the 'login form' in all item menus of navigation menu that are links to 'user/login'. Can anyone tell me how can I implement this?
Thanks in advance.
Comment #13
markhalliwellThis should be added to this module (eventually)
Comment #14
R-H commentedAny chance this could be made available?
Comment #15
markhalliwellA patch would be helpful.
Comment #16
R-H commentedIt's out of skill set but I asked cthiebault to submit a patch.
If anyone cares to help out that would be awesome.
Comment #17
markhalliwell