1

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?

3
  • You shoudn't do this: $output['#output'] = $output; But try adding a '#type' => 'markup' to your #markup - items Jun 25, 2021 at 10:41
  • That made no change. Additionally the fieldset isn't being pulled in either.
    – Ex0r
    Jun 25, 2021 at 11:03
  • 3
    You have a single underscore in the return['#theme'], but a double hyphen in the template name.
    – beltouche
    Jun 25, 2021 at 15:10

1 Answer 1

0

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;
}

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.