I had the same mistake. I have solved it deleting `{% trans %}`. I had this code:
`{% trans %} {{ 'My text'|t }} {% endtrans %} and then some inputs with "name" attribute. I have delete `{% trans %}` and now it works fine.

Comments

crash_springfield created an issue. See original summary.

amateescu’s picture

crash_springfield’s picture

Title: Update from 8.6.10 to 8.6.11 breaks certain admin pages » Update from 8.6.10 to 8.6.12 breaks certain admin pages
Version: 8.6.11 » 8.6.12
Issue summary: View changes
Status: Closed (duplicate) » Active

I posted this in the referenced issue and was told to create a new issue because none of the patches worked. This is not a duplicate. This is a different issue.

cilefen’s picture

What version of Twig is installed in the site?

crash_springfield’s picture

@cilefen v1.38.2

cilefen’s picture

I can't reproduce it.

jproctor’s picture

Error messages above are missing full template filenames. For posterity (and searchability):

/admin/modules/uninstall => core/themes/stable/templates/admin/system-modules-uninstall.html.twig

/admin/reports/dblog => core/themes/classy/templates/views/views-mini-pager.html.twig

/admin/reports/updates => core/themes/stable/templates/admin/update-project-status.html.twig

I’m seeing this too, and also in my user-facing theme for some content types but not others. Since it’s not easy to reproduce, I’m going to go spend a little time reading #3039408: Updating twig/twig to v1.38.0 or v1.38.1 causes fatal error and #3040210: Updating twig/twig to v1.38.2 (included in Drupal 8.6.11) causes fatal error when embed tags are used in templates and see if I can figure out how to diagnose a little further.

jproctor’s picture

I worked on dblog first, and tracked it down to the Twig node that was failing:

Project_trans_Node(
  count: 
  body: Twig\Node\Node(
          0: Twig\Node\TextNode(data: '            Page ')
          1: Twig\Node\PrintNode(
               expr: Twig\Node\Expression\FilterExpression(
                       node: Twig\Node\CheckToStringNode(
                               expr: Twig\Node\Expression\GetAttrExpression(type: 'any', is_defined_test: false, ignore_strict_check: false, disable_c_ext: false
                                       node: Twig\Node\Expression\NameExpression(name: 'items', is_defined_test: false, ignore_strict_check: false, always_defined: false)
                                       attribute: Twig\Node\Expression\ConstantExpression(value: 'current')
                                       arguments: Twig\Node\Expression\ArrayExpression()
                                     )
                             )
                       filter: Twig\Node\Expression\ConstantExpression(value: 'drupal_escape')
                       arguments: Twig\Node\Node(
                                    0: Twig\Node\Expression\ConstantExpression(value: 'html')
                                    1: Twig\Node\Expression\ConstantExpression(value: NULL)
                                    2: Twig\Node\Expression\ConstantExpression(value: true)
                                  )
                     )
             )
          2: Twig\Node\TextNode(data: '          ')
        )
  plural: 
  options: 
)

... which corresponds with this section of the template:

          {% trans %}
            Page {{ items.current }}
          {% endtrans %}

When I remove the call to {% trans %}, it works fine.

Same issue in the other two files: it fails on calls to {% trans %} in the template. The t filter was fine, though.

I found the same problem in /node/add, but there are other instances of {% trans %} that don’t seem to have a problem (at least for me, using Seven as the admin theme).

Aside: The first one that I checked, core/themes/stable/templates/admin/ckeditor-settings-toolbar.html.twig, had a Twig\Node\Expression\ConstantExpression instead of a Project_trans_Node.

I don’t know what’s different about those that are broken.

According to the Translation API Overview page, the {% trans %} block should be “supported (as defined by the Twig i18n extension)”. I assume that means the code in core/lib/Drupal/Core/Template/TwigTransTokenParser.php is supposed to be able to behave the same way as the actual Twig i18n extension, not that said extension is installed (it isn’t).

I tried adding watchdog lines to the top of its parse() and checkTransString() functions, and neither was called in the course of rendering one of the pages that throws an error. I know it is being created by the getTokenParsers() function in TwigExtension, so I started looking in Twig code again.

I suspect there is at least one error in Twig itself: in the getTags() function in src/Environment.php (which is the superclass of Drupal’s TwigEnvironment), it calls $this->getTokenParsers()->getParsers(), but every occurrence of getTokenParsers() everywhere returns a simple array, not an object that will respond to method getParsers(). This is not the problem we’re having, though.

I found the place where the parsers are attached to the tags, and finally figured out what’s going on. Drupal’s native TwigTransTokenParser is being clobbered by the Unified Twig Extensions module, which is not in Contrib. Mine was installed by Emulsify, which is.

This module looks in the theme for Twig extensions and adds them. Emulsify defines a trans tag extension, and it was being loaded second, and its code relies on the name attribute (which was recently removed from Twig). Thus the error message.

Emulsify version 2.4 renamed the file, which removes the conflict in a way that I don’t fully understand, but which worked for me:

Solution?

@crash_springfield, if you have an Emulsify-based theme, look in your user-facing theme folder(s), and you should have components/_twig-components/tags/trans.tag.php . Rename this to pl_trans.tag.php and replace all occurrences of Project_trans with Project_pl_trans (this will create things like Project_pl_trans_Node and Project_pl_trans_TokenParser). Then, also in your theme folder(s), make sure the file is updated in pattern-lab/source/; in my case that folder is a symlink to components/, so there was nothing else to do. And then, obviously, clear cache.

Emulsify has also updated the names of filters and functions, so you may want to get caught up on those as well if yours don’t have the pl_ prefix.

crash_springfield’s picture

@jproctor Thanks for the advice! I just managed to narrow it down to vaguely knowing there was an issue with my theme. This is exactly it!

crash_springfield’s picture

Status: Active » Closed (works as designed)

Issue was with an outdated Emulsify dependency.

jeromewiley’s picture

I'm using Canna (also called Cannan) theme, which I believe is not based on Emulsify. How to fix this? Will do a filename search for trans.tag.php in my codebase for now.

jeromewiley’s picture

No files found by that name. I'm getting the exact same error as reported above after attempting to upgrade from 8.6.10 -> 8.6.13 and twig from 1.37.1 -> 1.38.2.
Please advise fix.

cilefen’s picture

Does the theme code reference Twig_Node or Twig\Node\Node in its codebase?

jproctor’s picture

Something—either your theme or a module—is using code like getAttribute('name'). Find that and you’ll find what’s causing the error.

Challenge 1: it might not be 'name'; it might be "name" or some variable that evaluates to that string, possibly in a loop.

Challenge 2: when you find it, you still have to figure out how to fix it, but at least it gives you a better place to look.

guardiola86’s picture

In my case, I've upgraded to the latest core version: 8.6.13 and I'm using twig version 1.38.4. I've commented out this line in web/core/themes/classy/templates/content/node.html.twig:

{% trans %}Submitted by {{ author_name }} on {{ date }}{% endtrans %}

After removing that line, I don't get the error again. Any idea how to fix this?

cilefen’s picture

Title: Update from 8.6.10 to 8.6.12 breaks certain admin pages » Twig\Error\SyntaxError: An exception has been thrown during the compilation of a template ("Attribute "name" does not exist for Node "Twig\Node\CheckToStringNode"
Version: 8.6.12 » 8.6.x-dev
Component: other » theme system
Priority: Normal » Major
Status: Closed (works as designed) » Active
lalop’s picture

I have the same issue on page /admin/structure/block/block-content

An exception has been thrown during the compilation of a template ("Attribute "name" does not exist for Node "Twig\Node\CheckToStringNode".") in "core/themes/classy/templates/views/views-mini-pager.html.twig
BigEd’s picture

I spent a bit of time trying to debug this, looks like {% trans %} is where it does not like the use of getAttribute('name') in the new version of Twig.

This must be something that we are doing in a module or a theme Twig version 1.38.4 does not like, so have been through the theme and module functions and so far they look correct.

I can remove {% trans %} from the node.tpl.php and add this to the theme and this gets the site running but this is not really a solution to fixing the issue and is just masking the problem.

I suspect we all using some sort of implementation of twig module like Pattern labs that is causing the twig to chuck its toys out, so If I can narrow down where the issue is I can make a patch.

berdir’s picture

Core doesn't have any problems with %trans, there are some contrib modules/themes that overrride that, so check the theme and modules that you are using. I'm pretty sure that this is nothing that can be fixed in core. There have been problems with that before, see for example #2895187: Twig_Error_Syntax: The text to be translated with "trans" can only contain references to simple variables in Twig_Extensions_TokenParser_Trans->checkTransString()

BigEd’s picture

Yes, sorry i don't think I made that very clear. As you say Berdir it's not core its a theme issue.

d70rr3s’s picture

StatusFileSize
new57.6 KB
new3.78 KB

Hi, I've step on this too, in my case I'm using a theme based on Acquia's Cog starter theme (didn't find any whacky-hacky around their .theme) and I'm not using any modules to extend Twig engine beyond core's support. The snippet of code throwing the error (see screenshot 1) is the following:

{% set year = 'now' | date('Y') %}
{% trans with {'context': 'copyright'} %}
    © {{ year }} Acme Inc.. All rights reserved.
{% endtrans %}

If I remove the {{ year }} it just works fine. I can confirm that year is a simple var, just a string with the current year using 4 digits format (see screenshot 2).

In my case just fallback to use the t filter and is currently working but it would be nice to figure out whats going on for more complex templates like emails.

Ah almost forgot: Core 8.6.10, Twig 1.38.4

d70rr3s’s picture

StatusFileSize
new71.11 KB

Hi again, this issue is affecting also core themes not only custom themes (see screenshot) and this seems to be bogus code:

 <p>
    {% set create_content = path('node.type_add') %}
    {% trans %}
      You have not created any content types yet. Go to the <a href="{{ create_content }}">content type creation page</a> to add a new content type.
    {% endtrans %}
 </p>

Since this seems wide-spreading to more cases, I ended up downgrading Twig to the lowest required version and now is working fine:

chi-teck/drupal-code-generator               1.28.0   requires  twig/twig (^1.35)
drupal/core                                              8.6.10   requires  twig/twig (^1.35.0)
jcalderonzumba/mink-phantomjs-driver  v0.3.3   requires  twig/twig (~1.20|~2.0)
symfony/twig-bridge                               v3.4.23  requires  twig/twig (^1.37.1|^2.6.2)
Gathering patches for dependencies. This might take a minute.                                              
  - Downgrading twig/twig (v1.38.4 => v1.37.1): Loading from cache                                         
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file                                                                                          
mikelutz’s picture

Status: Active » Closed (works as designed)

d70rr3s - You are on 8.6.10, this issue in core was fixed in 8.6.12. You need to update core. If you were on 8.6.12, you wouldn't be able to downgrade to 1.37 because that version is no longer supported as of 8.6.12.

I'm closing this issue again, as we have not identified any core issues in this thread.

All issues with the %trans% tag have been accounted for either from a custom module or theme overriding core's trans, which is fixed, or by the reporter being on an old version of Drupal < 8.6.12 in which core's trans had not yet been fixed.

guardiola86’s picture

@jproctor solution worked for me.

dalin’s picture

The solution proposed by @jproctor in comment 8 worked for me. Even though I only saw the exception on an admin page, and it was my front-end (emulsify-based) theme that was the problem.

In short:

cd themes/custom
find . -type f -name "*.php" -print0 | xargs -0 sed -i '' -e 's/Project_trans/Project_pl_trans/g'

I also tried someone else's suggestion of downgrading twig/twig, but that didn't work for me. The lowest that I could go was 1.38.2 which still had the problem.

code-brighton’s picture

I was getting this error after upgrade to Core 8.6.15 from 8.6.10. The solution was found at https://www.drupal.org/project/drupal/issues/3051269 I needed to update twig/twig in my lock file from 1.40.0 to 1.40.1

sararellano’s picture

Issue summary: View changes

I had the same mistake. I have solved it deleting `{% trans %}`. I had this code:
`{% trans %} {{ 'My text'|t }} {% endtrans %} and then some inputs with "name" attribute. I have delete `{% trans %}` and now it works fine.

ressa’s picture

I was trying to translating strings inside {% trans %} {% plural count %} Twig tags for Facets Summary with this, and failed:

{% trans %}{{ count }} {{ "municipality"|t }}{% plural count %}{{ count }} {{ "municipalities"|t }}{% endtrans %}

With Twig variables, I could translate the strings first, and then use them inside {% trans %} like this:

{% set muni = "municipality"|trans %}
{% set munis = "municipalities"|trans %}
{% trans %}{{ count }} {{muni}}{% plural count %}{{ count }} {{munis}}{% endtrans %}