0

I did add a custom region to my theme via my .info.yml file :

regions:
  node_col_1: 'Bloc pour colonne 1'

and in my .theme file to make it available for node template:

function starterpf_d8_preprocess_node(&$variables) {
  // Allowed view modes
  $view_mode = $variables['view_mode']; // Retrieve view mode
  $allowed_view_modes = ['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 starterpf_d8_add_regions_to_node()
  if(in_array($view_mode, $allowed_view_modes)) {
    // Allowed regions (for performance so as to not execute for unneeded region)
    // Liste des régions utilisées directement dans les node.html.twig
    $allowed_regions = [
      'pole_col1','pole_col2','pole_col3','pole_col4',
      'node_col_1','node_col_2','node_col_3','node_col_4','node_col_5',
      'revue_presse','sortie_action','actuAssociation','projets','intervenants'
      ];
    starterpf_d8_add_regions_to_node($allowed_regions, $variables);
  }
}

/**
* THEME_add_regions_to_node
*/

function starterpf_d8_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;
  }
}

Then I try to apply a custom template for this region but it seem not to work,and twig doesn't provide suggestions like for block,field,etc.. My file is called : region--node_col_1.html.twig His content:

{%
  set classes = [
    'region',
    'region-' ~ region|clean_class,
  ]
%}
{% if content %}
  <div{{ attributes.addClass(classes) }}>
    {{ content }}
  </div>
{% endif %}

What do I miss ? Thanks for help

EDIT: After test, template suggestion work for custom region added to page.html.twig but not for region added to a node.html.twig...folder structure seem not to be the way,most probably a hook in .theme file.

EDIT : someone could help me for the theme_suggestions_block_alter ?

6
  • Are you doing a theme hook suggestion for this region in order to provide custom template names?
    – Kevin
    Apr 10, 2017 at 14:01
  • no.I use the native template suggestion of twig
    – user8753
    Apr 10, 2017 at 15:10
  • What does that mean?
    – Kevin
    Apr 10, 2017 at 15:10
  • I didn't added template suggestion via a hook theme so I get drupal/Twig suggestion like in my case : <!-- Region Custom ajoutée via le fichier .theme --> <!-- FILE NAME SUGGESTIONS: * block--poweredbydrupal.html.twig * block--system-powered-by-block.html.twig * block--system.html.twig x block.html.twig --> But perhaps it's a mistake on folder structure : because I have templates/regions folder whithout a region.html.twig like in the case of node or field...
    – user8753
    Apr 11, 2017 at 7:31
  • Those aren't region suggestions. You're inheriting region.html.twig from classy or stable.
    – Kevin
    Apr 11, 2017 at 10:53

2 Answers 2

2

I think your filename is incorrect. It should be:

region--node-col-1.html.twig.

Enable twig debugging to see the suggestions output in the HTML source, which will paint a better picture of what Drupal is expecting to see as template overrides.

3
  • I changed my file name to region--node_col_1.html.twig from region--node-col-1.html.twig thinking it wasn't the right way.Twig doesn't provide suggestion for my case but print drupal blocks within the region.
    – user8753
    Apr 10, 2017 at 15:13
  • No suggestions at all?
    – Kevin
    Apr 10, 2017 at 15:18
  • for region, no, for others parts like node, field or block, yes
    – user8753
    Apr 11, 2017 at 7:28
0

According to your previous example, you are making the region available in the node templates as in this guide: https://atendesigngroup.com/blog/making-region-content-available-node-templates-drupal-8

You can find a guide for the templates of the node here:

node--nodeid--viewmode.html.twig
node--nodeid.html.twig
node--type--viewmode.html.twig
node--type.html.twig
node--viewmode.html.twig node.html.twig

1
  • I already have a node template according to the content type,it's work and I can print my content region in it.
    – user8753
    Apr 10, 2017 at 15:11

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.