Squares, as the name recommends, are bits of substance that can be set anyplace on your Drupal site. They can contain straightforward content, structures or something with complex rationale.
In this instructional exercise, you’ll figure out how to make a square utilizing custom code and how to utilize Drupal Console to create it. On the off chance that you’ve utilized squares in Drupal 7, at that point you will be comfortable with the new interface in Drupal 8. In case you’re a site manufacturer, the entire cycle of making, altering and erasing a square is instinctive.
Prior to hopping into code, we should discuss the Drupal Block UI and comprehend what has changed since Drupal 7.
In hook_preprocess_page() add the following code:
$block_entity=Drupal\block_content\Entity\BlockContent::load(BLOCK_ID);// BLOCK_ID
$block_view=\Drupal::entityTypeManager()->getViewBuilder(‘block_content’)->view($block_entity, ‘DISPLAY_VIEW_NAME’);
if ($block_view) {
$variables[‘page’][‘your_block’] = $block_view;
}
In your Twig template following code put with block.
{% if page.your_block%}
{{ page.your_block}}
{% endif %}
Render custom blocks
$bid = ‘myblock’;
$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityTypeManager()->getViewBuilder(‘block_content’)->view($block);
Render plugin blocks using Programmatically
$block_manager = \Drupal::service(‘plugin.manager.block’);
$config = [];
$plugin_block = $block_manager->createInstance(‘system_yourblock_block’, $config);
$access_result = $plugin_block->access(\Drupal::currentUser());
if (is_object($access_result) && $access_result->isForbidden() || is_bool($access_result) && !$access_result) {
return [];
}
$render = $plugin_block->build();
if you have any query about Drupal Module Development then comment.