Dynamic theme registry alteration

Created on 6 August 2025, 7 days ago

Problem/Motivation

For Views integration, we are doing a 3 steps process.

1. altering the registry from a hook:

  #[Hook('theme_registry_alter')]
  public function themeRegistryAlter(array &$theme_registry): void {
    if ($this->adminContext->isAdminRoute()) {
      return;
    }
    $template_uri = $this->moduleExtensionList->getPath('display_builder_views') . '/templates';
    $theme_registry['views_view']['path'] = $template_uri;
  }

2. adding a display_builder variable from a preprocess_views_view hook.

3. Adding a condition to the related template

<div{{ attributes.addClass(classes) }}>
  {% if display_builder.0 %}
    {{ display_builder }}
  {% else %}
    {# Original view rendered as web/core/modules/views/templates/views-view.html.twig #}
   ...

In 📌 ThemeRegistryAlter must fallback on default templates Active , a similar mechanism with hook_theme_registry_alter was replaced by a dynamic alteration of the registry and the direct load of the expected template:

    // We alter the registry here instead of implementing
    // hook_theme_registry_alter in order keep the alteration specific to each
    // page.
    $theme_registry = $this->themeRegistry->get();
    $template_uri = $this->modules->getPath('display_builder_page_layout') . '/templates';
    $runtime = $this->themeRegistry->getRuntime();
    $theme_registry['page']['path'] = $template_uri;
    $runtime->set('page', $theme_registry['page']);
    $theme_registry['region']['path'] = $template_uri;
    $runtime->set('region', $theme_registry['region']);

https://git.drupalcode.org/project/display_builder/-/blob/1.0.x/modules/...

Proposed resolution

Do we want to do the same for Views integration? Is it possible ? It will simplify the implementation, that's good, but is it at the price of performance?

Remaining tasks

Don't forget to update the schema in the documentation :)

📌 Task
Status

Active

Version

1.0

Component

display_builder_views

Created by

🇫🇷France pdureau Paris

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

Comments & Activities

Production build 0.71.5 2024