Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Right now toString method is not callable from Twig templates because of our Sandbox policy but at least Url objects are using this.
Proposed resolution
Add toString method to the Twig sandbox policy whitelist
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#9 | add_tostring_method_to-2624284-9.patch | 1.47 KB | lauriii |
#9 | add_tostring_method_to-2624284-9-test-only.patch | 1.02 KB | lauriii |
#2 | add_tostring_method_to-2624284-2.patch | 465 bytes | lauriii |
Comments
Comment #2
lauriiiComment #3
lauriiiComment #4
Wim LeersGiven
__toString()
already exists, it only makes sense thattoString()
is treated the same.Also: the reason some objects don't use (the magic)
__toString()
, is because PHP will fatal/crash dramatically if an exception is thrown from there. So while this may seem strange, it has a very good reason.Comment #5
chx CreditAttribution: chx commented++ excellent idea.
Comment #6
alexpottShall we test it in TwigSandboxTest?
Comment #7
lauriiiIt seems like we don't have tests for the allows, instead we have tests for the disallowed methods. Maybe, maybe not, I don't have strong opinion.
Comment #8
alexpott\Drupal\Tests\Core\Template\TwigSandboxTest has tests for the allows. We can just test with a Url object. Also I'm not sure this is a bug so I think we should do this for 8.1.x and not in 8.0.x which is only for bugs fixes to the stable code base.
Comment #9
lauriiiComment #11
joelpittetBeauty, thanks @lauriii
Comment #12
alexpottCommitted cd4ae1b and pushed to 8.1.x. Thanks!