1

I've followed some previous solutions on DA to programmatically load a Custom block in my page preprocessor hook but nothing I try seems to be working.

Currently, my custom block is within Structure > Block Layout > Custom block Library under the name 'SGT Pricing Header' (Machine name: sgt_pricing_header, ID 12).

I have attempted loading it in the page preprocessor as well as the node preprocessor hooks.

Currently I have:

function THEME_preprocess_node(&$variables) {
    $block_entity = Drupal\block_content\Entity\BlockContent::load(12);// BLOCK_ID
    $block_view = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block_entity, 'sgt_pricing_header');
    $variables['block_pricing_header'] = $block_view;
}

I have also used 'sgt_pricing_header' in place of the ID 2 with no success.

In the TWIG template, which is a paragraph template by the way, if that is relevant, I am simply trying to display the block with

{{ block_pricing_header }}

I referenced Programatically render a block in a twig template when trying to get this to work.

I also tried using Twig Tweak to display the block, without success.

{{ drupal_block('sgt_pricing_header') }}

Simply does nothing.

PHP Version: 5.6.32-1+ubuntu17.10.1+deb.sury.org+1 Drupal version: Lightning (lightning-8.x-2.17)

Not entirely certain what to try next other than simply hard code the block content into the template which I would prefer to avoid.

3 Answers 3

1

I think the problem here is that Drupal 8 has both block definitions (a.k.a. plugins) and block instances. Both of which tend to be called blocks. What is listed in the custom block library is a block definition. This is like what is done when a block is created with code. It define the block but not the configuration.

An instance is the configuration data for a block. It has the settings for a specific block. E.g., title override, display title, region, and the like.

So, you are trying to render the block definition with code that requires an instance block.

How do you get an instance of a block to render? The simplest way is to create a hidden region in your theme by adding a region line in the info file. Something like "Hidden Blocks".

Add your custom (or code based) blocks to this region in the block placement screen. I also tend to set the rules for where this can be displayed just to help keep these from being generated when not needed.

Once you do this, you should be able to render using the block id from the placement screen. Note this can be different from the plug-in id on the custom block page if you have placed it more than one.

1
  • Ok, I used my original code, then added an 'instance' like you explained to the content region, configured it to appear on the content type I needed. Saved but then I disabled the block. My code now works :-) The block is appearing correctly. Many thanks. Marking this as the correct answer as it explains some needed theory and the fact that you need to add an instance. Thanks CG Monroe
    – John Cogan
    Dec 21, 2017 at 9:26
2

Rendering custom blocks with Twig tweak module can be done as follows (assuming the ID of your block is 12):

{{ drupal_entity('block_content', 12) }}
0

Try this code by including EntityInterface:

// Block 
use Drupal\Core\Entity\EntityInterface;
// the last part of the parameter is the block id
$block = \Drupal\block\Entity\Block::load('yourblockid');
$block_content = \Drupal::entityManager()
  ->getViewBuilder('block')
  ->view($block);
\Drupal::service('renderer')->renderRoot($block_content);
2
  • Thanks for response. Tried to implement but get and error stating the following: Recoverable fatal error: Argument 1 passed to Drupal\block\BlockViewBuilder::view() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /home/mywebsite/public_html/docroot/themes/custom/mytheme/mytheme.theme on line 1088 and defined in Drupal\block\BlockViewBuilder->view() (line 70 of core/modules/block/src/BlockViewBuilder.php). The use statement is at the top of the theme file and the code block is within the THEME_preprocess_paragraph(&$variables) hook.
    – John Cogan
    Dec 21, 2017 at 9:21
  • Have you included the use Drupal\Core\Entity\EntityInterface; See also: drupal.stackexchange.com/questions/219521/…
    – developer
    Dec 27, 2017 at 4:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.