escapeAdmin.es6.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @file
  3. * Replaces the home link in toolbar with a back to site link.
  4. */
  5. (function($, Drupal, drupalSettings) {
  6. const pathInfo = drupalSettings.path;
  7. const escapeAdminPath = sessionStorage.getItem('escapeAdminPath');
  8. const windowLocation = window.location;
  9. // Saves the last non-administrative page in the browser to be able to link
  10. // back to it when browsing administrative pages. If there is a destination
  11. // parameter there is not need to save the current path because the page is
  12. // loaded within an existing "workflow".
  13. if (
  14. !pathInfo.currentPathIsAdmin &&
  15. !/destination=/.test(windowLocation.search)
  16. ) {
  17. sessionStorage.setItem('escapeAdminPath', windowLocation);
  18. }
  19. /**
  20. * Replaces the "Home" link with "Back to site" link.
  21. *
  22. * Back to site link points to the last non-administrative page the user
  23. * visited within the same browser tab.
  24. *
  25. * @type {Drupal~behavior}
  26. *
  27. * @prop {Drupal~behaviorAttach} attach
  28. * Attaches the replacement functionality to the toolbar-escape-admin element.
  29. */
  30. Drupal.behaviors.escapeAdmin = {
  31. attach() {
  32. const $toolbarEscape = $('[data-toolbar-escape-admin]').once(
  33. 'escapeAdmin',
  34. );
  35. if ($toolbarEscape.length && pathInfo.currentPathIsAdmin) {
  36. if (escapeAdminPath !== null) {
  37. $toolbarEscape.attr('href', escapeAdminPath);
  38. } else {
  39. $toolbarEscape.text(Drupal.t('Home'));
  40. }
  41. }
  42. },
  43. };
  44. })(jQuery, Drupal, drupalSettings);