- Issue created by @SomebodySysop
- πΊπΈUnited States SomebodySysop
The closest related issue I could find is this: https://www.drupal.org/project/drupal/issues/2873293 π OutboundPathProcessorInterface does not correctly handle absolute urls anymore Closed: works as designed
Which was never resolved.
- π¦πΊAustralia larowlan π¦πΊπ.au GMT+10
Path processors are for paths not urls
What are you trying to achieve?
- πΊπΈUnited States SomebodySysop
What I wish to achieve:
Let's say I have a node, /node/210.
I have a table called "source urls". This table contains local paths I want re-directed to external sites. If a match is found, then I would like the user automatically sent to the external site.
So, when someone clicks on a link for this "/node/210" anywhere on my site, they are automatically redirected to the appropriate external site url.
The reason for this: I am building an AI response API that would allow users to embed and deploy a chatbot on their site for their content without them having to build and maintain this infrastructure themselves. So, ideally, /node/210 would consist of data from their site that we have processed for AI interaction, and rather than have the end-user access the data on our site, they are directed to the actual source.
- π¦πΊAustralia larowlan π¦πΊπ.au GMT+10
You could build that with a link field from core and the field_redirection β module
Add a link field to the node-type that contains the source URL.
Configure manage display for the node-type and set the formatter for the link field to a redirect.
That would be a zero-code approach.
If you'd prefer a code approach, I think you want an event subscriber that listens to
KernelEvents::RESPONSE
In the event subscriber you'd want something like this, this assumes you have a field called field_link_url on the node type, adapt as appropriate.
/** * Force the redirect to a link field. */ public function redirectToLinkField(ResponseEvent $event) { $request = $event->getRequest(); if ($request->get('_route') !== 'entity.node.canonical') { return; } $node = $request->get('node'); $bundle = $node->bundle(); if ($bundle == 'YOUR_NODE_TYPE') { if ($node->hasField('field_link_url') && !$node->field_link_url->isEmpty()) { $uri = $node->field_link_url->first()->getUrl()->setAbsolute(TRUE); $redirect = new TrustedRedirectResponse($uri->toString(), 301 /* pick what you want here */); $redirect->setMaxAge(2764800 /* here too */); $event->setResponse($redirect); } } }
- Status changed to Fixed
5 months ago 6:55am 18 January 2024 - πΊπΈUnited States SomebodySysop
@larowlan
Thank you VERY much for that guidance. Exactly what I needed.
Anyone else need to do something similar, this is how I utilized the Event Subscriber:
namespace Drupal\solrai\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Drupal\Core\Routing\TrustedRedirectResponse; class SolraiEventSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { $events[KernelEvents::RESPONSE][] = ['redirectToExternalUrl', 0]; return $events; } public function redirectToExternalUrl(ResponseEvent $event) { $request = $event->getRequest(); if ($request->get('_route') !== 'entity.node.canonical') { return; } $node = $request->get('node'); $nodePath = '/node/' . $node->id(); $externalUrl = $this->lookupPathInTable($nodePath); if ($externalUrl) { $redirect = new TrustedRedirectResponse($externalUrl, 301); $redirect->setMaxAge(2764800); $event->setResponse($redirect); } } private function lookupPathInTable($path) { $pathMappings = [ '/node/7849' => 'https://churchages.net/en/sermon/wesley/laying-the-foundation-of-the-new-chapel/', '/node/7850' => 'https://churchages.net/en/sermon/wesley/death-of-rev-mr-john-fletcher/' // Add more mappings as needed ]; return $pathMappings[$path] ?? null; } }
Automatically closed - issue fixed for 2 weeks with no activity.