User Login Persistent Destination in drupal
/**
* Implements hook_menu_local_tasks_alter().
*/
function Module_local_tasks_alter(array &$data, $route_name, RefinableCacheableDependencyInterface $cacheability) {
if ($route_name == 'user.login' || $route_name == 'user.register') {
// Vary cache by ?destination query argument.
$cacheability->addCacheContexts(['url.query_args:destination']);
// Get the 'destination' query argument from current request.
$destination = \Drupal::request()->query->get('destination');
if (empty($destination)) {
// No destination set for current request - we can exit now.
return;
}
foreach ($data['tabs'] as $items) {
foreach ($items as $item) {
/** @var \Drupal\Core\Url $url */
$url = $item['#link']['url'];
$query_arguments = $url->getOption('query');
if (is_null($query_arguments)) {
$query_arguments = [];
}
$query_arguments['destination'] = $destination;
$url->setOption('query', $query_arguments);
}
}
}
}