Problem/Motivation

URL: /admin/structure/block/block-content

PHP 7.2.0alpha2

The website encountered an unexpected error. Please try again later.

Twig_Error_Syntax: The text to be translated with "trans" can only contain references to simple variables in Twig_Extensions_TokenParser_Trans->checkTransString() (line 26 of core/themes/classy/templates/views/views-mini-pager.html.twig).
Twig_Extensions_TokenParser_Trans->parse(Object) (Line: 190)
Twig_Parser->subparse(Array) (Line: 36)
Twig_TokenParser_If->parse(Object) (Line: 190)
Twig_Parser->subparse(Array) (Line: 36)
Twig_TokenParser_If->parse(Object) (Line: 190)
Twig_Parser->subparse(NULL, ) (Line: 103)
Twig_Parser->parse(Object) (Line: 692)
Twig_Environment->parse(Object) (Line: 750)
Twig_Environment->compileSource(Object) (Line: 447)
Twig_Environment->loadTemplate('core/themes/classy/templates/views/views-mini-pager.html.twig') (Line: 64)
twig_render_template('core/themes/classy/templates/views/views-mini-pager.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('views_mini_pager', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 136)
__TwigTemplate_0b39505df84b691c5a3640573ee0790767231d2654971965753660bef51abd95->doDisplay(Array, Array) (Line: 432)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template->display(Array) (Line: 411)
Twig_Template->render(Array) (Line: 64)
twig_render_template('core/themes/classy/templates/views/views-view.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('views_view', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 448)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Twig code:

        <li class="pager__item is-active">
          {% trans %}
            Page {{ items.current }}
          {% endtrans %}
        </li>

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

kenorb created an issue. See original summary.

kenorb’s picture

Issue summary: View changes
steve.m’s picture

Status: Active » Needs review
StatusFileSize
new571 bytes
borisson_’s picture

Version: 8.3.5 » 8.5.x-dev

Not sure why this patch was tested with php5.3 - that's not supposed to work with drupal 8 - I started a new test on php7.1

darvanen’s picture

Status: Needs review » Reviewed & tested by the community

Yep, good one.

darvanen’s picture

Status: Reviewed & tested by the community » Needs work

Actually, I think we should also patch the other templates, i.e. in classy. Patch coming.

darvanen’s picture

Status: Needs work » Needs review
StatusFileSize
new1.18 KB

Ok, updated patch, haven't done an interdiff because all I did was the same treatment to the classy template. Couldn't find any other instances.

Note to reviewers - I think this issue only shows up with twig_extensions enabled.

berdir’s picture

Status: Needs review » Needs work

This is not correct and this can not be correctly translated to RTL languages for example, where it basically needs to be able to translate this to "1 Page-in-russian".

if that complex thing doesn't work then what you should be able to do is a {% set current_page = items.current %} or so before.

But maybe the problem is also in \Drupal\Core\Template\TwigTransTokenParser::checkTransString() because that seems like a valid use case.

darvanen’s picture

Status: Needs work » Needs review
StatusFileSize
new1.91 KB

You're absolutely right.

It definitely crashes without some kind of change (when twig_extensions are enabled), so how's this one?

Status: Needs review » Needs work

The last submitted patch, 9: core-twig_error-2895187-9.patch, failed testing. View results

darvanen’s picture

Issue summary: View changes
StatusFileSize
new7.98 KB

Whoops, that'll teach me to eyeball results before posting a patch:

Back to the drawing board.

darvanen’s picture

So, it appears perhaps we don't support this kind of thing?

darvanen’s picture

Version: 8.5.x-dev » 8.6.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.69 KB

Ok so here's a different, probably more controversial approach. This patch removes the translation from the template.

Has the stance changed since that comment I found from a few years ago? Do we now support {% trans %}?

If not, this seems like the simplest option to me considering that it's currently broken and cannot be used by any site in it's current state once the translation extension is enabled.

darvanen’s picture

Status: Needs review » Needs work

I've just discovered {% trans %} shows up nearly 100 times in core, looks like it's supported. This needs a different solution because the one Berdir suggested isn't working.

[edit: I've remembered now that I was talking about translating of variables being unsupported, still, I think this needs some expert help]

berdir’s picture

Component: views.module » theme system
Priority: Minor » Major
Status: Needs work » Postponed (maintainer needs more info)

This still doesn't make sense to me. If just visiting that page would result in an error then would it not happen for everyone?

So why doesn't it? What is your PHP version? How many blocks do you have? Can you debug the value of items.current? Try a var_dump()/breakpoint when the exception is thrown in \Drupal\Core\Template\TwigTransTokenParser::checkTransString().

Also, the priority minor can't be right. An exception just by visiting a certain URL is at least major, if it is reproducible.

darvanen’s picture

Project: Drupal core » Twig Extensions
Version: 8.6.x-dev » 8.x-1.x-dev
Component: theme system » Code
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new453 bytes

I agree on the upgrade to major, thanks @Berdir.

To replicate:

  • Install a fresh Drupal site with Standard profile
  • Download and enable Twig Extensions module
  • Go to /admin/structure/block/block-content

(I also found a new one yesterday at /admin/reports/updates, which uses the update-project-status.html.twig template)

I now think this is an issue for twig_extensions.

It looks like Drupal already has a Twig translation system in place that caters for Drupal's needs: /core/lib/Drupal/Core/Template/TwigTransTokenParser.php
Should we consider removing the twig translation extension from services? (patch attached)

What repercussions would this have, if any?

.jch’s picture

StatusFileSize
new57.88 KB

Core 8.5.4 drupalcommerce/project-base
PHP/7.2.4 1024M memory limit.

composer require drupal/twig_tweak:^2.0
bin\drupal module:install twig_tweak

composer require drupal/twig_extensions
composer require twig/extensions
bin\drupal module:install twig_extensions

Applied twig_extension-trans_error-2895187-16.patch

Installing/enabling twig_extensions with or without patch results in the generic white screen of death at site startup...
"The website encountered an unexpected error. Please try again later."

This is bartik with no custom theming. Regardless if patch is applied or not the error message is logged...

Twig_Error_Syntax: The text to be translated with "trans" can only contain references to simple variables in Twig_Extensions_TokenParser_Trans->checkTransString() (line 17 of ...\web\core\themes\bartik\templates\block--system-menu-block.html.twig).

CJdriver’s picture

I'll chime in because this issue has kinda kidnapped my staging site. I'm getting this error all across my admin pages. Cannot access the module uninstall page to uninstall the Twig Extensions module to solve the issue. Applying the twig_extension-trans_error-2895187-16.patch did solve my problem on my local, but with this being installed via composer I can't just push up to staging. Guess I'll try and deactivate the module via the DB now. If we could get this fix into the dev version it would be much appreciated.

I'm not sure we should have this module marked as a stable release whenever it blocks your basic admin pages after installation. Including the method of removing it.

For those coming here who are also locked out of their module uninstall, you can manually disable twig_extensions using the devel php page. so at /devel/php execute the following.

// Read the configuration.
$module_data = \Drupal::config('core.extension')->get('module');

// Unset the modules you do not need.
unset($module_data['twig_extensions']);

// Write the configuration.
\Drupal::configFactory()->getEditable('core.extension')->set('module', $module_data)->save();
darvanen’s picture

@Cjdriver, if the only problem you're facing is not being able to apply the patch with composer, have a look at cweagans/composer-patches and how it is implemented in drupal-composer/drupal-project.

Feel free to msg me on Drupal Slack if you get stuck (or just try the #support channel).

vijaycs85’s picture

Priority: Major » Critical
Status: Needs review » Reviewed & tested by the community

considering it breaks the core functionalities (mini pager), it's quite critical. I applied the patch in #17 and it fixes the error. Let's get it in.

Steps to reproduce

1. Install core (using current stable: 8.5.3) with standard.
2. composer require drupal/twig_extensions
3. Update content view to display 1 item per page and change pager to "mini pager"
4. Create two article
5. Go to admin/content.

Produces error trace in the issue summary.

vijaycs85’s picture

Issue summary: View changes
vijaycs85’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.26 KB
new1.82 KB

Here is the test coverage for this issue.

vijaycs85’s picture

Just noticed we don't have tests enabled. Tests running on issues now.

Status: Needs review » Needs work

The last submitted patch, 22: 2895187-22-test-only.patch, failed testing. View results

vijaycs85’s picture

Status: Needs work » Needs review

  • joelpittet committed 4356294 on 8.x-1.x authored by vijaycs85
    Issue #2895187 by Darvanen, vijaycs85, steve.m, .jch: Twig_Error_Syntax...
joelpittet’s picture

Status: Needs review » Fixed

I can't see where trans tag provided would be much help, maybe someone uses it, if they do I hope they speak up. Otherwise I've committed and released this. Thanks for your patience, it's been a busy time!

vijaycs85’s picture

Thanks @joelpittet!

darvanen’s picture

@joelpittet - when you say 'released', I see it was committed in 8.x-1.x but I assume the release in question was in 8.x-2.0?

  • joelpittet committed 2e73803 on 8.x-1.x
    Revert "Issue #2895187 by Darvanen, vijaycs85, steve.m, .jch:...
joelpittet’s picture

Version: 8.x-1.x-dev » 8.x-2.0

@Darvanen good point, i reverted it on the 1.x branch because it shouldn't be there.

darvanen’s picture

Many thanks for the clarification :)

Status: Fixed » Closed (fixed)

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