In a custom controller, when I try to use drupal_set_message(), it works only if I use the following code.

// The message is shown via {{ page.highlighted }}.
drupal_set_message('Test error', 'error');

return [
   '#type' => 'markup',
   '#markup' => "Welcome",

When I use the following code, it doesn't work.

drupal_set_message('Test error', 'error');

return [
   '#type' => 'page',
   '#markup' => "Welcome",

I need to set #type to page to be able to inject custom variables.

Can anybody help me to understand how it works?

1 Answer 1


You might want to try the following code.

return [
  '#theme' => 'ctr',
  '#myvar' => 'myvalue',

The module needs also to implement hook_theme().

 * Implements hook_theme().
function ctr_theme($existing, $type, $theme, $path) {
  $variables = array(
    'ctr' => array(
      'template' => 'cntr',
      'variables' => array(
        'myvar' => 'defaultvalue',
  return $variables;

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.