You are here

simplelogin.module in SimpleLogin 8.6

Same filename and directory in other branches
  1. 7 simplelogin.module

The module file for simplelogin pages module.

File

simplelogin.module
View source
<?php

/**
 * @file
 * The module file for simplelogin pages module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\Core\Controller\ControllerBase;
use Drupal\simplelogin\Controller;

/**
 * Implements hook_help().
 * 
 * {@inheritdoc}
 */
function simplelogin_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.simplelogin':
      $output = '';
      $output .= '<h3>' . t('Settings') . '</h3>';
      $output .= '<p>' . t('Simplelogin settings: /admin/config/simplelogin') . '</p>';
      return $output;
      break;
  }
}

/**
 * Implements hook_preprocess_html() for page--simplelogin.tpl.php.
 *
 * {@inheritdoc}
 */
function simplelogin_preprocess_html(&$variables) {
  $path = \Drupal::service('path.current')
    ->getPath();
  $user_path = array(
    '/user',
    '/user/login',
    '/user/password',
    '/user/register',
  );
  $current_user = \Drupal::currentUser();

  // Whether the current user is anonymous or authenticated
  if (!$current_user
    ->id()) {
    if (in_array($path, $user_path)) {
      $variables['attributes']['class'][] = 'simplelogin';
      $background_opacity = simple_login_settings('opacity');
      if ($background_opacity) {
        $variables['attributes']['class'][] = 'opacity';
      }
    }
  }
}

/**
 * Implements hook_theme_suggestions_alter
 *
 * {@inheritdoc}
 */
function simplelogin_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($hook == 'page') {
    $path = \Drupal::service('path.current')
      ->getPath();
    $user_path = array(
      '/user',
      '/user/login',
      '/user/password',
      '/user/register',
    );
    $current_user = \Drupal::currentUser();

    // Whether the current user is anonymous or authenticated
    if (!$current_user
      ->id()) {
      if (in_array($path, $user_path)) {
        $suggestions[] = 'page__simplelogin';
      }
    }
  }
}

/**
 * Implements hook_page_attachments
 *
 * {@inheritdoc}
 */
function simplelogin_page_attachments(array &$attachments) {
  $path = \Drupal::service('path.current')
    ->getPath();
  $user_path = array(
    '/user',
    '/user/login',
    '/user/password',
    '/user/register',
  );
  $current_user = \Drupal::currentUser();

  // Whether the current user is anonymous or authenticated
  if (!$current_user
    ->id()) {
    if (in_array($path, $user_path)) {
      $background_active = simple_login_settings('active');
      $background_image = simple_login_settings('image');
      $background_color = simple_login_settings('color');
      $background_opacity = simple_login_settings('opacity');
      if ($background_active) {
        $css = "body.simplelogin { background-image: url('{$background_image}'); }";
      }
      else {
        $css = "body.simplelogin { background-color: {$background_color}; }";
      }
      $features = ".simplelogin-form input.form-submit, .simplelogin-form button.form-submit, .simplelogin-form input.form-submit:hover, .simplelogin-form input.form-submit:active, .simplelogin-form input.form-submit:focus, .simplelogin .link:hover, .simplelogin .link:focus { background-color: {$background_color}; } .simplelogin a, .simplelogin-form .forgot-password a, .simplelogin a:hover, .simplelogin a:focus { color: {$background_color}; }";
      $attachments['#attached']['html_head'][] = array(
        array(
          '#type' => 'html_tag',
          '#tag' => 'style',
          '#value' => $css . $features,
          '#attributes' => array(
            'type' => "text/css",
          ),
        ),
        'simplelogin_background',
      );
    }
  }
}
function simplelogin_preprocess_simplelogin(&$variables) {
  $path = \Drupal::service('path.current')
    ->getPath();
  $module_path = drupal_get_path('module', 'simplelogin');
  $variables['#attached']['library'][] = 'simplelogin/simplelogin-library';

  // Able to use custom variables
  $variables['path'] = $path;
  $variables['base_path'] = base_path();
  $variables['logo'] = file_url_transform_relative(file_create_url(theme_get_setting('logo.url')));
  $variables['background_class'] = simple_login_settings('active');
  $variables['background_opacity'] = simple_login_settings('opacity');
  $variables['wrapper_width'] = simple_login_settings('width');
  $variables['site_name'] = \Drupal::config('system.site')
    ->get('name');
  $variables['site_register'] = \Drupal::config('user.settings')
    ->get('register');
}

/**
 * Implements settings for simple login pages.
 */
