0

I am using the following code in my theme to fetch 3 blocks (each containing 4 fields) generated via the custom block library interface i:e all 3 block are of the HomePage-Block type:

function legislation_research_theme_preprocess_page(&$variables, $hook) {
    for($i = 1; $i <= 3; $i++) {
        $block_entity = Drupal\block_content\Entity\BlockContent::load($i);// BLOCK_ID
        $block_view[$i] = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block_entity);
    }

    if ($block_view) {
        $variables['page']['my_block'] = $block_view;
    }
}

In my page--front.html.twig, my blocks output via:

    {{ page.my_block }}

However, the output is of the individual fields from each of the blocks rather than 3 divs containing 4 fields of content from each block. That's making it impossible to to style:

enter image description here

Can anyone advice how I might be able to output each block but contained in individual parent divs?

0

I render my custom library blocks like this:

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

So you have to search your block at your library and use this block's machine name where i say block_id. Hope it helps.

| improve this answer | |
  • Thank you for you response. I just tried it and Twig does not recognise the drupal_entity function and so it fails with an Unknown "drupal_entity" function error message. Is there something extra you are doing to make that function active in twig? – sisko Jun 4 at 10:13
  • I'm sorry, but i also found that in the documentation. drupal.org/docs/8/modules/twig-tweak/… – DigitalSM Jun 4 at 10:32
  • I see! But that would work only if the Twig Tweak module is installed. I'm assuming you do? – sisko Jun 4 at 14:11
  • Yes, i always install this module. I am not so advanced so i thought that was part of the best practice to follow when you install drupal. Something like a starter pack. So my bad. – DigitalSM Jun 4 at 14:21

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.