You are here

drop_down_login.module in Drop Down Login 7

Same filename and directory in other branches
  1. 8 drop_down_login.module

Module file for Drop Down Login.

File

drop_down_login.module
View source
<?php

/**
 * @file
 * Module file for Drop Down Login.
 */

/**
 * Implements hook_help().
 */
function drop_down_login_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/help#drop_down_login':
      $output .= '<p>' . t('This module provide a drop down block for user login,User can display this block in any region for all / specified pages.This is a tiny project, Login block display on login button toggle,if javascript is disabled clicking on the button will redirect user to user login page.') . '</p>';
      $output .= '<h6>' . t('Installation') . '</h6>';
      $output .= '<ol>';
      $output .= '<li>' . t('Install as usual, see <a href="@2367405">https://www.drupal.org/node/2367405</a> for further information.', array(
        '@2367405' => 'https://www.drupal.org/node/2367405',
      )) . '</li>';
      $output .= '<li>' . t('Copy the entire drop_down_login directory the Drupal sites/all/modules/custom directory.') . '</li>';
      $output .= '<li>' . t('Login as an administrator. Enable the module in the "Administer" -> "Modules".') . '</li>';
      $output .= '</ol>';
      $output .= '<h6>' . t('Configuration') . '</h6>';
      $output .= '<ul>';
      $output .= '<li>' . t('Add the new block "Drop down Login" to the required region via "Administer" -> "Structure" -> "Blocks".') . '</li>';
      $output .= '</ul>';
      break;
  }
  return $output;
}

/**
 * Implements hook_block_info().
 */
function drop_down_login_block_info() {
  $blocks['drop_down_login'] = array(
    'info' => t('Drop Down Login'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function drop_down_login_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'drop_down_login':
      global $theme;
      $bootstrap_template = variable_get('drop_down_login_bootstrap_theme');
      $bootstrap_theme = theme_get_setting('bootstrap_image_responsive', $theme);
      if (isset($bootstrap_template) && !empty($bootstrap_template) && isset($bootstrap_theme) && !empty($bootstrap_theme)) {
        $drop_down_myaccount = 'drop_down_bootstrap_myaccount';
        $drop_down_logout = 'drop_down_bootstrap_logout';
        $drop_down_login = 'drop_down_bootstrap_login';
      }
      else {
        $drop_down_myaccount = 'drop_down_myaccount';
        $drop_down_logout = 'drop_down_logout';
        $drop_down_login = 'drop_down_login';
      }
      if (user_is_anonymous()) {

        // Variables for login.
        // Block login form.
        $login_form = drupal_get_form('user_login_block');
        $login_url = url('user');
        $login_link_text = variable_get('login_text', t('Login'));
        $block['content']['#markup'] = theme($drop_down_login, array(
          'login_form' => $login_form,
          'login_url' => $login_url,
          'login_link_text' => $login_link_text,
        ));
      }
      else {

        // Check drop down login settings.
        global $user;
        $profile_pic = variable_get('drop_down_login_profile_pic');
        $myaccount = variable_get('drop_down_login_want_myaccount');
        $account_text = variable_get('my_account_text', t('My Account'));

        // Required my account drop down.
        if (isset($profile_pic) && isset($user->picture) && !empty($user->picture) && !empty($profile_pic)) {
          $profile_load = file_load($user->picture);
          $profile_image = image_style_url('thumbnail', $profile_load->uri);
        }
        if (isset($myaccount) && !empty($myaccount)) {
          $myaccount_links = array();
          $myaccount_url = url('user');
          $myaccount_text = $account_text;
          $myaccount_link = variable_get('drop_down_login_myaccount_links', array());
          foreach ($myaccount_link as $key => $value) {

            // If <front> is used as a menu url in configuration.
            if ($value['menu']['menu_url'] == '<front>') {
              $myaccount_links[$key] = $value;
              $myaccount_links[$key]['menu']['menu_url'] = variable_get('site_frontpage', 'node');
            }
            else {
              $router_item = menu_get_item($value['menu']['menu_url']);
              if ($router_item = menu_get_item($value['menu']['menu_url'])) {
                if ($router_item['access']) {
                  $myaccount_links[$key] = $value;
                }
              }
            }
          }

          // Sort links by weight.
          if (!empty($myaccount_links)) {
            usort($myaccount_links, function ($a, $b) {
              return $a['weight'] - $b['weight'];
            });
          }
          $block['content']['#markup'] = theme($drop_down_myaccount, array(
            'myaccount_links' => !empty($myaccount_links) ? $myaccount_links : NULL,
            'myaccount_url' => $myaccount_url,
            'myaccount_text' => $myaccount_text,
            'account' => $user,
            'profile_image' => isset($profile_image) ? $profile_image : '',
          ));
        }
        else {

          // Variables for Logout.
          $logout_url = url('user/logout');
          $logout_link_text = variable_get('logout_text', t('Logout'));
          $block['content']['#markup'] = theme($drop_down_logout, array(
            'logout_url' => $logout_url,
            'logout_link_text' => $logout_link_text,
          ));
        }
      }
      break;
  }

  // Attching required js and css.
  $block['content']['#attached'] = array(
    'css' => array(
      drupal_get_path('module', 'drop_down_login') . '/theme/drop_down_login.css',
    ),
    'js' => array(
      drupal_get_path('module', 'drop_down_login') . '/theme/drop_down_login.js',
    ),
  );
  return $block;
}

/**
 * Implements hook_form_alter().
 */
function drop_down_login_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login_block') {

    // Validate handler for login form.
    $form["#validate"][] = 'drop_down_login_validate_login';

    // Submit handler for login form.
    $form["#submit"][] = 'drop_down_login_submit_login';
  }
}

