bankid_oidc-1.x-dev/bankid_oidc.module
bankid_oidc.module
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements hook_form_FORM_ID_alter().
*/
function bankid_oidc_form_user_login_form_alter(
&$form,
FormStateInterface $form_state,
$form_id
) {
// Get all non form-api elements
$login_elements = array_filter(
array_keys($form),
function(string $key) {
return $key[0] !== '#';
}
);
// Create a wrapper that will contain such elements.
$form['drupal_login'] = [
'#type' => 'details',
'#title' => t('User/Password login'),
'#open' => FALSE,
'#weight' => 10,
'#attributes' => [
'class' => ['drupal-default-login-wrapper'],
],
];
// Move all elements in the hidden container.
foreach ($login_elements as $name) {
$form['drupal_login'][$name] = $form[$name];
unset($form[$name]);
}
$form['bankid_oidc'] = [
'#type' => 'details',
'#title' => t('BankID Login'),
'#attributes' => [
'class' => ['bankid-oidc-login-button-wrapper'],
],
'#open' => TRUE,
'#weight' => 0,
];
$info_text = t('Click the icon below to login using BankID or BankID on Mobile.');
$form['bankid_oidc']['info'] = [
'#markup' => "<p>{$info_text}</p>",
];
$path = \Drupal::moduleHandler()->getModule('bankid_oidc')->getPath();
$destination = \Drupal::request()->query->get('destination');
$options = [];
if ($destination) {
$options = [
'destination' => $destination
];
}
$form['bankid_oidc']['link'] = [
'#type' => 'link',
'#url' => Url::fromRoute('bankid_oidc.login', $options),
'#title' => t('BankID Login'),
'#attached' => [
'library' => ['bankid_oidc/login_block'],
],
'#attributes' => [
'style' => "background-image: url(/{$path}/asset/logo/BankID_Main_Logo.svg);",
],
];
// Add cache information
$form['#cache']['contexts'][] = 'url.query_args:destination';
}
