csna.module
5.46 KB
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
<?php
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
/**
* Returns an array of OAuth Third Party authentication providers.
*
* CSNA's API Framework provides a method for any enabled module to define
* additional authentication providers. This function gathers information on
* provider authentication methods defined by enabled modules.
*
* @param bool $valid
* @return array An associative array of providers keyed by the provider_id.
* An associative array of providers keyed by the provider_id.
* @see hook_csna_provider_info()
*/
function csna_providers($valid = FALSE) {
static $providers;
if (!isset($providers)) {
$providers = array();
// Build the providers array, including module names for the purpose
// of including files if necessary.
// An associative array of providers objects keyed by the provider_id.
$module_handler = \Drupal::moduleHandler();
foreach ($module_handler->getImplementations('csna_provider_info') as $module) {
foreach ($module_handler->invoke($module, 'csna_provider_info') as $provider_id => $provider) {
$config = \Drupal::config('csna.' . $provider);
$provider['provider_id'] = $provider_id;
$provider['module'] = $module;
$provider['key'] = $config->get('csna_' . $provider_id . '_key');
$provider['secret'] = $config->get('csna_' . $provider_id . '_secret');
$providers[$provider_id] = $provider;
if ($valid && (empty($provider['key']) || empty($provider['secret']))) {
unset($providers[$provider_id]);
}
}
}
$module_handler->alter('csna_provider_info', $providers);
foreach ($providers as $provider_id => & $provider) {
$defaults = array(
'provider_id' => $provider_id,
'base' => $provider_id,
'title' => '',
'description' => '',
'active' => FALSE,
'callback' => Url::fromRoute('csna.callback',array('csna_provider' => $provider_id), array('absolute' => TRUE)),
'file' => '',
);
$provider += $defaults;
// Default the display title to the title if necessary. The display
// title is used in instances where the login method has an official
// name but a different method of display on selection forms (like some
// text and a set of images).
if (empty($provider['display_title'])) {
$provider['display_title'] = $provider['title'];
}
// Default the short title to the title if necessary. Like the display
// title, the short title is an alternate way of displaying the title to
// the user consisting of plain text but with unnecessary information
// stripped off.
if (empty($provider[$provider_id]['short_title'])) {
$provider['short_title'] = $provider['title'];
}
}
}
return $providers;
}
function csna_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
//$form_id == 'user_login' || $form_id == 'user_login_block' || $form_id == 'user_register_form'
if ( $form_id == 'user_login_form' ) {
$providers = csna_providers();
// if admin form
if (empty($form['administer_users']['#value'])) {
$csna_provider = array(
'#theme' => 'csna_providers',
'#attributes' => array('providers' => $providers),
);
if (isset($form['submitted']['user_login_block']['#suffix']) && !empty($form['submitted']['user_login_block']['#suffix'])) {
$form['submitted']['user_login_block']['#suffix'] .= drupal_render($csna_provider);
}
else {
$form['submitted']['user_login_block']['#suffix'] = drupal_render($csna_provider);
}
}
}
dpm($form_id);
// dpm($form);
}
/**
* Implements hook_theme().
*/
function csna_theme($existing, $type, $theme, $path) {
$providers = csna_providers();
return array(
'csna_providers' => array(
'variables' => array('providers' => $providers),
),
);
}
/**
* Render a themed list of links to providers authorized login URL pages.
*
* @param array $providers
* An associative array of providers keyed by the provider_id, as returned by
* the function csna_providers().
* Mostly the display_title property of providers is used in this function
* to display a custom title, name or even HTML to be linked to corresponding
* provider's authorized login URL page. See API documentation for more
* information on providers available properties.
*
* @see csna_providers()
* @see hook_csna_provider_info()
*/
function template_preprocess_csna_providers(&$vars) {
dpm($vars);
$providers = $vars['providers'];
$items = array();
foreach ($providers as $provider_id => $provider) {
$items[] = array(
'data' => \Drupal::l($provider['display_title'], Url::fromRoute('csna.redirect', array('csna_provider' => $provider_id), array('html' => TRUE))),
'id' => 'csna_provider_' . $provider_id,
);
}
$item_list = array(
'#type' => 'item_list',
'items' => $items,
'attributes' => array('class' => 'csna-items'),
);
return drupal_render($item_list);
}