You are here

function logintoboggan_preprocess_page in LoginToboggan 8

Implements hook_preprocess_page() for page templates.

For login routes change forms rendered to support unified page and 403 login.

File

./logintoboggan.module, line 478
LoginToboggan module.

Code

function logintoboggan_preprocess_page(&$variables) {
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  $unified = \Drupal::config('logintoboggan.settings')
    ->get('unified_login');
  if ($route_name == 'user.login' && $unified == '1' || $route_name == 'user.register' && $unified == '1' || $route_name == 'logintoboggan.denied' && !$variables['logged_in']) {
    $variables['#attached']['library'][] = 'logintoboggan/logintoboggan-styles';
    $theme = \Drupal::service('theme.manager')
      ->getActiveTheme()
      ->getName();
    $theme_content = $theme . '_content';
    $theme_tasks = $theme . '_local_tasks';
    $variables['page']['content'][$theme_content] = [];
    $variables['page']['content'][$theme_tasks] = [];
    if (!\Drupal::currentUser()
      ->id()) {
      $login_form = Drupal::formBuilder()
        ->getForm('Drupal\\user\\Form\\UserLoginForm');
      if ($unified == '1') {
        $variables['page']['content']['login_link'] = [
          '#theme' => 'lt_unified_login_button',
        ];
        $variables['#attached']['library'][] = 'logintoboggan/logintoboggan_unified';
        $active_form = $route_name == 'user.register' ? 'register' : 'login';
        $variables['#attached']['drupalSettings']['LoginToboggan']['unifiedLoginActiveForm'] = $active_form;
        $variables['page']['content']['login_form'] = $login_form;
        $entity = \Drupal::entityTypeManager()
          ->getStorage('user')
          ->create([]);
        $formObject = \Drupal::entityTypeManager()
          ->getFormObject('user', 'register')
          ->setEntity($entity);
        $reg_form = \Drupal::formBuilder()
          ->getForm($formObject);
        $variables['page']['content']['registration_form'] = $reg_form;
        $pass_form = Drupal::formBuilder()
          ->getForm('Drupal\\user\\Form\\UserPasswordForm');
        $variables['page']['content']['pass_form'] = $pass_form;
      }
      else {

        // Login form can get displayed on 403 regardless of unified setting.
        $site_403 = \Drupal::config('logintoboggan.settings')
          ->get('site_403');
        if ($site_403) {
          $variables['page']['content']['login_form'] = $login_form;
        }
      }
    }
  }
}