Load blocks from the secondary menu in the node preprocess hook
Fix missing secondary menu
This commit is contained in:
parent
e3b84fb85c
commit
6648fe29ed
1 changed files with 58 additions and 0 deletions
|
@ -4,6 +4,64 @@ use Drupal\block\Entity\Block;
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
use Drupal\Core\Entity\EntityInterface;
|
||||||
use Drupal\Core\Url;
|
use Drupal\Core\Url;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements hook_preprocess_node() for NODE document templates.
|
||||||
|
*/
|
||||||
|
function agarica_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 agarica_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 = ['secondary_menu'];
|
||||||
|
agarica_add_regions_to_node($allowed_regions, $variables);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* THEME_add_regions_to_node
|
||||||
|
*/
|
||||||
|
|
||||||
|
function agarica_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(['theme' => $theme, 'region' => $region]);
|
||||||
|
|
||||||
|
// Sort ‘em
|
||||||
|
uasort($blocks, 'Drupal\block\Entity\Block::sort');
|
||||||
|
|
||||||
|
// Capture viewable blocks and their settings to $build
|
||||||
|
$build = [];
|
||||||
|
foreach ($blocks as $key => $block) {
|
||||||
|
if ($block->access('view')) {
|
||||||
|
$view_builder = \Drupal::entityTypeManager()
|
||||||
|
->getViewBuilder($block
|
||||||
|
->getEntityTypeId());
|
||||||
|
$build[$key] = $view_builder
|
||||||
|
->view($block, 'block');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add build to region
|
||||||
|
$variables[$region] = $build;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_preprocess_region__footer().
|
* Implements hook_preprocess_region__footer().
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue