diff --git a/js/auth-destination.js b/js/auth-destination.js index e69de29..2283a83 100644 --- a/js/auth-destination.js +++ b/js/auth-destination.js @@ -0,0 +1,33 @@ +/** + * @file + * Append destination parameter to social auth links + * + */ +(function ($, Drupal) { + + 'use strict'; + + Drupal.behaviors.destination = { + attach: function (context, settings) { + function getUrlVars() { + var vars = []; + var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) { + vars[key] = value; + }); + return vars; + } + + if (window.location.href.indexOf('destination') > -1){ + var authLinks = ".auth-link"; + var urlVars = getUrlVars()['destination']; + var destination = urlVars ? urlVars : ''; + + // Appending destination parameter to all auth links + $.each($(authLinks).once(), function (i, el) { + $(el).attr('href', $(el).attr('href') + '?destination=' + destination); + }); + } + } + }; + +})(jQuery, Drupal); diff --git a/social_auth.libraries.yml b/social_auth.libraries.yml index 0a1584f..b4cdb6d 100755 --- a/social_auth.libraries.yml +++ b/social_auth.libraries.yml @@ -1,4 +1,9 @@ auth-icons: + js: + js/auth-destination.js: {} + dependencies: + - core/jquery + - core/drupal css: theme: css/auth-icons.css: {} diff --git a/social_auth.module b/social_auth.module index a1f0198..5725f10 100755 --- a/social_auth.module +++ b/social_auth.module @@ -11,7 +11,7 @@ function social_auth_theme() { return [ 'login_with' => [ - 'variables' => ['social_networks' => NULL, 'destination' => NULL], + 'variables' => ['social_networks' => NULL], ], ]; } @@ -20,9 +20,5 @@ function social_auth_theme() { * Implements hook_preprocess_HOOK(). */ function social_auth_preprocess_login_with(&$variables) { - $request = \Drupal::request(); - if ($request->query->has('destination')) { - $variables['destination'] = $request->query->get('destination'); - } $variables['base_path'] = base_path(); } diff --git a/templates/login-with.html.twig b/templates/login-with.html.twig index 8270e00..93da9d0 100755 --- a/templates/login-with.html.twig +++ b/templates/login-with.html.twig @@ -2,12 +2,10 @@ {{ attach_library('social_auth/auth-icons') }} +