/
simplelogin.module
232 lines (214 loc) · 7.82 KB
/
simplelogin.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* @file
* The module file for simplelogin 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) {
$fileRelativePath = '';
$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_css = $simplelogin_settings->get('unset_css', '');
switch ($arguments) {
case "color":
$simplelogin = $background_color ? $background_color : '#00bfff';
break;
case "image":
//$file = File::load($background_image[0]); // File Load
$fileRelativePath = '';
if (!empty($file) && $file = File::load($background_image[0])) {
$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_css":
$simplelogin = $unset_css ? $unset_css : '';
break;
default:
$simplelogin = '';
}
return $simplelogin;
}
/**
* Implements hook_theme_registry_alter
*
* {@inheritdoc}
*/
function simplelogin_theme_registry_alter(&$theme_registry) {
$module_path = drupal_get_path('module', 'simplelogin');
$theme_registry['page__simplelogin'] = array(
'preprocess functions' => array(
0 => 'simplelogin_preprocess_simplelogin',
),
'template' => 'page--simplelogin',
'path' => $module_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_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)]);
}
}
}
}