Score:1

Making custom template & variables in module

cn flag

I am working with D8, and don't usually do themeing, so I am stuck on a problem.

I have a custom route in a custom module that calls a method checkinConfirmation() on a controller.

Inside that controller, I have this:

public function checkinConfirmation() {
  $output['items'] = [
    '#type' => 'fieldset',
    '#title' => t('Checked in items'),
  ];
  $output['items']['name'] = [
    '#prefix' => '<h2>',
    '#suffix' => '</h2>',
    '#markup' => t('Name'),
  ];
  $output['#theme'] = 'circulation_confirmation';
  $output['#output'] = $output;

  return $output;
}

and inside of my theme, I have this inside of hook_theme()

public function mytheme_theme() {
  $theme['circulation_confirmation'] = [
    'template' => 'page/circulation--confirmation',
    'variables' => [
      'output' => [],
    ],
  ];
}

In my template, I have a {{ output }} variable to print the rendered output provided from $output. The problem is though, I do not get the items in the template, and I cannot figure out why. The template is getting pulled in, as the wrapper in the template file is being applied on the page, and the main container is the width it should be.

In using this instead (in the controller):

return [
  '#theme' => 'circulation_confirmation',
  '#output' => $output,
];

I at least get the data, but it doesn't appear to be using the template I provided as the wrapper is now gone and the main container is full width instead of the width it should be.

I don't usually do themeing or templating, and when I do it's usually forms so I can just pass it 'render element' => 'form', to get it to pass the form items to the theme. I am not sure what I am doing wrong.

Please help?

Rainer Feike avatar
in flag
You shoudn't do this: $output['#output'] = $output; But try adding a '#type' => 'markup' to your #markup - items
Ex0r avatar
cn flag
That made no change. Additionally the fieldset isn't being pulled in either.
beltouche avatar
cn flag
You have a single underscore in the return['#theme'], but a double hyphen in the template name.
Score:0
in flag

You can try this:

In your custom_module.module:

function custom_module_theme() {
    return [
        'circulation_confirmation' => [
            'render element' => 'output',
            'variables' => [
                'output' => [],
            ],
        ],
    ];

And in your controller:

public function checkinConfirmation() {
    $rtn = [
        '#theme' => 'circulation_confirmation',
        '#output' => [],
    ];

    $rtn['#output']['items'] = [
        '#type' => 'fieldset',
        '#title' => t('Checked in items'),
    ];

    $rtn['#output']['items']['name'] = [
        '#prefix' => '<h2>',
        '#suffix' => '</h2>',
        '#markup' => t('Name'),
    ];

    return $rtn;
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.