Passing variables to twig using hook_theme within a module
Asked Answered
J

1

6

I'm fully aware how to do this in Drupal 7 so I will explain what I would normally do using Drupal 7.

When making a custom module I use hook_theme a lot, it is very powerful and reusable!

/**
 * Implements hook_theme().
 */
function MODULE_theme() {
    $themes = array();

    $themes['name_of_theme'] = array(
      'path' => drupal_get_path('module', 'module') .'/templates',
      'template' => 'NAME_OF_TEPLATE',
      'variables' => array(
        'param1' => NULL,
        'param2' => NULL,
      ),
    );

    return $themes;
}

I would then call this theme using

theme('name_of_theme', array(
   'param1' => 'VALUEA',
   'param2' => 'VALUEB'
)); 

This would then return html and I would be happy.

So Drupal 8 is out need to get to grips with it.

/**
 * Implements hook_theme().
 */
function helloworld_theme() {
  $theme = [];

  $theme['helloworld'] = [
    'variables' => [
      'param_1' => [],
      'param_2' => 'hello',
    ]
  ];

  return $theme;
}

and within my controller I'm am using

$hello_world_template = array(
  '#theme' => 'helloworld',
  'variables' => [
    'param_1' => 'hello world',
    'param_2' => 'hello from another world'
  ],
);

$output = drupal_render($hello_world_template,
  array(
    'variables' => array(
      'param_1' => $param_1,
      'param_2' => $param_2,
    )
  )
);

return [
    '#type' => 'markup',
    '#markup' => $output
];

I am getting an output on of my template, however what Im not sure about is where to pass my parameters so that they are available in my template (just to point out that my variables are available they are just null as defined in hook_theme)

I'm also open to the idea that I might be doing the fundamentally wrong and I'm open to an alternative route if my method is not best practise.

Jecoa answered 5/12, 2015 at 19:30 Comment(1)
Btw. You should use \Drupal::service('renderer')->renderRoot() instead of deprecated drupal_render().Appendicitis
J
2

Found the issue,

changing this,

$hello_world_template = array(
  '#theme' => 'helloworld',
  'variables' => [
    'param_1' => 'hello world',
    'param_2' => 'hello from another world'
  ],
);

to this,

$hello_world_template = array(
  '#theme' => 'helloworld',
  '#param_1' => $param_1,
  '#param_2' => $param_2
);

Im now able see the variables I'm passing.

I am still open for a better option?

Jecoa answered 6/12, 2015 at 12:12 Comment(4)
Suggestion don't call the drupal render method. Keep it as a render array add much as possible and preferably let drupal render it when it needs.Prokofiev
Hi @Prokofiev whats a best practise for making a module page? Create a controller then inside that return an array? I'm not to sure what would then render this array. I don't know if I'm making sense but when creating a module page we are retuning it to the content of the theme. unless its a block then your comments makes more sense.Jecoa
the best practice is to use a controller and return a render array. The render array will be placed inside the main content block.Prokofiev
How do we do this If I need to theme a new content added to node using hook_node_view() ??Ask

© 2022 - 2024 — McMap. All rights reserved.