
Probléma: Anonymousként egy Drupal oldalon vagyok és be akarok jelentkezni. A bejelentkezés után vigyen vissza a bejelentkezés ablak előtti éppen aktuális oldalra a Drupal.
Sokáig kerestem a megoldást, de megvan:
Az aktuális témába beillesztem
tema.info.yml:
libraries:
- <tema neve>/global-styling
tema.libraries.yml
global-styling:
version: 1.x
css:
theme:
css/style.css: {}
js:
js/global.js: {}
dependencies:
- core/drupal
- core/jquery
- core/jquery.once
A js/global.js:
(function ($, Drupal) {
var fzpathname= window.location.pathname;
var hrefin = $("a[href^='<path>/user/login']").attr("href");
$("a[href^='<path>/user/login']").attr("href", hrefin + "?destination=" + fzpathname);
var hrefout = $("a[href^='<path>/user/logout']").attr("href");
$("a[href^='<path>/user/logout']").attr("href", hrefout + "?destination=" + fzpathname);
})(jQuery, Drupal);
Természetesen a fentihez hasonló kód használatával bármilyen contrib modullal is elérhetem ezt. Ha ugyanezt leprogramozom vanilla javascripttel, akkor nem fog függni a jQuery-től. Íme ez a változat:
function (Drupal) {
var fzpathname = window.location.pathname;
// For login link
var loginLink = document.querySelector("a[href^='/fw3/user/login']");
if (loginLink) {
var hrefin = loginLink.getAttribute("href");
loginLink.setAttribute("href", hrefin + "?destination=" + fzpathname);
}
// For logout link
var logoutLink = document.querySelector("a[href^='/fw3/user/logout']");
if (logoutLink) {
var hrefout = logoutLink.getAttribute("href");
logoutLink.setAttribute("href", hrefout + "?destination=" + fzpathname);
}
})(Drupal);