Browse Source

Issue #3405514 by lobodacyril, kevinquillen: drupal_entity works incorrect when the source content is unpublished

merge-requests/20/merge
Kyrylo Loboda 12 months ago committed by Chi
parent
commit
4111163f51
  1. 3
      src/View/EntityFormViewBuilder.php
  2. 12
      src/View/EntityViewBuilder.php
  3. 2
      twig_tweak.services.yml

3
src/View/EntityFormViewBuilder.php

@ -29,6 +29,8 @@ class EntityFormViewBuilder {
/** /**
* Gets the built and processed entity form for the given entity type. * Gets the built and processed entity form for the given entity type.
* *
* @todo Add langcode parameter.
*
* @param \Drupal\Core\Entity\EntityInterface $entity * @param \Drupal\Core\Entity\EntityInterface $entity
* The entity. * The entity.
* @param string $form_mode * @param string $form_mode
@ -40,7 +42,6 @@ class EntityFormViewBuilder {
* The processed form for the given entity type and form mode. * 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 { public function build(EntityInterface $entity, string $form_mode = 'default', bool $check_access = TRUE): array {
$build = []; $build = [];
$operation = $entity->isNew() ? 'create' : 'update'; $operation = $entity->isNew() ? 'create' : 'update';

12
src/View/EntityViewBuilder.php

@ -5,6 +5,7 @@ namespace Drupal\twig_tweak\View;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
/** /**
@ -19,11 +20,19 @@ class EntityViewBuilder {
*/ */
protected $entityTypeManager; protected $entityTypeManager;
/**
* The entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/** /**
* Constructs an EntityViewBuilder object. * 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->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 { public function build(EntityInterface $entity, string $view_mode = 'full', string $langcode = NULL, bool $check_access = TRUE): array {
$build = []; $build = [];
$entity = $this->entityRepository->getTranslationFromContext($entity, $langcode);
$access = $check_access ? $entity->access('view', NULL, TRUE) : AccessResult::allowed(); $access = $check_access ? $entity->access('view', NULL, TRUE) : AccessResult::allowed();
if ($access->isAllowed()) { if ($access->isAllowed()) {
$build = $this->entityTypeManager $build = $this->entityTypeManager

2
twig_tweak.services.yml

@ -15,7 +15,7 @@ services:
twig_tweak.entity_view_builder: twig_tweak.entity_view_builder:
class: Drupal\twig_tweak\View\EntityViewBuilder class: Drupal\twig_tweak\View\EntityViewBuilder
arguments: ['@entity_type.manager'] arguments: ['@entity_type.manager', '@entity.repository']
twig_tweak.entity_form_view_builder: twig_tweak.entity_form_view_builder:
class: Drupal\twig_tweak\View\EntityFormViewBuilder class: Drupal\twig_tweak\View\EntityFormViewBuilder

Loading…
Cancel
Save