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

frankbaele’s picture

thx

mrupsidown’s picture

Category: task » support

Thanks!

TheJoker’s picture

return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
woop_light’s picture

Seems pretty cool -- where should I add this code?

TheJoker’s picture

Missed a comma in the code on line '# commands' => $ commands,

christian le fournis’s picture

Great 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.

<?php
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">';
	$form1 = drupal_get_form('user_login');
	$content .= drupal_render($form1);
	$content .= '</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">';
		$form = drupal_get_form('user_register_form');
		$content .= drupal_render($form);
		$content .= '</div>';
    $content .= '</li>';
  }
  $content .= '</ul>';

  return array(
    'subject' => NULL,
    'content' => $content,
  );
}
?>
Anticosti’s picture

Where could I copy/paste this code in my bootstrap subtheme?
Thanks in advance ;-)

woop_light’s picture

interested to know where this code goes as well.

joeystern’s picture

One more for hearing about how to implement this.

cthiebault’s picture

cthiebault’s picture

Issue summary: View changes

Add info about custom module

stopshinal’s picture

What needs to be adapted for this to work with the bootstrap 3 version?

aialves’s picture

Thanks 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.

markhalliwell’s picture

Project: Bootstrap » Bootstrap UX (moved)
Version: 7.x-2.x-dev »
Category: Support request » Feature request
Issue summary: View changes

This should be added to this module (eventually)

R-H’s picture

Any chance this could be made available?

markhalliwell’s picture

Status: Active » Needs work

A patch would be helpful.

R-H’s picture

It's out of skill set but I asked cthiebault to submit a patch.

If anyone cares to help out that would be awesome.

markhalliwell’s picture

Project: Bootstrap UX (moved) » Bootstrap Core
Version: » 7.x-3.x-dev