Score:0

How can I debug a Twig error that occurs after migrating from Drupal 8.9 to Drupal 9+

cn flag

I am trying migrate my Drupal 8.9 site to Drupal 9+ and I am stuck with an error its cause I am not able to hunt down. On almost every page I get the following PHP exception:

Twig\Error\RuntimeError: The "replace" filter expects an array or 

"Traversable" as replace values, got "string" in "__string_template__faabd18c7a37a1e54d0ef10115b24022e72bc201187fff502de48bf58589321f" at line 2. in twig_replace_filter() (line 554 of /var/customers/webs/freigeist/www9/vendor/twig/twig/src/Extension/CoreExtension.php).

__TwigTemplate_75ee113db094d36d71f294529e9d7055b2065bf116719e9a04b7e4795be3121d->doDisplay() (Line: 405)
Twig\Template->displayWithErrorHandling() (Line: 378)
Twig\Template->display() (Line: 390)
Twig\Template->render() (Line: 45)
Twig\TemplateWrapper->render() (Line: 235)
Drupal\Core\Template\TwigEnvironment->renderInline() (Line: 52)
Drupal\Core\Render\Element\InlineTemplate::preRenderInlineTemplate()
call_user_func_array() (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 786)
Drupal\Core\Render\Renderer->doCallback() (Line: 377)
Drupal\Core\Render\Renderer->doRender() (Line: 201)
Drupal\Core\Render\Renderer->render() (Line: 157)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 578)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 158)
Drupal\Core\Render\Renderer->renderPlain() (Line: 419)
Drupal\views\Plugin\views\PluginBase->viewsTokenReplace() (Line: 1356)
Drupal\views\Plugin\views\field\FieldPluginBase->renderAltered() (Line: 1249)
Drupal\views\Plugin\views\field\FieldPluginBase->renderText() (Line: 1177)
Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender() (Line: 238)
template_preprocess_views_view_field() (Line: 287)
Drupal\Core\Theme\ThemeManager->render() (Line: 436)
Drupal\Core\Render\Renderer->doRender() (Line: 201)
Drupal\Core\Render\Renderer->render() (Line: 1745)
Drupal\views\Plugin\views\field\FieldPluginBase->theme() (Line: 778)
Drupal\views\Plugin\views\style\StylePluginBase->elementPreRenderRow()
call_user_func_array() (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 786)
Drupal\Core\Render\Renderer->doCallback() (Line: 377)
Drupal\Core\Render\Renderer->doRender() (Line: 201)
Drupal\Core\Render\Renderer->render() (Line: 717)
Drupal\views\Plugin\views\style\StylePluginBase->renderFields() (Line: 584)
Drupal\views\Plugin\views\style\StylePluginBase->renderGrouping() (Line: 475)
Drupal\views\Plugin\views\style\StylePluginBase->render() (Line: 2170)
Drupal\views\Plugin\views\display\DisplayPluginBase->render() (Line: 1533)
Drupal\views\ViewExecutable->render() (Line: 183)
Drupal\views\Plugin\views\display\Page->execute() (Line: 1630)
Drupal\views\ViewExecutable->executeDisplay() (Line: 81)
Drupal\views\Element\View::preRenderViewElement()
call_user_func_array() (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 786)
Drupal\Core\Render\Renderer->doCallback() (Line: 377)
Drupal\Core\Render\Renderer->doRender() (Line: 201)
Drupal\Core\Render\Renderer->render() (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 578)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
call_user_func() (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 57)
Drupal\Core\StackMiddleware\Session->handle() (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 191)
Drupal\page_cache\StackMiddleware\PageCache->fetch() (Line: 128)
Drupal\page_cache\StackMiddleware\PageCache->lookup() (Line: 82)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
Stack\StackedHttpKernel->handle() (Line: 717)
Drupal\Core\DrupalKernel->handle() (Line: 19)

I found out that the error might be theme related (I am on Bootstrap). And on some pages I could narrow it down to the system block 'site_branding': Disabling this block makes the error disappear on some pages, but not on all.

My first guess was, that this might be an twig compatibility issue (may be in my bootstrap sub-theme), but it occurs also when I use other themes. So I am completely lost here.

How could I debug this error? Does the error message show, which component could be the cause of the error? Can anybody point out, which change between Drupal 8 and 9 could be responsible of the error?

4uk4 avatar
cn flag
What is the question? What is unclear about the error message? BTW the template name looks not like a real Twig template, more like an inline template in PHP. You could try to find it in a full error message with stack trace.
cn flag
@4k4 You are right, I did not ask a clear question, I think it was too late yesterday. I tried to make my questions clearer now. Should I use the devel module to stack trace.
4uk4 avatar
cn flag
You can enable full error messages in Drupal core: /admin/config/development/logging
Score:4
cn flag

This is the last line in the stack trace before the render pipeline is throwing the error:

Drupal\views\Plugin\views\PluginBase->viewsTokenReplace() (Line: 1356)

This might be caused by a |replace filter rewriting a Views field in the View you are trying to display as main content of the page.

See the deprecation warning in Drupal 8.9 (Twig 1.x):

function twig_replace_filter($str, $from, $to = null) {
  if (\is_string($from) && \is_string($to)) {
    @trigger_error('Using "replace" with character by character replacement is deprecated since version 1.22 and will be removed in Twig 2.0', E_USER_DEPRECATED);
  

https://api.drupal.org/api/drupal/vendor%21twig%21twig%21src%21Extension%21CoreExtension.php/function/twig_replace_filter/8.9.x

Drupal 9 has upgraded to Twig 2.x

https://api.drupal.org/api/drupal/vendor%21twig%21twig%21src%21Extension%21CoreExtension.php/function/twig_replace_filter/9.2.x

cn flag
Great, that helped a lot. It was the one dirty hack, in the main View I forgot about. How did you deduce however, that ´Drupal\views\Plugin\views\PluginBase->viewsTokenReplace() (Line: 1356)´ was the last line before the error?
4uk4 avatar
cn flag
The lines above are rendering methods which only run because line 419 of this Views plugin base class is starting a rendering process providing the data to be rendered. So it's very likely that this data is causing the issue, unless a faulty template had been involved in rendering the data. But then the error message had contained the template name, which would have made debugging much easier.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.