Skip to content

[5.x]: Optional chaining doesn't work in Craft's Twig environment #19279

Description

@MoritzLost

What happened?

Description

Twig 3.23 shipped optional chaining (null-safe operator), which should work in Craft 5.10, since that version bumped the Twig dependency. However, it looks like Craft's extension of the Twig environment breaks optional chaining completely.

{% set user = null %}
Hello {{ user?.name }}
{% set user = null %}
Hello {{ user?.getFullName() }}

Both of those result in a runtime error:

Impossible to access an attribute ("name") on a null variable in "path/to/template" at line 2.

While the template compiles correctly, the null-safe operator is apparently silently dropped. I checked the compiled template – the PHP line corresponding to that part of the template is completely identical, whether I use the null-safe operator or not.

This template works as expected in the playground. That's why it's probably an issue in Craft's implementation.

Craft CMS version

5.10.7

PHP version

8.5

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions