0

On Drupal 8, I'm looking for a way for creating an empty page (a page without any theme) that contains JavaScript code.
I created a controller via Drupal Console, and I searched for an example on the web, but I found only very simple examples.

Is it possible to create an empty page accessible via a controller and add JavaScript code to this page?

1 Answer 1

2

Controllers must return a response (you will get an error if you don't). Your options are to return a render array, an HtmlResponse, a JsonResponse, or an AjaxResponse.

Edit: Or as Kevin posted below, a Response.

By the sounds of it, you are wanting to return JSON, so JsonResponse is likely what you are looking for.

3
  • 1
    If you also use the Symfony Response class, I believe you will get what the OP is asking. It will by pass all the Drupal related events and serve it. Although unless you're an expert, I don't see where that is too useful.
    – Kevin
    Commented Apr 27, 2017 at 12:35
  • @Kevin, I agree, a symfony response is probably not useful here, because it bypasses drupal completely and the OP wants to attach javascript libraries in a drupal way. In this case it's better to use a custom page.html.twig and remove anything to get an empty page.
    – 4uk4
    Commented Apr 27, 2017 at 13:03
  • For the question after the edit (javascript in a drupal way removed) you can simply put the javascript in the symfony response in a <script> tag.
    – 4uk4
    Commented Apr 27, 2017 at 13:10

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.