Skip to content

Instantly share code, notes, and snippets.

@megclaypool
Last active August 23, 2023 01:16
Show Gist options
  • Save megclaypool/15be9919eee302873de269a91e137a13 to your computer and use it in GitHub Desktop.
Save megclaypool/15be9919eee302873de269a91e137a13 to your computer and use it in GitHub Desktop.
These are set up as Twig functions in a module in the NIJC site. This might be pretty sweet to import as a Radicati Drupal module?
<?php
/**
* @file
* Contains \Drupal\custom_blocks_forms\Twig\KintExtension.
*/
namespace Drupal\custom_blocks_forms\Twig;
use Drupal;
/**
* Provides the Kint debugging function within Twig templates.
*/
class RenderMenu extends \Twig\Extension\AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'custom_render_menu';
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('custom_render_block', array($this, 'custom_render_block'), array(
'is_safe' => array('html'),
'needs_environment' => TRUE,
'needs_context' => TRUE,
)),
new \Twig_SimpleFunction('custom_render_menu', array($this, 'custom_render_menu'), array(
'is_safe' => array('html'),
'needs_environment' => TRUE,
'needs_context' => TRUE,
)),
new \Twig\TwigFunction('custom_render_region', array($this, 'custom_render_region'), array(
'is_safe' => array('html'),
'needs_environment' => TRUE,
'needs_context' => TRUE,
)),
);
}
public function custom_render_block(\Twig\Environment $env, array $context, $bid)
{
$block = \Drupal\block\Entity\Block::load($bid);
if (!empty($block)) {
$render = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($block);
return $render;
}
}
/**
* Provides function to programmatically render a menu
*
* @param String $menu_name
* The machine configuration id of the menu to render
* @thanks: https://www.drupal.org/node/2416715 , http://drupal.stackexchange.com/questions/126477/drupal-8-implement-menu-links-as-plugins-trying-to-build-a-menu-tree, https://www.drupal.org/node/2226481
*/
public function custom_render_menu(\Twig\Environment $env, array $context, $menu_name)
{
$menu_tree = \Drupal::menuTree();
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// kint($parameters);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = array(
// Only show links that are accessible for the current user.
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
// Use the default sorting of menu links.
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($tree, $manipulators);
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
$menu['#attributes']['class'] = array('inline');
return render($menu);
}
/**
* Provides functionality to programmatically render regions in Twig templates.
*/
public function custom_render_region(\Twig\Environment $env, array $context, $region)
{
// Load region blocks
$blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(array('theme' => \Drupal::config('system.theme')->get('default'), 'region' => $region));
$build = array();
if (!empty($blocks)) {
// Sort ‘em
uasort($blocks, 'Drupal\block\Entity\Block::sort');
// Capture viewable blocks and their settings to $build
foreach ($blocks as $key => $block) {
if ($block->access('view')) {
$build[$key] = \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);
}
}
}
return $build;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment