Versione Drupal

Considerando che di default le regioni vengono gestite nei template delle pagine (quindi in page.html.twig e suoi sourrogati) in qualche caso si rende necessario poter accedere alle regioni in un template node.html.twig. Ad esempio se volessi stampare la sidebar nel nodo, non potrei farlo se non usiamo questo codice qui sotto:

PS: Su drupal 9 a causa di alcune funzioni deprecate, il codice è leggermente diverso quindi riporto sia per drupal 8 che per drupal 9.

Supponiamo di voler inserire la regione sidebar_right in un template di node quindi

{{ sidebar_right }}

CODICE PER DRUPAL 8 e 9

questo codice è comune per entrambi le versioni ma la funzione successiva ha delle differenze tra l'8 e il 9 dovuto a funzioni deprecate come "entity_load_multiple_by_properties" e "entity_view".

 

/**

* Implements hook_preprocess_node() for NODE document templates.
*/
function THEME_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 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 = ['sidebar_right'];
    THEME_add_regions_to_node($allowed_regions, $variables);
  }
}

CODICE PER DRUPAL 8

/**
* THEME_add_regions_to_node
*/

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

CODICE PER DRUPAL 9

/**
* THEME_add_regions_to_node
*/
 
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 = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(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')) {
        $block = Block::load($key);
        $block_content = \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);
        $build[$key] = $block_content;
      }
    }
    // Add build to region
    $variables[$region] = $build;
  }
}

Dopo aver svuotato la cache,  possiamo stampare il contenuto della regione sidebar_right nel template node aggiungendo quindi a node.html.twig il seguente codice:

{{ sidebar_right }}

Articolo preso da: https://atendesigngroup.com/articles/making-region-content-available-no…