Browse Source

Removed dependency injection.

8.x-1.x
Chi 8 years ago
parent
commit
216bff3f6b
  1. 169
      src/TwigExtension.php
  2. 10
      twig_tweak.services.yml

169
src/TwigExtension.php

@ -3,122 +3,18 @@
namespace Drupal\twig_tweak; namespace Drupal\twig_tweak;
use Drupal\Core\Block\TitleBlockPluginInterface; use Drupal\Core\Block\TitleBlockPluginInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Utility\Token;
use Drupal\image\Entity\ImageStyle; use Drupal\image\Entity\ImageStyle;
use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/** /**
* Twig extension with some useful functions and filters. * Twig extension with some useful functions and filters.
*/
class TwigExtension extends \Twig_Extension {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The menu link tree service.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected $menuTree;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The title resolver.
*
* @var \Drupal\Core\Controller\TitleResolverInterface
*/
protected $titleResolver;
/**
* The form builder.
* *
* @var \Drupal\Core\Form\FormBuilderInterface * As version 1.7 all dependencies are instantiated on demand for performance
* reasons.
*/ */
protected $formBuilder; class TwigExtension extends \Twig_Extension {
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* TwigExtension constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Utility\Token $token
* The token service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
* The menu tree service.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
* The title resolver.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, MenuLinkTreeInterface $menu_tree, RequestStack $request_stack, TitleResolverInterface $title_resolver, FormBuilderInterface $form_builder, RendererInterface $renderer) {
$this->entityTypeManager = $entity_type_manager;
$this->token = $token;
$this->configFactory = $config_factory;
$this->routeMatch = $route_match;
$this->menuTree = $menu_tree;
$this->requestStack = $request_stack;
$this->titleResolver = $title_resolver;
$this->formBuilder = $form_builder;
$this->renderer = $renderer;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -176,9 +72,10 @@ class TwigExtension extends \Twig_Extension {
* A render array for the block or NULL if the block does not exist. * A render array for the block or NULL if the block does not exist.
*/ */
public function drupalBlock($id) { public function drupalBlock($id) {
$block = $this->entityTypeManager->getStorage('block')->load($id); $entity_type_manager = \Drupal::entityTypeManager();
$block = $entity_type_manager->getStorage('block')->load($id);
if ($block && $this->entityAccess($block)) { if ($block && $this->entityAccess($block)) {
return $this->entityTypeManager->getViewBuilder('block')->view($block); return $entity_type_manager->getViewBuilder('block')->view($block);
} }
return NULL; return NULL;
} }
@ -196,12 +93,13 @@ class TwigExtension extends \Twig_Extension {
* A render array to display the region content. * A render array to display the region content.
*/ */
public function drupalRegion($region, $theme = NULL) { public function drupalRegion($region, $theme = NULL) {
$blocks = $this->entityTypeManager->getStorage('block')->loadByProperties([ $entity_type_manager = \Drupal::entityTypeManager();
$blocks = $entity_type_manager->getStorage('block')->loadByProperties([
'region' => $region, 'region' => $region,
'theme' => $theme ?: $this->configFactory->get('system.theme')->get('default'), 'theme' => $theme ?: \Drupal::config('system.theme')->get('default'),
]); ]);
$view_builder = $this->entityTypeManager->getViewBuilder('block'); $view_builder = $entity_type_manager->getViewBuilder('block');
$build = []; $build = [];
@ -210,9 +108,9 @@ class TwigExtension extends \Twig_Extension {
if ($this->entityAccess($block)) { if ($this->entityAccess($block)) {
$block_plugin = $block->getPlugin(); $block_plugin = $block->getPlugin();
if ($block_plugin instanceof TitleBlockPluginInterface) { if ($block_plugin instanceof TitleBlockPluginInterface) {
$request = $this->requestStack->getCurrentRequest(); $request = \Drupal::request();
if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) { if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
$block_plugin->setTitle($this->titleResolver->getTitle($request, $route)); $block_plugin->setTitle(\Drupal::service('title_resolver')->getTitle($request, $route));
} }
} }
$build[$id] = $view_builder->view($block); $build[$id] = $view_builder->view($block);
@ -239,11 +137,12 @@ class TwigExtension extends \Twig_Extension {
* A render array for the entity or NULL if the entity does not exist. * A render array for the entity or NULL if the entity does not exist.
*/ */
public function drupalEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL) { public function drupalEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL) {
$entity = $id ? $entity_type_manager = \Drupal::entityTypeManager();
$this->entityTypeManager->getStorage($entity_type)->load($id) : $entity = $id
$this->routeMatch->getParameter($entity_type); ? $entity_type_manager->getStorage($entity_type)->load($id)
: \Drupal::routeMatch()->getParameter($entity_type);
if ($entity && $this->entityAccess($entity)) { if ($entity && $this->entityAccess($entity)) {
$render_controller = $this->entityTypeManager->getViewBuilder($entity_type); $render_controller = $entity_type_manager->getViewBuilder($entity_type);
return $render_controller->view($entity, $view_mode, $langcode); return $render_controller->view($entity, $view_mode, $langcode);
} }
return NULL; return NULL;
@ -267,9 +166,10 @@ class TwigExtension extends \Twig_Extension {
* A render array for the field or NULL if the value does not exist. * A render array for the field or NULL if the value does not exist.
*/ */
public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) { public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $id $entity = $id
? $this->entityTypeManager->getStorage($entity_type)->load($id) ? \Drupal::entityTypeManager()->getStorage($entity_type)->load($id)
: $this->routeMatch->getParameter($entity_type); : \Drupal::routeMatch()->getParameter($entity_type);
if ($entity && $this->entityAccess($entity)) { if ($entity && $this->entityAccess($entity)) {
if ($langcode && $entity->hasTranslation($langcode)) { if ($langcode && $entity->hasTranslation($langcode)) {
$entity = $entity->getTranslation($langcode); $entity = $entity->getTranslation($langcode);
@ -295,7 +195,9 @@ class TwigExtension extends \Twig_Extension {
* A render array for the menu. * A render array for the menu.
*/ */
public function drupalMenu($menu_name, $level = 1, $depth = 0) { public function drupalMenu($menu_name, $level = 1, $depth = 0) {
$parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name); /** @var \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree */
$menu_tree = \Drupal::service('menu.link_tree');
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Adjust the menu tree parameters based on the block's configuration. // Adjust the menu tree parameters based on the block's configuration.
$parameters->setMinDepth($level); $parameters->setMinDepth($level);
@ -304,16 +206,16 @@ class TwigExtension extends \Twig_Extension {
// Hence this is a relative depth that we must convert to an actual // Hence this is a relative depth that we must convert to an actual
// (absolute) depth, that may never exceed the maximum depth. // (absolute) depth, that may never exceed the maximum depth.
if ($depth > 0) { if ($depth > 0) {
$parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth())); $parameters->setMaxDepth(min($level + $depth - 1, $menu_tree->maxDepth()));
} }
$tree = $this->menuTree->load($menu_name, $parameters); $tree = $menu_tree->load($menu_name, $parameters);
$manipulators = [ $manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'], ['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
]; ];
$tree = $this->menuTree->transform($tree, $manipulators); $tree = $menu_tree->transform($tree, $manipulators);
return $this->menuTree->build($tree); return $menu_tree->build($tree);
} }
/** /**
@ -326,7 +228,7 @@ class TwigExtension extends \Twig_Extension {
* A render array to represent the form. * A render array to represent the form.
*/ */
public function drupalForm($form_id) { public function drupalForm($form_id) {
return $this->formBuilder->getForm($form_id); return \Drupal::formBuilder()->getForm($form_id);
} }
/** /**
@ -350,7 +252,7 @@ class TwigExtension extends \Twig_Extension {
* @see \Drupal\Core\Utility\Token::replace() * @see \Drupal\Core\Utility\Token::replace()
*/ */
public function drupalToken($token, array $data = [], array $options = []) { public function drupalToken($token, array $data = [], array $options = []) {
return $this->token->replace("[$token]", $data, $options); return \Drupal::token()->replace("[$token]", $data, $options);
} }
/** /**
@ -365,7 +267,7 @@ class TwigExtension extends \Twig_Extension {
* The data that was requested. * The data that was requested.
*/ */
public function drupalConfig($name, $key) { public function drupalConfig($name, $key) {
return $this->configFactory->get($name)->get($key); return \Drupal::config($name)->get($key);
} }
/** /**
@ -419,12 +321,11 @@ class TwigExtension extends \Twig_Extension {
* A render array to represent page title. * A render array to represent page title.
*/ */
public function drupalTitle() { public function drupalTitle() {
$title = $this->titleResolver->getTitle( $title = \Drupal::service('title_resolver')->getTitle(
$this->requestStack->getCurrentRequest(), \Drupal::request(),
$this->routeMatch->getRouteObject() \Drupal::routeMatch()->getRouteObject()
); );
$build['#markup'] = is_string($title) || $title instanceof TranslatableMarkup ? $build['#markup'] = render($title);
$title : $this->renderer->render($title);
$build['#cache']['contexts'] = ['url']; $build['#cache']['contexts'] = ['url'];
return $build; return $build;
} }
@ -439,7 +340,7 @@ class TwigExtension extends \Twig_Extension {
* The entered HTML text with tokens replaced. * The entered HTML text with tokens replaced.
*/ */
public function tokenReplaceFilter($text) { public function tokenReplaceFilter($text) {
return $this->token->replace($text); return \Drupal::token()->replace($text);
} }
/** /**

10
twig_tweak.services.yml

@ -1,15 +1,5 @@
services: services:
twig_tweak.twig_extension: twig_tweak.twig_extension:
class: Drupal\twig_tweak\TwigExtension class: Drupal\twig_tweak\TwigExtension
arguments:
- '@entity_type.manager'
- '@token'
- '@config.factory'
- '@current_route_match'
- '@menu.link_tree'
- '@request_stack'
- '@title_resolver'
- '@form_builder'
- '@renderer'
tags: tags:
- { name: twig.extension } - { name: twig.extension }

Loading…
Cancel
Save