Commit 7c1031b2 authored by Jyri-Petteri Paloposki's avatar Jyri-Petteri Paloposki Committed by Phil Stewart
Browse files

Issue #3219321 by ZeiP, pstewart: Redirecting to custom 404 page is an anti-pattern

parent a2947040
Loading
Loading
Loading
Loading
+52 −19
Changes for src/Controller/DefaultController.php: 52 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -7,15 +7,54 @@
namespace Drupal\notfoundpassthrough\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Routing\LocalRedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Default controller for the notfoundpassthrough module.
 */
class DefaultController extends ControllerBase {
class DefaultController extends ControllerBase implements ContainerInjectionInterface {
  /**
   * Symfony\Component\HttpKernel\HttpKernelInterface definition.
   *
   * @var Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * An instance of the "request_stack" service.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  public function __construct(HttpKernelInterface $http_kernel, RequestStack $request_stack, LanguageManagerInterface $language_manager) {
    $this->httpKernel = $http_kernel;
    $this->requestStack = $request_stack;
    $this->languageManager = $language_manager;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('http_kernel.basic'),
      $container->get('request_stack'),
      $container->get('language_manager')
    );
  }

  public function _notfoundpassthrough_title() {
    return $this->config('notfoundpassthrough.settings')->get('title');
@@ -128,23 +167,17 @@ class DefaultController extends ControllerBase {
    if (!$new_status && !$new_path) {
      $redirect_path = $this->config('notfoundpassthrough.settings')->get('site_404');
      if ($redirect_path) {
        // Remove the destination because Drupal 8 is trying to drive its
        // developers to suicide.  There is a proposed fix in this issue:
        // https://www.drupal.org/project/drupal/issues/2950883
        \Drupal::request()->query->remove('destination');
        $new_status = '302 Found';
        $new_status_code = 302;
        $url = Url::fromUserInput($redirect_path)
          ->setAbsolute()
          ->toString(TRUE);
        // Because of the somewhat nightmarish early rendering / cacheability
        // issue described in #3032255) let's do the redirect separately.
        $new_path = $url->getGeneratedUrl();
        $response = new LocalRedirectResponse($new_path, $new_status_code);
        \Drupal::logger('notfoundpassthrough')->notice('Redirected to 404 path ' . $new_path);
        // Add the $url object as a dependency of [ the response ]
        $response->addCacheableDependency($url);
        return $response;
        if ($this->languageManager->isMultilingual()) {
          $language = $this->languageManager->getCurrentLanguage()->getId();
          $redirect_path = '/' . $language . $redirect_path;
        }
        $sub_request = Request::create($redirect_path, 'GET');
        if ($session = $this->requestStack->getCurrentRequest()->getSession()) {
          $sub_request->setSession($session);
        }
        $sub_response = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);

        return $sub_response;
      }
    }
    // If there is a status and a path, we go there.