I have a custom search form which appears in the modal and is accessible only to logged in users. The ajax url of the form is /my/custom/search/modal/{nojs}. The router definition is the following one.

  path: '/my/custom/search/modal/{nojs}'
    _form:  '\Drupal\mymodule\Form\MyCustomSearchForm'
    _title: 'My Custom search'
    nojs: ajax
    _user_is_logged_in: 'TRUE'

How do I redirect users to the login page when the anonymous user tries to access /my/custom/search/modal/{nojs} via an AJAX call?

1 Answer 1


There are a couple of modules that do this

This page was old, but looked like it had been updated along the way for Drupal 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.