I need to display custom logic in 404 pages so I have overridden the route controller of the system.404 route like this:
class RouteSubscriber extends RouteSubscriberBase {
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('system.404')) {
$route->setDefault('_controller', '\Drupal\mymodule\Controller\myController::on404');
}
}
}
So let's say I am currently at the path /non-existing-path and the on404 callback renders my custom content just fine. Problem is I am also rendering a form that contains an Ajax element (a button), which doesn't work because apparently the Ajax request is made towards the following path:
/system/404?q=non-existing-path&destination=non-existing-path&_exception_statuscode=404&ajax_form=1&_wrapper_format=drupal_ajax
which, not unexpectedly, returns a Status Code: 404 Not Found
What am I missing?
--
So far I've tried intercepting the NotFoundHttpException with my own listener and changing it to a HttpException(200) in hopes I could turn a 404 page into a 200 but I am at a loss after that and it seems like an overkill.