View source
<?php
use Drupal\Core\Url;
use Drupal\user\UserInterface;
function gin_login_page_attachments_alter(&$page) {
if (_gin_login__check_path()) {
if (_gin_login_gin_is_active()) {
$theme = \Drupal::theme()
->getActiveTheme()
->getName();
$darkmode = theme_get_setting('enable_darkmode', $theme);
$preset_accent_color = theme_get_setting('preset_accent_color', $theme);
$accent_color = theme_get_setting('accent_color', $theme);
$preset_focus_color = theme_get_setting('preset_focus_color', $theme);
$focus_color = theme_get_setting('focus_color', $theme);
$highcontrastmode = theme_get_setting('high_contrast_mode', $theme);
$darkmode_class = 'gin--dark-mode';
$highcontrastmode_class = 'gin--high-contrast-mode';
$page['#attached']['library'][] = 'gin/gin_init';
$page['#attached']['library'][] = 'gin/gin';
$page['#attached']['library'][] = 'gin/gin_accent';
if (file_exists('public://gin-custom.css')) {
$page['#attached']['library'][] = 'gin/gin_custom_css';
}
$page['#attached']['drupalSettings']['gin']['darkmode'] = $darkmode;
$page['#attached']['drupalSettings']['gin']['darkmode_class'] = $darkmode_class;
$page['#attached']['drupalSettings']['gin']['preset_accent_color'] = $preset_accent_color;
$page['#attached']['drupalSettings']['gin']['accent_color'] = $accent_color;
$page['#attached']['drupalSettings']['gin']['preset_focus_color'] = $preset_focus_color;
$page['#attached']['drupalSettings']['gin']['focus_color'] = $focus_color;
$page['#attached']['drupalSettings']['gin']['highcontrastmode'] = $highcontrastmode;
$page['#attached']['drupalSettings']['gin']['highcontrastmode_class'] = $highcontrastmode_class;
}
$page['#attached']['library'][] = 'gin_login/gin_login';
}
}
function gin_login_preprocess_html(&$variables) {
if (_gin_login__check_path() && _gin_login_gin_is_active()) {
$theme = \Drupal::theme()
->getActiveTheme()
->getName();
$darkmode = theme_get_setting('enable_darkmode', $theme);
$preset_accent_color = theme_get_setting('preset_accent_color', $theme);
$high_contrast_mode = theme_get_setting('high_contrast_mode', $theme);
if ($darkmode === TRUE || $darkmode === 1) {
$variables['attributes']['class'][] = 'gin--dark-mode';
}
if ($preset_accent_color) {
$variables['attributes']['data-gin-accent'] = $preset_accent_color;
}
if ($high_contrast_mode === TRUE) {
$variables['attributes']['class'][] = 'gin--high-contrast-mode';
}
}
}
function gin_login_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'user_login') !== FALSE || strpos($form_id, 'user_register') !== FALSE || strpos($form_id, 'user_pass') !== FALSE) {
$form['actions']['submit']['#attributes']['class'][] = 'button--primary';
}
if (strpos($form_id, 'user_login') !== FALSE) {
$form['actions']['#weight'] = '98';
$form['actions']['submit']['#attributes']['class'][] = 'button-login';
$form['more-links'] = [
'#type' => 'container',
'#weight' => '99',
'#attributes' => [
'class' => [
'more-links',
],
],
];
if (\Drupal::config('user.settings')
->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
$form['more-links']['register_button'] = [
'#type' => 'link',
'#url' => Url::fromRoute('user.register'),
'#title' => t('Create new account'),
'#attributes' => [
'class' => [
'register-button',
'button',
'button--secondary',
],
],
'#weight' => '1',
];
}
$form['more-links']['forgot_password_link'] = [
'#type' => 'link',
'#url' => Url::fromRoute('user.pass'),
'#title' => t('Forgot your password?'),
'#attributes' => [
'class' => [
'link',
'forgot-password-link',
],
],
'#weight' => '2',
];
}
if (strpos($form_id, 'user_pass') !== FALSE) {
$form['actions']['submit']['#value'] = t('Reset');
}
}
function gin_login_theme() {
$theme['page__user__login'] = [
'template' => 'page--user--login',
'preprocess functions' => [
'gin_login_preprocess_ginlogin',
],
];
$theme['page__user__password'] = [
'template' => 'page--user--password',
'preprocess functions' => [
'gin_login_preprocess_ginlogin',
],
];
$theme['page__user__register'] = [
'template' => 'page--user--register',
'preprocess functions' => [
'gin_login_preprocess_ginlogin',
],
];
return $theme;
}
function gin_login_css_alter(&$css, $assets) {
$path = drupal_get_path('module', 'gin_login') . '/dist/css/gin_login.css';
if (isset($css[$path])) {
$css[$path]['group'] = 201;
}
}
function gin_login_preprocess_ginlogin(&$variables) {
$theme = \Drupal::theme()
->getActiveTheme()
->getName();
$darkmode = theme_get_setting('enable_darkmode', $theme);
$useDefaultIcon = theme_get_setting('icon_default', $theme);
$ginIconPath = theme_get_setting('icon_path', $theme);
$config = \Drupal::config('gin_login.settings');
$variables['site_name'] = \Drupal::config('system.site')
->get('name');
if (!$darkmode) {
$default_logo = '/images/gin.svg';
}
else {
$default_logo = '/images/gin_darkmode.svg';
}
if (!$useDefaultIcon && !empty($ginIconPath)) {
$variables['logo'] = file_create_url($ginIconPath);
}
else {
$defaultLogo = drupal_get_path('module', 'gin_login') . $default_logo;
$variables['logo'] = file_create_url($defaultLogo);
}
$configData = $config
->get();
if ($configData['logo']['use_default'] == "0") {
if (isset($configData['logo']['path']) && !empty($configData['logo']['path'])) {
$variables['logo'] = file_create_url($configData['logo']['path']);
}
}
$variables['brand_image'] = '//source.unsplash.com/collection/9407737';
$variables['brand_alt'] = 'Random image from unsplash';
if ($configData['brand_image']['use_default'] == "0") {
if (isset($configData['brand_image']['path']) && !empty($configData['brand_image']['path'])) {
$variables['brand_image'] = file_create_url($configData['brand_image']['path']);
$variables['brand_alt'] = '';
}
}
}
function _gin_login__check_path() {
$route = \Drupal::routeMatch()
->getRouteName();
if ($route == 'user.login' || $route == 'user.pass' || $route == 'user.register') {
return TRUE;
}
else {
return FALSE;
}
}
function _gin_login_gin_is_active() {
$theme_handler = \Drupal::service('theme_handler')
->listInfo();
$frontend_theme_name = \Drupal::config('system.theme')
->get('default');
if (isset($theme_handler[$frontend_theme_name]->base_themes)) {
$frontend_base_themes = $theme_handler[$frontend_theme_name]->base_themes;
}
$frontend_base_themes[$frontend_theme_name] = $frontend_theme_name;
$admin_theme_name = \Drupal::config('system.theme')
->get('admin');
if ($admin_theme_name) {
$admin_base_themes = isset($theme_handler[$admin_theme_name]->base_themes) ? $theme_handler[$admin_theme_name]->base_themes : [];
$admin_base_themes[$admin_theme_name] = $admin_theme_name;
}
else {
$admin_base_themes = $frontend_base_themes;
}
$gin_activated = array_key_exists('gin', $admin_base_themes);
return $gin_activated;
}