3

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?

1
  • Btw. You should use \Drupal::service('renderer')->renderRoot() instead of drupal_render().
    – kenorb
    Commented Aug 4, 2017 at 21:53

3 Answers 3

1

For display only your block in your templates with preprocess the best way is

$block = Block::load('my_block_id');
$variables['My_region'] = \Drupal::entityManager()
      ->getViewBuilder('block')
      ->view($block);

And in your page.html.twig or node.html.twig or xxx.html.twig use your variable My_region like this :

{% if page.My_region %}
    {{ page.My_region }}
{% endif %}

And in rendable array (custom module) by exemple into an controller custom in content():

public function content() {
    $block = Block::load('my_block_id');
    $block_content = \Drupal::entityManager()
      ->getViewBuilder('block')
      ->view($block);

          return array(
            '#type' => 'container',
            '#attributes' => array(
            'class' => array("Myclass"),
          ),
           "element-content" => $block_content,
           '#weight' => 0,
  );
}

drupal_render is not usefull drupal already assume the render in D8 and this is deprecated (https://api.drupal.org/api/drupal/core!includes!common.inc/function/drupal_render/8)

it's a bit heavy, it is better to use the maximum area system and does not add load block from the preprocess. In the case of using a controller in your modules this seems a justified use.

1
  • 1
    did you read my question or only copy/paste this from another Question( I saw this before)?
    – Yuseferi
    Commented Jun 29, 2016 at 9:24
1

Attentively see topic: https://drupaldeveloper.in/codelet/drupal8-set-block-region-programmatically

You add and display block correctly but have some mistakes in your code.

The first mistake when you load block:

$block = \Drupal\block_content\Entity\BlockContent::load(2);

Do you have block with id "2"? As I understand you have block with id "block_content" and you need load this block.

After that you need add block to variables and you did, but make mistake in method getViewBuilder() you must set 'block', not 'block_content'.

Also, you don't need to use drupal_render. So the final correct code must be next:

function hook_preprocess_page(&$vars) {

    $block = \Drupal\block_content\Entity\BlockContent::load('block_content');
    $vars['contact-us-info-block']= \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);
}
-1
function hook_preprocess_page(&$vars) {
  $block = \Drupal\block_content\Entity\BlockContent::load(2);
  $blockview = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);
  $vars['contact-us-info-block'] = render($blockview);
}
1

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.