From 689d517ebf960230e5415cd3a48bfa1aa1ddd501 Mon Sep 17 00:00:00 2001 From: prashantdsala Date: Wed, 20 Dec 2023 17:28:40 +0530 Subject: [PATCH] Adding to condition --- src/TwigTweakExtension.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index d007798..d709ff8 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -198,6 +198,10 @@ class TwigTweakExtension extends AbstractExtension { public static function drupalField(string $field_name, string $entity_type, string $id, $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE): array { $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($id); if ($entity) { + // Restrict anonymous user to view unpublished entities. + if ($check_access && \Drupal::currentUser()->isAnonymous() && !$entity->isPublished()) { + return []; + } return \Drupal::service('twig_tweak.field_view_builder') ->build($entity, $field_name, $view_mode, $langcode, $check_access); }