fancy_login.module in Fancy Login 8.2
Same filename and directory in other branches
Holds hooks for the Fancy Login module.
File
fancy_login.moduleView source
<?php
/**
* @file
* Holds hooks for the Fancy Login module.
*/
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\FormStateInterface;
use Drupal\fancy_login\Ajax\FancyLoginRefreshPageCommand;
use Drupal\fancy_login\Ajax\FancyLoginRedirectCommand;
use Drupal\fancy_login\Ajax\FancyLoginClosePopupCommand;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Ajax callback function for fancy_login_user_login_block submit button.
*/
function fancy_login_user_login_block_ajax_callback(array $form, FormStateInterface $form_state) {
global $base_url;
$response = new AjaxResponse();
$message_data = drupal_get_messages(NULL, FALSE);
// Check to see if there were any errors with the form submission.
if (!isset($message_data['error'])) {
$config = \Drupal::config('fancy_login.settings');
if ($config
->get('no_redirect')) {
drupal_set_message(t('You have been successfully logged in. Please wait while the page is refreshed.'));
$response
->addCommand(new FancyLoginRefreshPageCommand(TRUE));
}
else {
drupal_set_message(t('You have been successfully logged in. Please wait while you are redirected.'));
$dest = $base_url . $form_state
->getRedirect();
$response
->addCommand(new FancyLoginRedirectCommand(TRUE, $dest));
}
}
$renderer = \Drupal::service('renderer');
$messages = [
'#type' => 'status_messages',
];
$messages = $renderer
->renderRoot($messages);
$selector = '#fancy_login_user_login_block_wrapper';
$response
->addCommand(new AppendCommand($selector, $messages));
return $response;
}
/**
* Ajax callback function for fancy_login_user_pass submit button.
*/
function fancy_login_user_pass_ajax_callback(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$message_data = drupal_get_messages(NULL, FALSE);
// Check to see if there were any errors with the form submission.
if (!count($message_data['error'])) {
$response
->addCommand(new FancyLoginClosePopupCommand());
}
$renderer = \Drupal::service('renderer');
$messages = [
'#type' => 'status_messages',
];
$messages = $renderer
->renderRoot($messages);
$selector = '#fancy_login_user_pass_block_wrapper';
$response
->addCommand(new AppendCommand($selector, $messages));
return $response;
}
/**
* Implements hook_page_bottom().
*
* Adds login form and page dimmer to the footer of the page.
*/
function fancy_login_page_bottom(array &$page_bottom) {
if (\Drupal::currentUser()
->isAnonymous() || !empty($GLOBALS['menu_admin'])) {
$form = \Drupal::formBuilder()
->getForm('Drupal\\fancy_login\\Form\\FancyLoginLoginForm');
$login_path = \Drupal::service('path.alias_manager')
->getAliasByPath('/user/login');
$fl_config = \Drupal::config('fancy_login.settings');
$fl_settings = [
'screenFadeColor' => $fl_config
->get('screen_fade_color'),
'screenFadeZIndex' => $fl_config
->get('screen_fade_z_index'),
'loginBoxBackgroundColor' => $fl_config
->get('login_box_background_color'),
'loginBoxTextColor' => $fl_config
->get('login_box_text_color'),
'loginBoxBorderColor' => $fl_config
->get('login_box_border_color'),
'loginBoxBorderWidth' => $fl_config
->get('login_box_border_width'),
'loginBoxBorderStyle' => $fl_config
->get('login_box_border_style'),
'loginPath' => $login_path,
'dimFadeSpeed' => $fl_config
->get('dim_fade_speed'),
'boxFadeSpeed' => $fl_config
->get('box_fade_speed'),
'hideObjects' => $fl_config
->get('hide_objects'),
];
$page_bottom['fancy_login'] = [
'#prefix' => '<div class="region region-page-bottom"><div id="fancy_login_dim_screen"></div><div id="fancy_login_login_box">',
'#suffix' => '</div></div>',
'form_wrapper' => [
'#prefix' => '<div id="fancy_login_form_contents"><a href="#" id="fancy_login_close_button">X</a>',
'#suffix' => '</div>',
'form' => $form,
],
];
$page_bottom['fancy_login']['#attached']['library'][] = 'fancy_login/popup';
$page_bottom['fancy_login']['#attached']['drupalSettings']['fancyLogin'] = $fl_settings;
}
}
/**
* Implements hook_form_alter().
*/
function fancy_login_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'fancy_login_user_login_form') {
unset($form['name']['#attributes']['autofocus']);
unset($form['name']['#description']);
unset($form['pass']['#description']);
$form['name']['#size'] = 15;
$form['pass']['#size'] = 15;
// If the SSL icon is to be shown on the form, insert it into the form in
// the relevant location.
$config = \Drupal::config('fancy_login.settings');
$icon_position = $config
->get('icon_position');
$https_config = $config
->get('https');
if ($icon_position && $https_config) {
$form['ssl_logo'] = [
'#theme' => 'ssl_icon',
];
if ($icon_position == 1) {
$form['ssl_logo']['#weight'] = -100;
$form['#attributes'] = [
'class' => 'ssl_icon_above',
];
}
elseif ($icon_position == 2) {
$form['ssl_logo']['#weight'] = 100;
$form['#attributes'] = [
'class' => [
'ssl_icon_below',
],
];
}
}
// Add a wrapper for our #ajax callback.
$form['#prefix'] = '<div id="fancy_login_user_login_block_wrapper">';
$form['#suffix'] = '</div>';
// Add links to be used in our Fancy Login block, allowing the states to be
// changed between login, register, and recover password.
$items = [];
if (\Drupal::config('user.settings')
->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
$url = Url::fromRoute('user.register', [], [
'attributes' => [
'title' => t('Create a new user account.'),
],
]);
$items['create_account'] = Link::fromTextAndUrl(t('Create new account'), $url);
}
$url = Url::fromRoute('user.pass', [], [
'attributes' => [
'title' => t('Request new password via email.'),
],
]);
$items['request_password'] = Link::fromTextAndUrl(t('Request new password'), $url);
$form['user_links'] = [
'#theme' => 'item_list',
'#items' => $items,
];
// If Fancy Login is set to use https, change the
// protocol of the form action if necessary.
if ($https_config && strpos(\Drupal::request()
->getRequestUri(), 'https://') != 0) {
if (strpos($form['#action'], 'https') !== 0) {
if (strpos($form['#action'], 'http') === 0) {
$form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
}
elseif (strpos($form['#action'], '//') === 0) {
$form['#action'] = 'https:' . $form['#action'];
}
else {
$form['#action'] = preg_replace('/^http:/', 'https:', \Drupal::request()
->getRequestUri()) . $form['#action'];
}
}
}
else {
// Set the submit button of the forum to submit with #ajax. Attach
// JavaScript settings to the element.
$form['actions']['submit']['#ajax'] = [
'callback' => 'fancy_login_user_login_block_ajax_callback',
];
$form['actions']['submit-' . REQUEST_TIME] = $form['actions']['submit'];
unset($form['actions']['submit']);
}
}
elseif ($form_id == 'fancy_login_user_pass') {
// Add a wrapper for the #ajax callback.
$form['#prefix'] = '<div id="fancy_login_user_pass_block_wrapper">';
$form['#suffix'] = '</div>';
// Add links to be used in the Fancy Login block, allowing the states to be
// changed between login, register, and recover password.
$items = [];
$url = URL::fromRoute('user.login', [], [
'attributes' => [
'title' => t('Log in to @site_name.', [
'@site_name' => \Drupal::config('system.site')
->get('name') ?: t('this site'),
]),
],
]);
$items['sign_in'] = Link::fromTextAndUrl(t('Sign in'), $url);
if (\Drupal::config('user.settings')
->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
$url = Url::fromRoute('user.register', [], [
'attributes' => [
'title' => t('Create a new user account.'),
],
]);
$items['create_account'] = Link::fromTextAndUrl(t('Create new account'), $url);
}
$form['user_links'] = [
'#theme' => 'item_list',
'#items' => $items,
];
// If Fancy Login is set to use https, change the protocol of the form
// action if necessary.
$https_config = \Drupal::config('fancy_login.settings')
->get('https');
if ($https_config && strpos(\Drupal::request()
->getRequestUri(), 'https:') !== 0) {
if (strpos($form['#action'], 'https') !== 0) {
if (strpos($form['#action'], 'http') === 0) {
$form['#action'] = preg_replace('/^http:/', 'https:', $form['#action']);
}
elseif (strpos($form['#action'], '//') === 0) {
$form['#action'] = 'https:' . $form['#action'];
}
else {
$form['#action'] = preg_replace('/^http:/', 'https:', \Drupal::request()
->getRequestUri()) . $form['#action'];
}
}
}
else {
// Set the submit button of the forum to submit with #ajax.
$form['actions']['submit']['#ajax'] = [
'callback' => 'fancy_login_user_pass_ajax_callback',
];
$form['actions']['submit-' . REQUEST_TIME] = $form['actions']['submit'];
unset($form['actions']['submit']);
}
}
}
/**
* Implements hook_theme().
*/
function fancy_login_theme() {
return [
'ssl_icon' => [
'template' => 'ssl-icon',
],
];
}
/**
* Implements hook_preprocess_ssl_icon().
*/
function fancy_login_preprocess_ssl_icon(&$variables) {
$variables['file_url'] = file_create_url(drupal_get_path('module', 'fancy_login') . '/images/https.png');
$variables['alt'] = t('Secure Login');
$variables['title'] = t('This login is SSL protected');
}
Functions
Name | Description |
---|---|
fancy_login_form_alter | Implements hook_form_alter(). |
fancy_login_page_bottom | Implements hook_page_bottom(). |
fancy_login_preprocess_ssl_icon | Implements hook_preprocess_ssl_icon(). |
fancy_login_theme | Implements hook_theme(). |
fancy_login_user_login_block_ajax_callback | Ajax callback function for fancy_login_user_login_block submit button. |
fancy_login_user_pass_ajax_callback | Ajax callback function for fancy_login_user_pass submit button. |