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 ?
<!-- 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 havetemplates/regions
folder whithout aregion.html.twig
like in the case of node or field...