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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Jake Lacey
  • 623
  • 7
  • 24
  • Btw. You should use `\Drupal::service('renderer')->renderRoot()` instead of deprecated `drupal_render()`. – kenorb Aug 05 '17 at 10:53

1 Answers1

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?

Jake Lacey
  • 623
  • 7
  • 24
  • 1
    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. – Eyal Dec 06 '15 at 22:38
  • Hi @Eyal 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. – Jake Lacey Dec 13 '15 at 12:52
  • 1
    the best practice is to use a controller and return a render array. The render array will be placed inside the main content block. – Eyal Dec 14 '15 at 20:47
  • How do we do this If I need to theme a new content added to node using hook_node_view() ?? – Fazeela Abu Zohra Feb 10 '16 at 08:36