/** * Checks if a node's type requires a redirect. * * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event * The event to process. */ public function purlCheckNodeContext(GetResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher_interface) { $route_options = $this->routeMatch->getRouteObject()->getOptions(); $isAdminRoute = array_key_exists('_admin_route', $route_options) && $route_options['_admin_route']; if (!$isAdminRoute && ($matched = $this->matchedModifiers->getMatched() && ($entity = $this->routeMatch->getParameter('node')))) { $node_type = $this->entityStorage->load($entity->bundle()); $purl_settings = $node_type->getThirdPartySettings('purl'); if (!isset($purl_settings['keep_context']) || !$purl_settings['keep_context']) { $url = \Drupal\Core\Url::fromRoute($this->routeMatch->getRouteName(), $this->routeMatch->getRawParameters()->all(), ['host' => Settings::get('purl_base_domain'), 'absolute' => TRUE]); try { $redirect_response = new TrustedRedirectResponse($url->toString()); $redirect_response->getCacheableMetadata()->setCacheMaxAge(0); $modifiers = $event->getRequest()->attributes->get('purl.matched_modifiers', []); $new_event = new ExitedContextEvent($event->getRequest(), $redirect_response, $this->routeMatch, $modifiers); $dispatcher_interface->dispatch(PurlEvents::EXITED_CONTEXT, $new_event); $event->setResponse($new_event->getResponse()); return; } catch (RedirectLoopException $e) { \Drupal::logger('redirect')->warning($e->getMessage()); $response = new Response(); $response->setStatusCode(503); $response->setContent('Service unavailable'); $event->setResponse($response); return; } } } }
public function onRequest(GetResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher) { $request = $event->getRequest(); $modifiers = $this->getModifiers(); $matches = array(); foreach ($modifiers as $modifier) { $provider = $modifier->getProvider(); $modifierKey = $modifier->getModifierKey(); $method = $modifier->getMethod(); if ($method->contains($request, $modifierKey)) { $matches[$provider->getProviderId()] = array('method' => $method, 'modifier' => $modifierKey, 'provider_key' => $provider->getProviderId(), 'provider' => $modifier->getProvider(), 'value' => $modifier->getValue()); } } foreach ($matches as $match) { if (!$match['method'] instanceof RequestAlteringInterface) { continue; } $match['method']->alterRequest($request, $match['modifier']); $this->reinitializeRequest($request); } foreach ($matches as $match) { $event = new ModifierMatchedEvent($request, $match['provider_key'], $match['method'], $match['modifier'], $match['value']); $dispatcher->dispatch(PurlEvents::MODIFIER_MATCHED, $event); $this->matchedModifiers->add($event); } $request->attributes->set('purl.matched_modifiers', $matches); }
public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) { if (array_key_exists('purl_context', $options) && $options['purl_context'] == false) { if (count($this->matchedModifiers->getMatched()) && $bubbleable_metadata) { $cacheContexts = $bubbleable_metadata->getCacheContexts(); $cacheContexts[] = 'purl'; $bubbleable_metadata->setCacheContexts($cacheContexts); } return $this->contextHelper->processOutbound($this->matchedModifiers->createContexts(Context::EXIT_CONTEXT), $path, $options, $request, $bubbleable_metadata); } return $this->contextHelper->processOutbound($this->matchedModifiers->createContexts(), $path, $options, $request, $bubbleable_metadata); }
/** * @param string|\Symfony\Component\Routing\Route $name * @param array $parameters * @param array $options * @param bool $collect_bubbleable_metadata * @return \Drupal\Core\GeneratedUrl|string */ public function generateFromRoute($name, $parameters = array(), $options = array(), $collect_bubbleable_metadata = FALSE) { $hostOverride = null; $originalHost = null; $action = array_key_exists('purl_context', $options) && $options['purl_context'] == false ? Context::EXIT_CONTEXT : Context::ENTER_CONTEXT; $this->contextHelper->preGenerate($this->matchedModifiers->createContexts($action), $name, $parameters, $options, $collect_bubbleable_metadata); if (isset($options['host']) && strlen((string) $options['host']) > 0) { $hostOverride = $options['host']; $originalHost = $this->getContext()->getHost(); $this->getContext()->setHost($hostOverride); } $result = $this->urlGenerator->generateFromRoute($name, $parameters, $options, $collect_bubbleable_metadata); // Reset the original host in request context. if ($hostOverride) { $this->getContext()->setHost($originalHost); } return $result; }