I create a block at /admin/structure/block/block-content
and I want programmatically render it in our custom template files. What I did is
function hook_preprocess_page(&$vars) {
$block = \Drupal\block_content\Entity\BlockContent::load(2);
$vars['contact-us-info-block']= \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);
//kint( ($vars['contact-us-info-block']));
//kint( drupal_render($vars['contact-us-info-block']));
}
and in page template file I try
<p>{{ contact-us-info-block }} </p>
but I only see a "0" in output.
if I use
$vars['contact-us-info-block'] = drupal_render($vars['contact-us-info-block']);
everything is Ok but I find out drupal_render is deprecated in Drupal 8. Where is problem and how can I render a block at page template programmatically?
\Drupal::service('renderer')->renderRoot()
instead ofdrupal_render()
.