/**
 * Validate function to validate login form.
 */
function drop_down_login_validate_login($form, &$form_state) {

  // On successful login, the uid is set in form_state.
  if (empty($form_state["uid"])) {
    $attributes = array();
    if (!drupal_is_front_page()) {

      // No query string passed for homepage.
      $attributes = array(
        'query' => drupal_get_destination(),
      );
    }
    unset($_GET['destination']);
    drupal_static_reset('drupal_get_destination');
    drupal_get_destination();
    $form_state['redirect'] = array(
      'user',
      $attributes,
    );
    drupal_redirect_form($form_state);
  }
}

/**
 * Submit handler for login form.
 */
function drop_down_login_submit_login($form, &$form_state) {
  if (drupal_is_front_page()) {

    // Redirect to maintain homepage after login.
    $form_state['redirect'] = array(
      '<front>',
      array(
        'query' => drupal_get_query_parameters(),
      ),
    );
  }
  else {

    // Redirect to maintain the current page.
    $form_state['redirect'] = array(
      current_path(),
      array(
        'query' => drupal_get_query_parameters(),
      ),
    );
  }
}

/**
 * Implements hook_permission().
 */
function drop_down_login_permission() {
  return array(
    'drop down login settings' => array(
      'title' => t('Drop down login settings'),
      'description' => t('Permission for settings drop down login.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function drop_down_login_menu() {
  $items = array();
  $items['admin/config/user-interface/drop_down_login/settings'] = array(
    'title' => 'Drop down login settings',
    'description' => 'Drop down login settings forms page.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'drop_down_login_settings',
    ),
    'access arguments' => array(
      'drop down login settings',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'drop_down_login.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function drop_down_login_theme($existing, $type, $theme, $path) {
  return array(
    'drop_down_login' => array(
      'template' => 'drop_down_login',
      'path' => $path . '/theme',
      'variable' => array(
        'login_form' => NULL,
        'login_url' => NULL,
        'login_link_text' => NULL,
      ),
    ),
    'drop_down_logout' => array(
      'template' => 'drop_down_logout',
      'path' => $path . '/theme',
      'variable' => array(
        'logout_url' => NULL,
        'logout_link_text' => NULL,
      ),
    ),
    'drop_down_myaccount' => array(
      'template' => 'drop_down_myaccount',
      'path' => $path . '/theme',
      'variable' => array(
        'myaccount_links' => NULL,
        'myaccount_url' => NULL,
        'muaccount_link_text' => NULL,
        'account' => NULL,
        'profile_image' => NULL,
      ),
    ),
    'drop_down_bootstrap_login' => array(
      'template' => 'drop_down_bootstrap_login',
      'path' => $path . '/theme',
      'variable' => array(
        'login_form' => NULL,
        'login_url' => NULL,
        'login_link_text' => NULL,
      ),
    ),
    'drop_down_bootstrap_logout' => array(
      'template' => 'drop_down_bootstrap_logout',
      'path' => $path . '/theme',
      'variable' => array(
        'logout_url' => NULL,
        'logout_link_text' => NULL,
      ),
    ),
    'drop_down_bootstrap_myaccount' => array(
      'template' => 'drop_down_bootstrap_myaccount',
      'path' => $path . '/theme',
      'variable' => array(
        'myaccount_links' => NULL,
        'myaccount_url' => NULL,
        'muaccount_link_text' => NULL,
        'account' => NULL,
        'profile_image' => NULL,
      ),
    ),
    'drop_down_login_dragandrop' => array(
      'render element' => 'element',
    ),
  );
}

/**
 * Theme for make our multifield draggable.
 */
function theme_drop_down_login_dragandrop($variables) {
  $element = $variables['element'];
  $output = '';
  $rows = array();
  foreach (element_children($element) as $id) {
    if (!empty($element[$id]['menu']) && !empty($element[$id]['weight'])) {
      $rows[$id]['data']['menu'] = drupal_render($element[$id]['menu']);
      $rows[$id]['data']['weight'] = drupal_render($element[$id]['weight']);
      $rows[$id]['class'][] = 'draggable';
    }
  }

  // We now define the table header values.  Ensure that the 'header' count
  // matches the final column count for your table.
  $header = array(
    t('Menu'),
    t('Weight'),
  );

  // Aet a uniq id for this table.
  $table_id = 'drop-down-login-multifield-table_' . $element['#id'];

  // We can now render our tabledrag table for output.
  $output .= theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => $table_id,
    ),
  ));

  // Just in case...
  $output .= drupal_render_children($element);

  // Include js.
  drupal_add_tabledrag($table_id, 'order', 'sibling', 'drop-down-login-item-weight');
  return $output;
}

Functions

Namesort descending Description
drop_down_login_block_info Implements hook_block_info().
drop_down_login_block_view Implements hook_block_view().
drop_down_login_form_alter Implements hook_form_alter().
drop_down_login_help Implements hook_help().
drop_down_login_menu Implements hook_menu().
drop_down_login_permission Implements hook_permission().
drop_down_login_submit_login Submit handler for login form.
drop_down_login_theme Implements hook_theme().
drop_down_login_validate_login Validate function to validate login form.
theme_drop_down_login_dragandrop Theme for make our multifield draggable.