You are here

better_login_form_config.module in Better Login Form Configuration 8

File

better_login_form_config/better_login_form_config.module
View source
<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation;

/*
*
*  hook_form_alter implement
*/
function better_login_form_config_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  global $base_url;
  $myConfig = \Drupal::config('fancy_login.settings');
  $site_name = \Drupal::config('system.site')
    ->get('name');
  if ($form_id == 'user_login_form') {
    $myConfig = \Drupal::config('fancy_login.settings');

    // at the top of the method
    $form['name']['#title'] = !empty($myConfig
      ->get('username_label')) ? $myConfig
      ->get('username_label') : 'Username';
    $form['name']['#description'] = !empty($myConfig
      ->get('username_description')) ? $myConfig
      ->get('username_description') : t('Enter your @s username.', array(
      '@s' => $site_name,
    ));
    $form['pass']['#title'] = !empty($myConfig
      ->get('password_label')) ? $myConfig
      ->get('password_label') : 'Password';
    $form['pass']['#description'] = !empty($myConfig
      ->get('password_description')) ? $myConfig
      ->get('password_description') : 'Enter the password that accompanies your username.';
    if ($myConfig
      ->get('form_title')) {
      $request = \Drupal::request();
      if ($route = $request->attributes
        ->get(\Symfony\Cmf\Component\Routing\RouteObjectInterface::ROUTE_OBJECT)) {
        $route
          ->setDefault('_title', $myConfig
          ->get('form_title'));
      }
    }
    $form['actions']['submit']['#value'] = !empty($myConfig
      ->get('login_button')) ? $myConfig
      ->get('login_button') : 'Log in';
  }
  if ($form_id == 'user_login_form' || $form_id == 'user_register_form' || $form_id == 'user_pass') {
    $form['#attached']['library'][] = 'better_login_form_config/fancylogin_css';
  }
  if ($form_id == 'user_register_form') {
    $form['name']['#title'] = !empty($myConfig
      ->get('forgot_form_username')) ? $myConfig
      ->get('forgot_form_username') : 'Username';
  }
  if ($form_id == 'user_register_form') {
    $form['account']['mail']['#description'] = !empty($myConfig
      ->get('register_mail_desc')) ? $myConfig
      ->get('register_mail_desc') : 'A valid email address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.';
    $form['account']['mail']['#title'] = !empty($myConfig
      ->get('register_form_mail')) ? $myConfig
      ->get('register_form_mail') : 'Email Address.';
    $form['account']['name']['#description'] = !empty($myConfig
      ->get('register_mail_desc')) ? $myConfig
      ->get('register_mail_desc') : "Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign.";
    $form['account']['name']['#title'] = !empty($myConfig
      ->get('register_form_name')) ? $myConfig
      ->get('register_form_name') : 'Username';
    $form['actions']['submit']['#value'] = !empty($myConfig
      ->get('register_form_button')) ? $myConfig
      ->get('register_form_button') : 'Create new account.';
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function better_login_form_config_theme_registry_alter(&$theme_registry) {
  $myConfig = \Drupal::config('fancy_login.settings');
  $pages = [
    'page__user__login' => 'page--user--login',
    'page__user__register' => 'page--user--register',
    'page__user__password' => 'page--user--password',
  ];

  // exclude register template
  if (!empty($myConfig
    ->get('include_regi_template'))) {
    unset($pages['page__user__register']);
  }

  // exclude login template
  if (!empty($myConfig
    ->get('include_login'))) {
    unset($pages['page__user__login']);
  }

  // exclude forgot password template
  if (!empty($myConfig
    ->get('include_forgot_template'))) {
    unset($pages['page__user__password']);
  }
  $mod_path = drupal_get_path('module', 'better_login_form_config') . '/templates';
  foreach ($pages as $key => $template) {
    $theme_registry[$key]['template'] = $template;
    $theme_registry[$key]['path'] = $mod_path;
    $theme_registry[$key]['preprocess functions'][0] = 'better_login_form_config_preprocess_page';
    $theme_registry[$key]['type'] = 'base_theme_engine';
  }
}

/**
 * Implements hook_preprocess_page().
 */
function better_login_form_config_preprocess_page(&$variables) {
  $variables['site_name'] = \Drupal::config('system.site')
    ->get('name');
  $variables['logo'] = theme_get_setting('logo.url');
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $myConfig = \Drupal::config('fancy_login.settings');
  $variables['create_accounts'] = $myConfig
    ->get('create_account');
  $variables['forgot_password'] = $myConfig
    ->get('forgot_password');
  switch ($current_path) {
    case '/user/login':
      $variables['title'] = $myConfig
        ->get('form_title');
      if (empty($myConfig
        ->get('include_login'))) {
        $variables['page']['content']['bartik_local_tasks'] = array();
        $variables['page']['content']['bartik_page_title'] = array();
      }
      break;
    case '/user/password':
      $variables['title'] = t($myConfig
        ->get('forgot_form_title'));
      if (empty($myConfig
        ->get('include_forgot_template'))) {
        $variables['page']['content']['bartik_local_tasks'] = array();
        $variables['page']['content']['bartik_page_title'] = array();
      }
      break;
    case '/user/register':
      $variables['title'] = t($myConfig
        ->get('register_form_title'));
      if (empty($myConfig
        ->get('include_regi_template'))) {
        $variables['page']['content']['bartik_local_tasks'] = array();
        $variables['page']['content']['bartik_page_title'] = array();
      }
      break;
  }
  $variables['#cache']['contexts'][] = 'route';
  $variables['page']['#cache']['contexts'][] = 'route';
}