View source
<?php
namespace Drupal\bamboo_twig_loaders\TwigExtension;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class Loader extends \Twig_Extension {
protected $entityTypeManager;
protected $routeMatch;
protected $blockManager;
protected $formBuilder;
protected $menuTree;
protected $configFactory;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match, BlockManagerInterface $blockManager, FormBuilderInterface $formBuilder, MenuLinkTreeInterface $menu_tree, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->routeMatch = $route_match;
$this->blockManager = $blockManager;
$this->formBuilder = $formBuilder;
$this->menuTree = $menu_tree;
$this->configFactory = $config_factory;
}
public function getFunctions() {
return [
new \Twig_SimpleFunction('load_block', [
$this,
'loadBlock',
], [
'is_safe' => [
'html',
],
]),
new \Twig_SimpleFunction('load_form', [
$this,
'loadForm',
], [
'is_safe' => [
'html',
],
]),
new \Twig_SimpleFunction('load_entity', [
$this,
'loadEntity',
]),
new \Twig_SimpleFunction('load_region', [
$this,
'loadRegion',
], [
'is_safe' => [
'html',
],
]),
new \Twig_SimpleFunction('load_field', [
$this,
'loadField',
]),
new \Twig_SimpleFunction('load_menu', [
$this,
'loadMenu',
], [
'is_safe' => [
'html',
],
]),
];
}
public function getName() {
return 'bamboo_twig.twig.loader';
}
public function loadBlock($block_id, $params = []) {
$instance = $this->blockManager
->createInstance($block_id, $params);
return $instance
->build($params);
}
public function loadForm($module, $form, $params = []) {
return $this->formBuilder
->getForm('Drupal\\' . $module . '\\Form\\' . $form, $params);
}
public function loadEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL) {
$entity = $id ? $this->entityTypeManager
->getStorage($entity_type)
->load($id) : $this->routeMatch
->getParameter($entity_type);
if ($entity) {
$render_controller = $this->entityTypeManager
->getViewBuilder($entity_type);
return $render_controller
->view($entity, $view_mode, $langcode);
}
return NULL;
}
public function loadRegion($region, $theme = NULL) {
$blocks = $this->entityTypeManager
->getStorage('block')
->loadByProperties([
'region' => $region,
'theme' => $theme ?: $this->configFactory
->get('system.theme')
->get('default'),
]);
$view_builder = $this->entityTypeManager
->getViewBuilder('block');
$build = [];
foreach ($blocks as $id => $block) {
$block_plugin = $block
->getPlugin();
if ($block_plugin instanceof TitleBlockPluginInterface) {
$request = $this->requestStack
->getCurrentRequest();
if ($route = $request->attributes
->get(RouteObjectInterface::ROUTE_OBJECT)) {
$block_plugin
->setTitle($this->titleResolver
->getTitle($request, $route));
}
}
$build[$id] = $view_builder
->view($block);
}
return $build;
}
public function loadField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) {
$entity = $id ? $this->entityTypeManager
->getStorage($entity_type)
->load($id) : $this->routeMatch
->getParameter($entity_type);
if ($langcode && $entity
->hasTranslation($langcode)) {
$entity = $entity
->getTranslation($langcode);
}
if (isset($entity->{$field_name})) {
return $entity->{$field_name}
->view($view_mode);
}
return NULL;
}
public function loadMenu($menu_name, $level = 1, $depth = 0) {
$parameters = $this->menuTree
->getCurrentRouteMenuTreeParameters($menu_name);
$parameters
->setMinDepth($level);
if ($depth > 0) {
$parameters
->setMaxDepth(min($level + $depth - 1, $this->menuTree
->maxDepth()));
}
$parameters
->onlyEnabledLinks();
$parameters->expandedParents = [];
$tree = $this->menuTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuTree
->transform($tree, $manipulators);
return $this->menuTree
->build($tree);
}
}