0

I am looking at building a Drupal 8 Commerce 2 product display through twig but I am having issues with how to print custom blocks in this template file. (And also testing these in normal page.html.twig as well)

Looking at the Custom Block Library (/admin/structure/block/block-content) I can see that the ID is '4', as the ID from the Edit URL is 4. (http://www.example.com/block/4)

I have installed the module Twig Tweak and tried all the combinations below in the commerce-product.html.twig, and each time I get a WSOD

The website encountered an unexpected error. Please try again later.

{{ drupal_entity('block_content', 4) }}

{{ drupal_entity('block_content', '4') }}

{{ drupal_block('block_content:4') }}

{{ drupal_entity('block', 4) }}

{{ drupal_entity('block', '4') }}

I have also tried in my example.theme file:

function theme_preprocess_page(&$variables) {
  $block = \Drupal\block\Entity\Block::load('4');
  $block_content = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($block);
 $variables['productinfo'] = $block_content;
}

Then in *.html.twig with lots of cache flushing before and after:

{{ productinfo }}

I also seam to be not even able to use any of the examples from the Twig Tweak Cheat Sheet, as each one of these I add to my any *.html.twig I get the WSOD.

{{ drupal_view('who_s_new', 'block_1') }}

or

{{ drupal_region('sidebar_first') }}

Question: How do I print custom blocks into a commerce-product.html.twig template?

  • 1
    What's the error message? It'll be in your server logs and/or the watchdog – Clive Feb 27 '19 at 11:17
0

You can use hook_preprocess_commerce_product to load the block and pass the variable to your twig file.

function YOURTHEME_preprocess_commerce_product(array &$variables) {
  $block = \Drupal\block_content\Entity\BlockContent::load(4);
  $variables['custom_block'] = \Drupal::entityTypeManager()->
    getViewBuilder('block_content')->view($block);
}

and then in your twig file you will have the {{ custom_block }} variable set.

On a sidenote, this answer helps a lot when trying to programatically load blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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