Browse Source

Made entity ID parameter optional.

8.x-1.x 8.x-1.1
Chi 9 years ago
parent
commit
f749c00346
  1. 19
      src/TwigExtension.php
  2. 2
      twig_tweak.services.yml

19
src/TwigExtension.php

@ -9,6 +9,7 @@ namespace Drupal\twig_tweak;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Utility\Token; use Drupal\Core\Utility\Token;
/** /**
@ -37,6 +38,13 @@ class TwigExtension extends \Twig_Extension {
*/ */
protected $configFactory; protected $configFactory;
/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/** /**
* TwigExtension constructor. * TwigExtension constructor.
* *
@ -46,11 +54,14 @@ class TwigExtension extends \Twig_Extension {
* The token service. * The token service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory. * The configuration factory.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/ */
public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory) { public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->token = $token; $this->token = $token;
$this->configFactory = $config_factory; $this->configFactory = $config_factory;
$this->routeMatch = $route_match;
} }
/** /**
@ -151,8 +162,10 @@ class TwigExtension extends \Twig_Extension {
* @return NULL|array * @return NULL|array
* 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, $view_mode = NULL, $langcode = NULL) { public function drupalEntity($entity_type, $id = NULL, $view_mode = NULL, $langcode = NULL) {
$entity = $this->entityTypeManager->getStorage($entity_type)->load($id); $entity = $id ?
$this->entityTypeManager->getStorage($entity_type)->load($id) :
$this->routeMatch->getParameter($entity_type);
if ($entity) { if ($entity) {
$render_controller = $this->entityTypeManager->getViewBuilder($entity_type); $render_controller = $this->entityTypeManager->getViewBuilder($entity_type);
return $render_controller->view($entity, $view_mode, $langcode); return $render_controller->view($entity, $view_mode, $langcode);

2
twig_tweak.services.yml

@ -1,6 +1,6 @@
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'] arguments: ['@entity_type.manager', '@token', '@config.factory', '@current_route_match']
tags: tags:
- { name: twig.extension } - { name: twig.extension }

Loading…
Cancel
Save