I'm developing a site using Drupal 8 beta-14. I've created a view block of different terms and now I want to display it using code. How can I display it programatically?
I used to do it in Drupal 7 using this code but I'm confused about Drupal 8.
$block = module_invoke('block', 'block_view', '4');
$text_block = render($block['content']);
|
|||
|
For display only your block in your templates with preprocess the best way is
And in your page.html.twig or node.html.twig or xxx.html.twig use your variable My_region like this :
And in rendable array (custom module) by exemple into an controller custom in content() :
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. |
|||
|
There are two types of blocks, and the method for rendering the two is a bit different: Content BlocksContent blocks are blocks that you create in the interface. They are much like nodes configurable data structures, with fields etc. If you want to render one of these, you can do what you would normally do with entities, load them and render them with the view builder:
Plugin blocksBlocks can also be plugin, defined in various modules. An example could be the breadcrumb block. If you want to render these, you will need to use the block plugin manager.
Config entitiesShared for the two types are blocks, are that once you insert them into a region, you will create a config entity that has all of the settings for the block. In some cases it will be more useful handling config entities. Since the same block can be place in multiple regions with and with different configuration, it can get more tricky using the block config entities. The nice thing is that you might want to render a block with specific configuration, the bad thing is that config ids can change by messing with the interface, so the code might end up not working after letting users use the block interface.
|
|||||||||||||
|
|
|||||
|
Based on my research, you could base the code from How to render a block programmatically in drupal 8. You could also change
into something as simple as |
|||||||||
|
You get block output:
And then you may return output in different ways:
or:
|
|||||||||
|