Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UHF-8946: redirect to existing job listing translation #313

Merged
merged 15 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- { name: 'event_subscriber' }
helfi_rekry_content.job_listing_redirect_subscriber:
class: Drupal\helfi_rekry_content\EventSubscriber\JobListingRedirectSubscriber
arguments: ['@config.factory', '@current_user']
arguments: ['@config.factory', '@current_user', '@entity_type.manager']
tags:
- { name: 'event_subscriber' }
logger.channel.helfi_rekry_content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
namespace Drupal\helfi_rekry_content\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;

/**
* Redirect job listing 403s for anonymous users.
* Http exception event subscribers for job listings.
*/
class JobListingRedirectSubscriber extends HttpExceptionSubscriberBase {

Expand All @@ -24,10 +25,13 @@ class JobListingRedirectSubscriber extends HttpExceptionSubscriberBase {
* The configuration factory.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* The entity type manager.
*/
public function __construct(
protected ConfigFactoryInterface $configFactory,
protected AccountInterface $currentUser
protected AccountInterface $currentUser,
protected EntityTypeManager $entityTypeManager,
) {}

/**
Expand Down Expand Up @@ -66,7 +70,52 @@ public function on403(ExceptionEvent $event): void {
$url = Url::fromRoute('entity.node.canonical', ['node' => $redirectNode])->toString();

// Set temporary redirect.
$response = new RedirectResponse($url, 307);
$response = new TrustedRedirectResponse($url, 307);
$event->setResponse($response);
}

/**
* The 404 exception listener.
*
* #UHF-8946 External service's automation is only capable of creating links to
* finnish job listings. If finnish translation doesn't exist the user will be
* automatically redirected to existing translation with matching job ID.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
* The Event to process.
*/
public function on404(ExceptionEvent $event) : void {
$uri = $event->getRequest()->getRequestUri();
$redirectPaths = [
'fi' => 'avoimet-tyopaikat/avoimet-tyopaikat/',
'sv' => 'lediga-jobb/lediga-jobb/',
'en' => 'open-jobs/open-jobs/',
];

$redirectFrom = NULL;
foreach ($redirectPaths as $path) {
if (str_contains($uri, $path)) {
$redirectFrom = $path;
break;
}
}

if (!$redirectFrom) {
return;
}

$recruitmentId = array_reverse(explode('/', $uri))[0];
$nodes = $this->entityTypeManager
->getStorage('node')
->loadByProperties(['field_recruitment_id' => $recruitmentId]);

if (!$nodes || !$node = reset($nodes)) {
return;
}

$url = $node->toUrl('canonical', ['language' => $node->language()])->toString();
$response = new TrustedRedirectResponse($url);
$response->addCacheableDependency($url);
$event->setResponse($response);
}

Expand Down