function simple_login_settings($arguments) {
  $simplelogin_settings = \Drupal::config('simplelogin.settings');
  $background_active = $simplelogin_settings
    ->get('background_active', '');
  $background_image = $simplelogin_settings
    ->get('background_image', '');
  $background_opacity = $simplelogin_settings
    ->get('background_opacity', '');
  $background_color = $simplelogin_settings
    ->get('background_color', '');
  $wrapper_width = $simplelogin_settings
    ->get('wrapper_width', '');
  $unset_active_css = $simplelogin_settings
    ->get('unset_active_css', '');
  $unset_css = $simplelogin_settings
    ->get('unset_css', '');
  switch ($arguments) {
    case "color":
      $simplelogin = $background_color ? $background_color : '#00bfff';
      break;
    case "image":
      $fileRelativePath = '';
      if (isset($background_image[0])) {
        $file = File::load($background_image[0]);

        // File Load
        if ($file) {
          $fileUrl = $file
            ->getFileUri();
          $fileRelativePath = file_url_transform_relative(file_create_url($fileUrl));
        }
      }
      $simplelogin = $fileRelativePath;
      break;
    case "opacity":
      $simplelogin = $background_opacity && $background_active ? 1 : '';
      break;
    case "active":
      $simplelogin = $background_active ? 'active' : '';
      break;
    case "width":
      $simplelogin = $wrapper_width ? $wrapper_width : '360';
      break;
    case 'unset_active_css':
      $simplelogin = $unset_active_css ? $unset_active_css : '';
      break;
    case "unset_css":
      $simplelogin = $unset_css ? $unset_css : '';
      break;
    default:
      $simplelogin = '';
  }
  return $simplelogin;
}

/**
 * Implements hook_theme_registry_alter
 *
 * {@inheritdoc}
 */
function simplelogin_theme_registry_alter(&$theme_registry) {
  $path = drupal_get_path('module', 'simplelogin');

  // Check if file exists in the theme directory.
  $theme = \Drupal::theme()
    ->getActiveTheme();
  if (file_exists($theme
    ->getPath() . '/templates/page--simplelogin.html.twig')) {
    $path = drupal_get_path('theme', $theme
      ->getName());
  }
  $theme_registry['page__simplelogin'] = array(
    'preprocess functions' => array(
      0 => 'simplelogin_preprocess_simplelogin',
    ),
    'template' => 'page--simplelogin',
    'path' => $path . '/templates',
    'render element' => 'page',
    'type' => 'base_theme_engine',
  );
}

/**
 * Implements hook_form_alter().
 *
 * {@inheritdoc}
 */
function simplelogin_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $types = array(
    'textfield',
    'email',
    'textarea',
    'password',
    'container',
  );
  foreach ($form as $key => $value) {
    switch ($form_id) {
      case 'user_register_form':
        foreach ($form['account'] as $key => $val) {
          if (isset($form['account'][$key]['#type'])) {
            if (in_array($form['account'][$key]['#type'], $types)) {
              $form['account'][$key]['#attributes']["placeholder"] = $form['account'][$key]["#title"];
              $form['account'][$key]['#title_display'] = 'invisible';
              $form['account'][$key]['#description'] = '';
            }
          }
        }
        break;
      case 'user_login_form':
        $form['actions']['submit']['#value'] = t('Login to Account');
      case 'user_pass':
        if (isset($value['#type']) && in_array($value['#type'], $types)) {
          $form[$key]['#attributes']["placeholder"] = $value["#title"];
          $form[$key]['#title_display'] = 'invisible';
          $form[$key]['#description'] = '';
        }
        break;
    }
  }

  //if (\Drupal::moduleHandler()->moduleExists('captcha')) {

  //$form['captcha']['#after_build'][] = 'simplelogin_set_captcha';

  //}
}

/**
 * Implements hook_element_info_alter().
 */
function simplelogin_element_info_alter(array &$types) {
  if (isset($types['password_confirm'])) {
    $types['password_confirm']['#process'][] = 'simplelogin_password_confirm';
  }
}

/**
 * Process the password_confirm element.
 */
function simplelogin_password_confirm($element) {

  // hiding password title
  $element['pass1']['#title_display'] = 'invisible';
  $element['pass2']['#title_display'] = 'invisible';

  // Adding placeholder
  $element['pass1']["#placeholder"] = $element['pass1']['#title'];
  $element['pass2']["#placeholder"] = $element['pass2']['#title'];
  return $element;
}

/**
 * Implements hook_css_alter().
 *
 * {@inheritdoc}
 */
function simplelogin_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
  $path = \Drupal::service('path.current')
    ->getPath();
  $user_path = array(
    '/user',
    '/user/login',
    '/user/password',
    '/user/register',
  );
  $current_user = \Drupal::currentUser();

  // Whether the current user is anonymous or authenticated
  if (!$current_user
    ->id()) {
    if (in_array($path, $user_path)) {
      $unset_css = simple_login_settings('unset_css');
      $unset_css_array = explode("\n", $unset_css);
      foreach ($unset_css_array as $key => $value) {
        unset($css[trim($value)]);
      }

      // The active theme CSS files are removed from the simple login pages
      $unset_active_css = simple_login_settings('unset_active_css');
      if ($unset_active_css) {
        foreach ($css as $stylesheet => $value) {
          $theme = \Drupal::theme()
            ->getActiveTheme();
          if (strstr($stylesheet, $theme
            ->getPath())) {
            unset($css[trim($value['data'])]);
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
simplelogin_css_alter Implements hook_css_alter().
simplelogin_element_info_alter Implements hook_element_info_alter().
simplelogin_form_alter Implements hook_form_alter().
simplelogin_help Implements hook_help().
simplelogin_page_attachments Implements hook_page_attachments
simplelogin_password_confirm Process the password_confirm element.
simplelogin_preprocess_html Implements hook_preprocess_html() for page--simplelogin.tpl.php.
simplelogin_preprocess_simplelogin
simplelogin_theme_registry_alter Implements hook_theme_registry_alter
simplelogin_theme_suggestions_alter Implements hook_theme_suggestions_alter
simple_login_settings Implements settings for simple login pages.