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

2
twig_tweak.services.yml

@ -1,6 +1,6 @@
services:
twig_tweak.twig_extension:
class: Drupal\twig_tweak\TwigExtension
arguments: ['@entity_type.manager', '@token', '@config.factory']
arguments: ['@entity_type.manager', '@token', '@config.factory', '@current_route_match']
tags:
- { name: twig.extension }

Loading…
Cancel
Save