Problem/Motivation

We are recibing multiples errors related to theme hooks, like "Theme hook responsive_image_formatter not found." The problem occurs when an editor saves a node and the synchronous option is enabled, the module performs a theme switch but does not restart the registry, so the old admin theme registry is kept, and the errors are registered.

EntityRenderContext::renderEntity()

  $previousTheme = $theme !== NULL
    ? $this->themeSwitcher->switchTo($theme)
    : $this->themeSwitcher->switchToDefault();

ThemeSwitcher.php

  public function switchToDefault(): ?ActiveTheme {
    $defaultTheme = $this->configFactory
      ->get('system.theme')
      ->get('default');        // = Main site theme.
    return $this->switchTo($defaultTheme);
  }
  public function switchTo(string $themeName): ?ActiveTheme {
    $activeTheme = $this->themeManager->getActiveTheme();

    // If the default theme is the same as the switched theme, it returns NULL, but the active theme is the admin one.
    if ($themeName === $activeTheme->getName()) {
      return NULL;             // <-- CLAVE
    }

    // Here changes the theme.
    $newTheme = $this->themeInitializer->getActiveThemeByName($themeName);
    $this->themeManager->setActiveTheme($newTheme); 
    drupal_static_reset('template_preprocess');
    return $activeTheme;
  }

The error comes from ThemeManager::render()

  $active_theme = $this->getActiveTheme();          // New active theme.
  $theme_registry = $this->themeRegistry->getRuntime();  // Old registry theme (admin one).

  if (!$theme_registry->has($hook)) {
    while ($pos = strrpos($hook, '__')) { ... }     // try to get theme hooks that are registered by admin theme, but no available on main theme.
    if (!$theme_registry->has($hook)) {
      if (!isset($candidate)) {
        \Drupal::logger('theme')->warning('Theme hook %hook not found.', ['%hook' => $hook]);
      }
      return FALSE;
    }
  }

Proposed resolution

Process the item on the destruct, same strategy as search_api

  search_api.post_request_indexing:
    class: Drupal\search_api\Utility\PostRequestIndexing
    tags:
      - { name: needs_destruction }
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

eduardo morales alberti’s picture

Status: Active » Needs review
eduardo morales alberti’s picture

Ready to review

eduardo morales alberti’s picture

Status: Needs review » Fixed

Merged

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.