Fix Drupal ~10.0 and TWIG ~3.0 Unexpected token "name" of value "if" ("end of statement block" expected)

Created on 8 April 2025, 4 months ago

Problem/Motivation

The following error occurs in Drupal 10.4.3 with Twig v3.19.0:

The website encountered an unexpected error. Try again later.

Twig\Error\SyntaxError: Unexpected token "name" of value "if" ("end of statement block" expected). in Twig\TokenStream->expect() (line 5 of modules/contrib/forum_access/templates/forum-access-table.html.twig).

Twig\TokenParser\ForTokenParser->parse(Object) (Line: 202)
Twig\Parser->subparse(Array) (Line: 42)
Twig\TokenParser\IfTokenParser->parse(Object) (Line: 202)
Twig\Parser->subparse(NULL, ) (Line: 95)
Twig\Parser->parse(Object) (Line: 559)
Twig\Environment->parse(Object) (Line: 590)
Twig\Environment->compileSource(Object) (Line: 131)
Drupal\Core\Template\TwigEnvironment->compileSource(Object) (Line: 409)
Twig\Environment->loadTemplate('__TwigTemplate_82e7ac16ef626a053af3fefb7f6232d9', 'modules/contrib/forum_access/templates/forum-access-table.html.twig') (Line: 371)
Twig\Environment->load('modules/contrib/forum_access/templates/forum-access-table.html.twig') (Line: 33)
twig_render_template('modules/contrib/forum_access/templates/forum-access-table.html.twig', Array) (Line: 348)
Drupal\Core\Theme\ThemeManager->render('forum_access_table', Array) (Line: 491)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 504)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 504)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 248)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 638)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 231)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 186)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 116)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 90)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 53)
Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 741)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

Install the module in a Drupal 10+ installation using twig 3+ and try to edit a forum or a container.

Proposed resolution

Modify the forum-access-table.html.twig template to make it work with twig 3 by separating if clauses from for loops as for if is deprecated since Twig 2.10 (see https://twig.symfony.com/doc/2.x/deprecated.html#tags).

Remaining tasks

  • Update the template file

User interface changes

N/A

API changes

N/A

Data model changes

N/A

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany semiaddict

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

Merge Requests

Comments & Activities

  • Issue created by @semiaddict
  • Pipeline finished with Success
    4 months ago
    Total: 145s
    #468291
  • πŸ‡ΊπŸ‡¦Ukraine voleger Ukraine, Rivne

    Upload the static patch from MR

  • πŸ‡ΊπŸ‡¦Ukraine voleger Ukraine, Rivne

    Works for Drupal ~10 setup.

  • πŸ‡ΊπŸ‡ΈUnited States lee56

    Installed the Forum Access version 2.0.1 / ACL version 2.0.1 modules on 5/15/2025 on test site.

    Had same error issues described for Drupal version 10.4.3 with 11.1.7 - Reported by "semiaddict"

    Twig\Error\SyntaxError:
    Unexpected token "name" of value "if" ("end of statement block" expected). in Twig\TokenStream->expect() (line 5 of modules/contrib/forum_access/templates/forum-access-table.html.twig).

    Applied the patch:
    forum_access-3517929-3.patch

    The patch also works without any known issues for Drupal 11.1.7

    Thanks to "voleger" for the patch and to "semiaddict" for reporting the issue.

Production build 0.71.5 2024