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
Comment #2
kenorb commentedComment #3
steve.m commentedComment #4
borisson_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
Comment #5
darvanenYep, good one.
Comment #6
darvanenActually, I think we should also patch the other templates, i.e. in classy. Patch coming.
Comment #7
darvanenOk, 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.
Comment #8
berdirThis 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.
Comment #9
darvanenYou're absolutely right.
It definitely crashes without some kind of change (when twig_extensions are enabled), so how's this one?
Comment #11
darvanenWhoops, that'll teach me to eyeball results before posting a patch:
Back to the drawing board.
Comment #12
darvanenSo, it appears perhaps we don't support this kind of thing?
Comment #13
darvanenOk 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.
Comment #14
darvanenI'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]
Comment #15
berdirThis 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.
Comment #16
darvanenI agree on the upgrade to major, thanks @Berdir.
To replicate:
/admin/structure/block/block-content(I also found a new one yesterday at
/admin/reports/updates, which uses theupdate-project-status.html.twigtemplate)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.phpShould we consider removing the twig translation extension from services? (patch attached)
What repercussions would this have, if any?
Comment #17
.jch commentedCore 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).
Comment #18
CJdriver commentedI'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.
Comment #19
darvanen@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).
Comment #20
vijaycs85considering 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_extensions3. 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.
Comment #21
vijaycs85Comment #22
vijaycs85Here is the test coverage for this issue.
Comment #23
vijaycs85Just noticed we don't have tests enabled.Tests running on issues now.Comment #25
vijaycs85Comment #27
joelpittetI 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!
Comment #28
vijaycs85Thanks @joelpittet!
Comment #29
darvanen@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?
Comment #31
joelpittet@Darvanen good point, i reverted it on the 1.x branch because it shouldn't be there.
Comment #32
darvanenMany thanks for the clarification :)