2

I want to output a google map div with a side panel in my drupal 8 moodule. So no need for blocks surrounding my main content.

The question is how do I tell Drupal to only render main content using a Twig template of my module without outputting the entire Drupal theme stuff a long with the menus and the blocks?

All online materials I found so far talks about concepts behind Drupal rendering mechanism and render arrays but none really tell something about their implementation.

Take for instance, the concept of page variants in the following link. HTMLRenderer fires an event to determine which type of page will be used to wrap the main content around: RenderEvents::SELECT_PAGE_DISPLAY_VARIANT. Its also says the SimplePageVariant, which will only output the main content, is suppose to be used by default but in case the Block module is enabled like my case, the BlockPageVariant kicks in to allow the placement of blocks in the regions around the main content. It continues, if you want, you can subscribe to this event in your own module and provide your own variant. However, I couldn't figure out how to do this subscribing.

I feel what I want is simpler than creating my own Page variant. I just want to output simple html with two divs in the Twig template of my own module.

Anyone has an idea what to do?

Edit

So there are two ways to do this as mentioned in the answer below, I will just summarize them and add a little more clarification of naming convention in drupal 8:

Return a basic Symfony response with out any drupal specific rendering:

$element['#title'] = 'Title';
$html = \Drupal::service('renderer')->renderRoot($element);
$response = new Response();
$response->setContent($html);
return $response;

Doing so, you will not be able to attach css or js libraries with render arrays. However, if you want to do that you can override the page.html.twig by following dupral 8 template naming conventions link with out returning "pure symfony" response and hindering drupal from finishing its theming.

4

There are two similar questions.

Problems creating a custom blank page with my custom module

How to output from custom module without rest of theme

You can use a symfony response or replace the page template with a blank twig in hook_theme().

  • Ran into another problem Using a pure symfony response it seems doesn't allow you to attach assets - JS or CSS files to the render array. Doing so: $build['#attached']['library'] = 'mymodule/my-lib'; with the render array should output an html head tag and js/css files with the page but the attached library gets neglected for some reason. Then what is inside the twig template only gets printed out, even <html> is not present. Tried many things but none worked. My fear is that I might need to use {% block javascripts %} in the twig template, how to reference my js files then? Any idea? – Moqbel Apr 30 '16 at 15:28
  • By using a pure symfony response there is no html template. From your comment it seems, that you want to keep the html template and only override the page template. Did you try the code in the second question, I mean the question itself, because the OP has updated the code in the question with what was discussed in the comments. – 4k4 Apr 30 '16 at 16:24
  • Thanks for your reply 4k4, but I don't understand what is the difference between the html template and page template. Are you referring to the website general theme vs the module twig template? In your previous answer what do you mean by: Override page.html.twig and html.html.twig, remove anything out of this templates and put only that in what you want. Where are both files? – Moqbel Apr 30 '16 at 18:07
  • What is really daunting is when using pure symfony response you could still use the twig template of your module and outputting stuff such as {{ name }} still works but when using {{ attach_library() }} function directly from twig, it gets totally bypassed... – Moqbel Apr 30 '16 at 18:14
  • ... bypassing the theme is why you would use a symfony response. And without a theme you don't have attach_library, but also hundreds of other things. Perhaps you only want to override page.html.twig. Normally you do this in the theme. I would start with this, because it is straightforward. To get a feeling for it, remove anything from page.html.twig, so you have no menu, no blocks, nothing and put back step by step parts of the original twig. When you have the result you want you can try to move the twig to the module. But you only need this, if you want to distribute it with the module. – 4k4 Apr 30 '16 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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