From f00f28dd053c6f095854976c48b3ce60544ff234 Mon Sep 17 00:00:00 2001 From: git Date: Sat, 24 Apr 2021 16:58:45 +0500 Subject: [PATCH] Issue #3207943 by nimoatwoodway: Add UUID support to drupal_entity --- src/TwigTweakExtension.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index e6516b2..73602ac 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -155,12 +155,21 @@ class TwigTweakExtension extends AbstractExtension { /** * Returns the render array to represent an entity. */ - public static function drupalEntity(string $entity_type, string $id, string $view_mode = 'full', ?string $langcode = NULL, bool $check_access = TRUE): array { - $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($id); + public static function drupalEntity(string $entity_type, string $selector, string $view_mode = 'full', ?string $langcode = NULL, bool $check_access = TRUE): array { + + // Load entity by id or uuid. + if (Uuid::isValid($selector)) { + $entities = \Drupal::entityTypeManager()->getStorage($entity_type)->loadByProperties(['uuid' => $selector]); + $entity = reset($entities); + } else { + $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($selector); + } + if ($entity) { return \Drupal::service('twig_tweak.entity_view_builder') ->build($entity, $view_mode, $langcode, $check_access); } + return []; }