a00331188cd45b061bcb7a1de0af9f3d4b4982d4
[yaffs-website] / Controller / Fix404IgnoreController.php
1 <?php
2
3 namespace Drupal\redirect_404\Controller;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Url;
8 use Drupal\redirect_404\RedirectNotFoundStorageInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\Request;
11
12 /**
13  * Controller to ignore a path from the 'Fix 404 pages' page.
14  */
15 class Fix404IgnoreController extends ControllerBase {
16
17   /**
18    * The config factory.
19    *
20    * @var \Drupal\Core\Config\ConfigFactoryInterface
21    */
22   protected $configuration;
23
24   /**
25    * The redirect storage.
26    *
27    * @var \Drupal\redirect_404\RedirectNotFoundStorageInterface
28    */
29   protected $redirectStorage;
30
31   /**
32    * Constructs a Fix404Ignore object.
33    *
34    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35    *   The config factory.
36    * @param \Drupal\redirect_404\RedirectNotFoundStorageInterface $redirect_storage
37    *   A redirect storage.
38    */
39   public function __construct(ConfigFactoryInterface $config_factory, RedirectNotFoundStorageInterface $redirect_storage) {
40     $this->configuration = $config_factory;
41     $this->redirectStorage = $redirect_storage;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('config.factory'),
50       $container->get('redirect.not_found_storage')
51     );
52   }
53
54   /**
55    * Adds path into the ignored list.
56    *
57    * @param \Symfony\Component\HttpFoundation\Request $request
58    *   The HttpRequest object representing the current request.
59    *
60    * @return \Symfony\Component\HttpFoundation\RedirectResponse
61    */
62   public function ignorePath(Request $request) {
63     $ignored_paths = $this->config('redirect_404.settings')->get('pages');
64     $path = $request->query->get('path');
65     $langcode = $request->query->get('langcode');
66
67     if (empty($ignored_paths) || !strpos($path, $ignored_paths)) {
68       $this->redirectStorage->resolveLogRequest($path, $langcode);
69
70       drupal_set_message($this->t('Resolved the path %path in the database. Please check the ignored list and save the settings.', [
71         '%path' => $path,
72       ]));
73     }
74
75     $options = [
76       'query' => [
77         'ignore' => $path,
78         'destination' => Url::fromRoute('redirect_404.fix_404')->getInternalPath(),
79       ],
80     ];
81
82     return $this->redirect('redirect.settings', [], $options);
83   }
84
85 }