Swich theme on render entitities

Created on 4 April 2025, 8 days ago

Problem/Motivation

When renders an entity to extract the links it is possible that the default theme is not loaded, instead it loads the theme that is available when is executing the operation of regenerating the links, if it launched using the batch on the backend, it will use the admin theme.

The problem comes from \Drupal\entity_mesh\EntityRender::getFullEntityDom

    $render_output = DeprecationHelper::backwardsCompatibleCall(
      currentVersion: \Drupal::VERSION,
      deprecatedVersion: '10.3',
      currentCallable: fn() => $this->renderer->renderInIsolation($pre_render),
      deprecatedCallable: fn() => $this->renderer->renderPlain($pre_render),
    );

Steps to reproduce

Regenerate the elements using the batch, it will render the elements using the admin theme.

Proposed resolution

We already done a account switcher to render the elements using the anonymous user.

  protected function getFullEntityDom(EntityInterface $entity, string $langcode) {
    $this->accountSwitcher->switchTo(new AnonymousUserSession());

The solution, based on Search API when renders the elements to index them could be do a theme switch before rendering.
\Drupal\search_api\Plugin\search_api\processor\RenderedItem::addFieldValues

    // Switch to the default theme in case the admin theme (or any other theme)
    // is enabled.
    $previous_theme = $this->getThemeSwitcher()->switchToDefault();
🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024