The ticket https://www.drupal.org/node/2732881 handles the issue where the wrong theme is used for the indexation.

However this does not seem to be resolved, as the following error occurs when saving a node:

Le site Web a rencontré une erreur inattendue. Veuillez essayer de nouveau plus tard.

Drupal\Core\Entity\EntityStorageException: Template "core/themes/seven/templates/field--node--field-ni-related-block-link--news-item.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "core/themes/seven/templates/field--node--field-ni-related-block-link--news-item.html.twig" in the Drupal theme registry.) in "themes/custom/CUSTOM_THEMENAME/templates/field--node--field-spon-side-block-link--sponsoring-item.html.twig" at line 1. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 770 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

It seems that the patch actually causes the issue here.
I'll create a patch to show which lines cause the issue, when these are removed the issue is resolved.
However there must be a reason why they exist ?

Issue fork search_api-2869103

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

hoebekewim created an issue. See original summary.

hoebekewim’s picture

Patch

drunken monkey’s picture

Status: Active » Postponed (maintainer needs more info)

However there must be a reason why they exist ?

The reason is as discussed in #2732881: 'Rendered item' processor renders in the admin theme if indexing is done either on node edit or from the Search API UI: the "Rendered item" property should always use the same theme, not a different one based on where the indexing operation originated. So this code should definitely be there, but it might of course be buggy or have unintended side effects.

I have to admit, I'm not very familiar with the theme system, especially in Drupal 8, so I'm not sure what could be going wrong there. The error message sounds like there might actually be an issue with your custom template? What does the template contain, could you post/attach that?
Also, what's the theme setup on your site? I guess Seven as admin theme and CUSTOM_THEMENAME as your default theme? Are there any parent themes in use, could that maybe lead to the issue?

gremy’s picture

Status: Postponed (maintainer needs more info) » Needs review

@drunken-monkey Hello. I am currently running in the same issue using 8.x-1.3.

My use case:

  • In my custom theme I am preprocessing a node-type and I also provide a custom twig template for it

    Ex. node--custom-node.html twig and hook_preprocess_node__custom_node() in my custom theme.
  • I created a DB index that uses "Rendered HTML content" and I am using the Default view-mode for rendering
  • When I try to index I get twig errors for the custom variables that I add in my preprocess hook
  • After investigating with Xdebug and twig_xdebug I found out that the indexing is using the twig template from my custom theme, but not running the preprocess hook from my custom theme, instead it was using the "seven" admin theme
  • I tried the "patch" provided by @hoebekewim and that actually does not crash my indexing anymore.

I hope this is useful.

gremy’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: 2869103-2--admin-theme-wrongly-used.patch, failed testing. View results

drunken monkey’s picture

Component: General code » Plugins

Thanks for chiming in here. Good to know other people have the same problem. Seems there's really something not quite right with that code. Or, as I'd guess, something missing – namely, the clearing of some static cache(s) that still make some of the theme code refer to the "old" theme, not the one we explicitly set.
It's unfortunate that this leads to the indexing completely failing in such cases, and it's clear that removing the theme-switching code would resolve that problem. However, as explained above, this is definitely not the right solution, because it creates a different problem for everyone else using the processor (and you, too).

As said above, though, I don't know enough about the theme system to know how this should really be fixed. I'd need some theme layer expert to tell me that.
If no other solution presents itself, I guess we could make that code optional, maybe with a hidden setting. But it's really a workaround, not a solution.

borisson_’s picture

As said above, though, I don't know enough about the theme system to know how this should really be fixed. I'd need some theme layer expert to tell me that.
If no other solution presents itself, I guess we could make that code optional, maybe with a hidden setting. But it's really a workaround, not a solution.

I don't really know the solution either, but making that code optional sounds like a bad workaround. Is there a possibility to surface this to any of the core theme maintainers?

weri’s picture

weri’s picture

The problem we have seems related to this. The theme gets switched correctly, but the preprocess functions of the active theme are not called during the indexing. That's the current state, but I will do some more debugging.

osopolar’s picture

I have the same problem. Did somebody find a solution or at least a better workaround? The patch in #2 may lead to render the content using the admin-theme, what at least in my case is not desired.

osopolar’s picture

It seems that this issue brought me on the right way: #3049414: {{ directory }} in Twig template resolves the directory of the child theme rather than that of the parent theme.

It seems that Search API \Drupal\search_api\Plugin\search_api\processor\RenderedItem::addFieldValues() is using the right theme but, there is a function _template_preprocess_default_variables() in core/includes/theme.inc which seems to be called before that. There the directory variable get set incorrectly to the admin-theme path ($variables['directory'] = \Drupal::theme()->getActiveTheme()->getPath();) and static cached at first call (at least in my case).

osopolar’s picture

Status: Needs work » Needs review

Resetting the statically cached default variables fixes the issue for me.

Patch for MR: https://git.drupalcode.org/project/search_api/-/merge_requests/19.patch

drunken monkey’s picture

StatusFileSize
new1.47 KB
new2.07 KB

Thanks a lot for debugging, would be awesome to finally resolve this!
Unfortunately, automated testing of issue forks doesn’t work in this project for some reason (cf. #3190024: Problem with test dependencies when testing issue forks), so we’ll need to work with patches.

Attaching a minor revision, where the only real change is removing one of the two $this->getThemeManager()->setActiveTheme($default_theme); lines – I assume you meant to move it into the if, not have it twice?
Otherwise, this looks really good, so if you don’t object to my changes, I’d think this is RTBC.

Thanks a lot again, great job!

drunken monkey’s picture

Status: Needs review » Fixed

Committed. Thanks a lot again!

osopolar’s picture

Attaching a minor revision, where the only real change is removing one of the two $this->getThemeManager()->setActiveTheme($default_theme); lines – I assume you meant to move it into the if, not have it twice?

Yes it makes total sense, I don't know why I missed this. Thanks for fixing.

Status: Fixed » Closed (fixed)

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