1

I'm creating a module that will render a few nodes before getting back the result and print it as PDF. (I can't use wkhtmltopdf; I wish I could.)

So far, I managed to make my controller/action use the template I wanted, and anything I write in the template file is showed with die($html). However, I can't find any example of how to pass variables to the view; I tried a few things, but I got nothing, excepted 500 errors.

This is the code I am using.

public function nodeAction(NodeInterface $node) {
  // Here, I define a variable named $responses that contains a lot of datas as arrays
  $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  $build = $view_builder->view($node, 'cnas_pdf');
  $html = \Drupal::service('renderer')->renderPlain($build, ['var' => $response]);

  // No error in this configuration, but "var" is nowhere to be found
  die($html);
}

2 Answers 2

3

This is not possible to add parameters to renderPlain(). What you can do depends. If you want to use a variable inside of the node, then do this in a node preprocess hook. But more likely you want to add additional elements to the build array before you render it, for example:

$build = [];
$build['top'] = ['#markup' => 'abc'];
$build['node'] = $view_builder->view($node, 'cnas_pdf');
$build['footer'] = ['#markup' => 'xyz'];
1
  • Thank you, I guess I'll have to use the preprocess_hook(), thank you
    – Jaeger
    Sep 6, 2017 at 9:40
4

This might be a slightly different scenario than your original question, but I stumbled across while searching for ways to add custom variables to an entity's template at the time that I build it with the ViewBuilder, versus altering existing variables in a preprocess. Hopefully this should be helpful.

The ViewBuilder's view() method returns a render array, so you can use that to pass your custom variables to the template.

public function nodeAction(NodeInterface $node) {
  // Here, I define a variable named $responses that contains a lot of datas as arrays
  $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  $build = $view_builder->view($node, 'cnas_pdf');
  $build['#my_response'] = $response;
  $html = \Drupal::service('renderer')->renderPlain($build);

  // No error in this configuration, but "var" is nowhere to be found
  die($html);
}

After modifying the $build to include the new array key, you should have access to it in your node's template, such as with {{ elements['#my_response'] }} in this case. If you wanted to make this a bit cleaner in a preprocess, you could then do something like:

/**
 * Implements theme_preprocess_node().
 */
function mytheme_preprocess_node(&$variables) {
  if (isset($variables['elements']['#my_response'])) {
    $variables['my_response'] = $variables['elements']['#my_response'];
  }
}

You would then be able to access it in your template directly with {{ my_response }}.

2
  • Pretty clever solution. Nice!
    – ec0g
    Dec 12, 2019 at 16:01
  • This is brilliant, thanks a lot.
    – Gabriele
    May 17, 2022 at 14:48

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.