diff --git a/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php b/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php index 5e6e85488..68e80a38a 100644 --- a/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php +++ b/modules/tide_site/src/EventSubscriber/TideSiteRequestEventSubscriber.php @@ -109,6 +109,25 @@ public function onRequestAddSiteFilter(RequestEvent $event) { $path = $api_helper->getRequestedPath($request); // Only prefix non-homepage and unrouted path. if ($path !== '/') { + // If the path already has a site prefix, return a redirect response + // in the same format as Drupal redirect module so the FE can handle + // it with its existing redirect logic. + if ($this->helper->hasSitePrefix($path)) { + $clean_path = preg_replace('#^/site-\d+/#', '/', $path); + $this->setRedirectRouteResponse($event, $request, $clean_path); + return; + } + // If the path is an internal node path (e.g. /node/1234), resolve + // to its alias and return a redirect response. + if (preg_match('#^/node/\d+$#', $path)) { + /** @var \Drupal\path_alias\AliasManagerInterface $alias_manager */ + $alias_manager = $this->container->get('path_alias.manager'); + $alias = $alias_manager->getAliasByPath($path); + if ($alias && $alias !== $path) { + $this->setRedirectRouteResponse($event, $request, $alias); + return; + } + } try { $url = Url::fromUri('internal:' . $path); if (!$url->isRouted() && !$this->helper->hasSitePrefix($path)) { @@ -280,6 +299,38 @@ protected function setEventErrorResponse(RequestEvent $event, $error_message, $c $event->stopPropagation(); } + /** + * Set a redirect route JSON response matching Drupal redirect module format. + */ + protected function setRedirectRouteResponse(RequestEvent $event, Request $request, $redirect_url, $status_code = '301') { + $self_href = $request->getSchemeAndHttpHost() . $request->getRequestUri(); + /** @var \Drupal\Component\Uuid\UuidInterface $uuid_service */ + $uuid_service = $this->container->get('uuid'); + $json_response = [ + 'data' => [ + 'type' => 'route', + 'links' => [ + 'self' => [ + 'href' => $self_href, + ], + ], + 'id' => $uuid_service->generate(), + 'attributes' => [ + 'status_code' => $status_code, + 'redirect_url' => $redirect_url, + 'redirect_type' => 'internal', + ], + ], + 'links' => [ + 'self' => [ + 'href' => $self_href, + ], + ], + ]; + $response = new JsonResponse($json_response); + $event->setResponse($response); + } + /** * Helper to build field name for provided entity type. *