3 namespace Drupal\redirect_404\EventSubscriber;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\Core\Path\PathMatcherInterface;
8 use Drupal\redirect_404\RedirectNotFoundStorageInterface;
9 use Drupal\Core\Path\CurrentPathStack;
10 use Symfony\Component\HttpFoundation\RequestStack;
11 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13 use Symfony\Component\HttpKernel\KernelEvents;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17 * An EventSubscriber that listens to redirect 404 errors.
19 class Redirect404Subscriber implements EventSubscriberInterface {
24 * @var \Drupal\Core\Path\CurrentPathStack
26 protected $currentPath;
31 * @var \Drupal\Core\Path\PathMatcherInterface
33 protected $pathMatcher;
36 * The request stack (get the URL argument(s) and combined it with the path).
38 * @var \Symfony\Component\HttpFoundation\RequestStack
40 protected $requestStack;
43 * The language manager.
45 * @var \Drupal\Core\Language\LanguageManagerInterface
47 protected $languageManager;
50 * The redirect storage.
52 * @var \Drupal\redirect_404\RedirectNotFoundStorageInterface
54 protected $redirectStorage;
57 * The configuration factory.
59 * @var \Drupal\Core\Config\ConfigFactoryInterface
64 * Constructs a new Redirect404Subscriber.
66 * @param \Drupal\Core\Path\CurrentPathStack $current_path
68 * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
69 * The path matcher service.
70 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
72 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
73 * The language manager.
74 * @param \Drupal\redirect_404\RedirectNotFoundStorageInterface $redirect_storage
76 * @param \Drupal\Core\Config\ConfigFactoryInterface $config
77 * The configuration factory.
79 public function __construct(CurrentPathStack $current_path, PathMatcherInterface $path_matcher, RequestStack $request_stack, LanguageManagerInterface $language_manager, RedirectNotFoundStorageInterface $redirect_storage, ConfigFactoryInterface $config) {
80 $this->currentPath = $current_path;
81 $this->pathMatcher = $path_matcher;
82 $this->requestStack = $request_stack;
83 $this->languageManager = $language_manager;
84 $this->redirectStorage = $redirect_storage;
85 $this->config = $config->get('redirect_404.settings');
91 public static function getSubscribedEvents() {
92 $events[KernelEvents::EXCEPTION][] = 'onKernelException';
97 * Logs an exception of 404 Redirect errors.
99 * @param GetResponseForExceptionEvent $event
100 * Is given by the event dispatcher.
102 public function onKernelException(GetResponseForExceptionEvent $event) {
103 // Only log page not found (404) errors.
104 if ($event->getException() instanceof NotFoundHttpException) {
105 $path = $this->currentPath->getPath();
107 // Ignore paths specified in the redirect settings.
108 if ($pages = mb_strtolower($this->config->get('pages'))) {
109 // Do not trim a trailing slash if that is the complete path.
110 $path_to_match = $path === '/' ? $path : rtrim($path, '/');
112 if ($this->pathMatcher->matchPath(mb_strtolower($path_to_match), $pages)) {
117 // Allow to store paths with arguments.
118 if ($query_string = $this->requestStack->getCurrentRequest()->getQueryString()) {
119 $query_string = '?' . $query_string;
121 $path .= $query_string;
122 $langcode = $this->languageManager->getCurrentLanguage()->getId();
125 $this->redirectStorage->logRequest($path, $langcode);