From 4111163f518266259a533bf4d3d6f24c715e9c32 Mon Sep 17 00:00:00 2001 From: Kyrylo Loboda <25081-lobodacyril@users.noreply.drupalcode.org> Date: Sat, 6 Jan 2024 05:59:13 +0000 Subject: [PATCH] Issue #3405514 by lobodacyril, kevinquillen: drupal_entity works incorrect when the source content is unpublished --- src/View/EntityFormViewBuilder.php | 3 ++- src/View/EntityViewBuilder.php | 12 +++++++++++- twig_tweak.services.yml | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/View/EntityFormViewBuilder.php b/src/View/EntityFormViewBuilder.php index a8f485e..64095cc 100644 --- a/src/View/EntityFormViewBuilder.php +++ b/src/View/EntityFormViewBuilder.php @@ -29,6 +29,8 @@ class EntityFormViewBuilder { /** * Gets the built and processed entity form for the given entity type. * + * @todo Add langcode parameter. + * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * @param string $form_mode @@ -40,7 +42,6 @@ class EntityFormViewBuilder { * The processed form for the given entity type and form mode. */ public function build(EntityInterface $entity, string $form_mode = 'default', bool $check_access = TRUE): array { - $build = []; $operation = $entity->isNew() ? 'create' : 'update'; diff --git a/src/View/EntityViewBuilder.php b/src/View/EntityViewBuilder.php index ba447ba..1fe4eb7 100644 --- a/src/View/EntityViewBuilder.php +++ b/src/View/EntityViewBuilder.php @@ -5,6 +5,7 @@ namespace Drupal\twig_tweak\View; use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; /** @@ -19,11 +20,19 @@ class EntityViewBuilder { */ protected $entityTypeManager; + /** + * The entity repository service. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + /** * Constructs an EntityViewBuilder object. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) { $this->entityTypeManager = $entity_type_manager; + $this->entityRepository = $entity_repository; } /** @@ -31,6 +40,7 @@ class EntityViewBuilder { */ public function build(EntityInterface $entity, string $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE): array { $build = []; + $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); $access = $check_access ? $entity->access('view', NULL, TRUE) : AccessResult::allowed(); if ($access->isAllowed()) { $build = $this->entityTypeManager diff --git a/twig_tweak.services.yml b/twig_tweak.services.yml index f5c6ef3..6e33f41 100644 --- a/twig_tweak.services.yml +++ b/twig_tweak.services.yml @@ -15,7 +15,7 @@ services: twig_tweak.entity_view_builder: class: Drupal\twig_tweak\View\EntityViewBuilder - arguments: ['@entity_type.manager'] + arguments: ['@entity_type.manager', '@entity.repository'] twig_tweak.entity_form_view_builder: class: Drupal\twig_tweak\View\EntityFormViewBuilder