-1

I have region--footer-sustain.html.twig which is expressed in templates via {{page['footer_sustain']}}.

Client wants some content moved into the CMS so I added to theme_info.yml these regions:

sustain_footer_left : 'Sustain Footer Left Col' sustain_footer_middle : 'Sustain Footer Middle Col' sustain_footer_right : 'Sustain Footer Right Col'

I added the HTML to new Custom Blocks and assigned the blocks to the regions in the same way I have all over the site but no output was expressed.

I tried {{page['sustain_footer_left']}} and spitballing tried {{region['sustain_footer_middle']}} and later {{sustain_footer_right}}

After reading Place block region in node template I tried editing theme.theme with the following:

function theme_add_regions_to_node($allowed_regions, &$variables) {
  // Retrieve active theme
  $theme = \Drupal::theme()->getActiveTheme()->getName();

  // Retrieve theme regions
  $available_regions = system_region_list($theme, 'REGIONS_ALL');

  // Validate allowed regions with available regions
  $regions = array_intersect(array_keys($available_regions), $allowed_regions);

  // For each region
  foreach ($regions as $key => $region) {

    // Load region blocks
    $blocks = entity_load_multiple_by_properties('block', array('theme' => $theme, 'region' => $region));

    // Sort ‘em
    uasort($blocks, 'Drupal\block\Entity\Block::sort');

    // Capture viewable blocks and their settings to $build
    $build = array();
    foreach ($blocks as $key => $block) {
      if ($block->access('view')) {
        $build[$key] = entity_view($block, 'block');
      }
    }

    // Add build to region
    $variables[$region] = $build;
  }
}

And then called it like so:

function theme_preprocess_node(&$variables) {  
  $view_mode = $variables['view_mode']; // Retrieve view mode
  $allowed_view_modes = array('full'); // Array of allowed view modes (for performance so as to not execute on unneeded nodes)

  // If view mode is in allowed view modes list, pass to THEME_add_regions_to_node()
  if(in_array($view_mode, $allowed_view_modes)) {
    // Allowed regions (for performance so as to not execute for unneeded region)
    $allowed_regions = array('sustain_footer_left','sustain_footer_middle','sustain_footer_right');
    theme_add_regions_to_node($allowed_regions, $variables);
  }
}

Also tried commenting out the if(in_array()) as a test. Also tried doing same in theme_preprocess_page().

UPDATE

Also tried

  $variables['sustain_footer_right'] = entity_load('block_content', 'sustainfooterrightcolumn')->getPlugin()->build(); 
 $block = \Drupal\block_content\Entity\BlockContent::load('sustainfooterrightcolumn');
 // $variables['sustain_footer_right'] = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);

And also tried

 $custom_block = \Drupal::entityTypeManager()->getStorage('block_content')->load('96');
 $variables['sustain_footer_right'] = $custom_block->field_myfield->value;

AND ALSO TRIED

  $block = \Drupal\block\Entity\Block::load('sustainfooterrightcolumn');
  if ($block) {
  $variables['sustain_footer_right'] = \Drupal::entityManager()
          ->getViewBuilder('block')
          ->view($block);
  } else {
    $variables['sustain_footer_right'] = 'x';
  }

Also in twig tried all of these, and yes, cleared cache every time.

{{sustain_footer_right}} {{page.sustain_footer_right}} {{page['sustain_footer_right']}}

I get no errors but also no output. How do I get the content of these blocks to be expressed in region--footer-sustain.html.twig ?

1
1

Adding a new region to output blocks in does not require a whole lot of code other than the yaml definition you already have. From there, you just need to render the region in the page template:

{% if page.sustain_footer_right %}
  {{ page.sustain_footer_right }}
{% endif %}

If you make a custom region twig file, it needs to render also:

<div class="container">
  {{ content }}
</div>

Based on your region name, the twig template should be region--sustain-footer-right.html.twig.

This is all that is needed to render blocks in new regions. I would say eliminate all the custom code and starting from there.

2
  • Thank you- So originally I had functional region--footer-sustain.html.twig which is expressed in templates via {{page['footer_sustain']}}. I need to make region--sustain-footer-right.html.twig and render in region--footer-sustain.html.twig via {{ page.sustain_footer_right }} ? Sep 28 '17 at 15:59
  • 1
    Yes it should be dot notation. But, you don't even need the custom region template - the system will use region.html.twig provided by core base theme.
    – Kevin
    Sep 28 '17 at 16:00

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.