Override the layout in the default view mode only

Created on 9 April 2024, 8 months ago
Updated 10 May 2024, 7 months ago

Problem/Motivation

We're experiencing that when rendering an entity via code and passing it to twig, it renders in an altered layout defined in Layout Library.

    if (!empty($productId)) {
      $entityType = 'commerce_product';
      $viewMode = 'modal';

      $storage = \Drupal::entityTypeManager()->getStorage($entityType);
      $productEntity = $storage->load($productId);

      if ($productEntity->access('view', \Drupal::currentUser())) {
        $viewBuilder = \Drupal::entityTypeManager()->getViewBuilder($entityType);
        $build = $viewBuilder->view($productEntity, $viewMode);
        return [
          'id' => $productVariationId,
          'rendered' => \Drupal::service('renderer')->renderRoot($build),
        ];
      }
    }

In our use case we need the altered only for the Default view mode.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

RTBC

Version

1.0

Component

Code

Created by

πŸ‡ΈπŸ‡°Slovakia kaszarobert

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024