0

I am using a custom module to create a new page on my Drupal 8 site.

My routing.yml file is configured like this:

mymodule.print:
  path: '/print-mymodule'
  defaults:
    _controller: \Drupal\mymodule\Controller\MymodulePrintController::content
  requirements:
    _permission: 'view mymodule entity'
  options:
    _auth: ['basic_auth']

MymodulePrintController::content is as follows:

public function content() {
    return array(
        '#type' => 'page',
        '#theme' => 'mytheme'
    );
}

mytheme is used everywhere else on my site.

In mytheme.theme, I also have a mytheme_preprocess() function which is adding some blocks to the page. I am guessing the theme is being detected properly because this function is executing just fine. The template I am using is named according to twig's suggestions and is loading fine.

The problem is that the content() function is not really outputting a "page". Using Postman, I see that the entire contents of the response is just a div, there is no <html> tag, no <head>, no <body>, nothing else except what is defined in the one twig file and mytheme_preprocess. I need this page to be using this theme's css styles like the rest of my pages are.

How can I accomplish this? Every guide/question I've seen on the topic so far either is just returning a render array in the content() function like I already am doing, or is suggesting to use Response classes without enough detail for me to really understand.

  • 1
    I think you're conceptually doing something wrong here. What is it that you actually want to achieve? Your controller shouldn't return #type page, usually a controller just provides the main content which is then surrounded by the blocks and all of that makes up the page, but that's then automatically built for you. – Berdir Dec 19 '16 at 20:31
  • @Berdir I'm trying to make a page that just contains a few paragraphs and a series of predefined blocks that a wkhtmltopdf function can see (it for whatever reason ignores blocks inserted via the block structure interface so I don't have the option of doing this through the drupal GUI), which is supposed to be very stripped down and printer-friendly, but this still needs some table styles and such. The content has to be restricted to only certain users and accessible with either a session cookie or a basic auth header. The cookie didn't work, so I'm going with basic auth. – saramm1 Dec 19 '16 at 20:35
  • (ran out of space in my last comment) Basically how I want to accomplish this is similar to adding standalone forms to routing.yml files, which I've done before, the page templates seems to work with that since form classes have a buildForm function. I just want to do something similar except just a static page and not a form. – saramm1 Dec 19 '16 at 20:49
  • 1
    Not sure I 100% understand what you want. Try just a singe #markup => 'hello world' in your controller. And then combine that with either a page--print-mymodule.html.twig (see suggetsions) where you don't print the regions you don't want or by implementing hook_block_access() and deny view access to all blocks that you don't want to see (probably by only allowing certain blocks) or configure them with path based settings by hand. Does that go in the right direction? – Berdir Dec 19 '16 at 21:06
  • @Berdir I have tried that, but a template with "page" in the name isn't detected by twig's template suggestions. It is only picking up a template that starts with "html--" – saramm1 Dec 19 '16 at 21:10
0

When you generate an entity with drupal console you get a twig template for the content of the entity. This is not a page and not a theme. If you want to use a page template, that is specific to the entity, then you use a template name suggestion, for exampe page--myentity.html.twig. The content from the entity controller will end up like any other content inside of this page in the region content if you use the standard block layout, where the block "Main page content" is placed in this region.

You can remove anything else from the page template what you don't need and replace this with static html.

  • Sorry if I'm not fully understanding, are you saying I would have to create a new entity type to accomplish what I want? – saramm1 Dec 19 '16 at 21:31
  • No, the name for the page template is an example. Use twig debug to see what name suggestions are available for page.html.twig. – 4k4 Dec 19 '16 at 21:46
  • Oh ok, that's what I am doing, it is picking up my template but that template is not a "page" template. – saramm1 Dec 20 '16 at 13:50
  • This is the page template: cgit.drupalcode.org/drupal/tree/core/themes/stable/templates/…. You only need the region "page.content". You can remove anything else, like the menus, if you don't want them in the pdf. – 4k4 Dec 20 '16 at 14:01

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.