Follow-up to #2568181: [META] Update to Twig 2.x in Drupal 9
Problem/Motivation
We are currently using some deprecated code that will be removed or not workable in Twig 2.0.
To help move that along we would like to make the TwigExtension more UnitTestable.
Proposed resolution
- Move twig_without() to the extension
Remaining tasks
User interface changes
n/a
API changes
n/a
Data model changes
n/a

| Comment | File | Size | Author |
|---|---|---|---|
| #60 | 2591515-60.patch | 5.75 KB | joelpittet |
Comments
Comment #2
joelpittetHere is that patch only.
Comment #3
joelpittetComment #4
star-szrYup.
Comment #5
alexpottDiscussed with @effulgentsia and we're not sure how this is necessary for Twig 2.0.x - yes it is nice to have more unit testable code but nice to have does not mean it is allowed in RC. We're trying to minimise all change. If we're wrong - re-add the tag and improve the issue summary to explain why it is necessary for RC.
Comment #6
joelpittet@alexpott it just forces us to add fake methods at the bottom of the unit tests and 2.0.x changed how the extension was loaded and the unit tests fail, so instead of doing the janky if
!function_exists('twig_without')) {at the bottom we clean up the tests at the same time. Also unlikely people are using .engine code, so super minimal change as this all should be considered internal anyway.Comment #7
star-szrActually the unit test in question *actually* tests this function from what I recall. So we'd have to copy + paste it into the test class or include/require the file.
I don't think this needs to go in RC but it should probably be done along the path to Twig 2.0.x.
Comment #8
star-szrSplitting #2568181: [META] Update to Twig 2.x in Drupal 9 and these changes make the most sense here. Also have some other BC ideas but want to get this up.
Comment #10
star-szrJust an idea to make it a bit less disruptive. Will look at that failing test.
Comment #11
star-szrNot sure if this is the way to go, needs discussion.
Maybe we actually want to implement initRuntime() in our Twig extension. Seems kinda odd that the extension would be responsible for loading the theme engine though.
Comment #13
star-szrNever mind that idea: https://github.com/twigphp/Twig/commit/9774f4f1a43ae7c1dfd952430b1826217...
Comment #14
joelpittetThanks for trying to make this less disruptive @Cottser!
This looks like it snuck into the patch.
Love that this gets removed!
Strange that this test needed to add the engine, was this for that function? If so it's a test and maybe we can make it use the method.
Comment #15
joelpittetComment #16
star-szr#14.1 was intentional, that change is needed for Twig 2.x and wasn't sure where to put it when splitting #2568181: [META] Update to Twig 2.x in Drupal 9.
Regarding #14.3 what method?
Comment #17
joelpittet@Cottser Yeah that doesn't read right #14.3
twig_render_template()maybe go with the rest of the fire too?Comment #18
star-szrI think that would have to be part of an engine refactor as plugins or classes of some kind. It's a "hook" of sorts for theme engines, nyan cat should have one similar.
Comment #19
xjmComment #20
star-szrI think the feedback from #14 was addressed without changing any code, if I'm wrong please let me know :)
Comment #25
joelpittetIf this didn't sneak in and was intentional, it at the very least needs a docblock saying what it's for.
Really wish we didn't need to add this.
Really doubt anybody would use this function in *.engine, would still rather remove it than leave the BC laying about.
If there is anybody is using this I am willing to console them in their loss.
Comment #26
alexpottDiscussed with @effulgentsia and @xjm we couldn't see why this is 100% necessary for Twig 2.0 and why the change is necessary for RC. I can see that the consistency of having everything together in the TwigExtension is nice - but it only looks like a nice-to-have to me.
Comment #27
star-szrAt least as it stands now, our test suite will fail with Twig 2.x.x without these changes. Sorry a bit short on time now otherwise I'd like to an example patch showing those fails.
Comment #28
xjm@Cottser, that's okay I think. #26 doesn't quite reflect my take on it, which is that these changes are safe to make in a minor, and therefore can be made in 8.1.x or whenever as we are introducing Twig 2.x.
Comment #29
star-szrYep, that's totally fine with me :) thanks!
Comment #30
joelpittetComment #31
joelpittetChanged it to "move" so people don't decide to copy the code. Also this patch is doing very similar clean-up.
#2612800: Deprecate drupal_find_theme_templates to Theme Registry service
Comment #33
xjmComment #34
joelpittetUnpostponing so we can tidy the feedback up in #25 and keep on trucking
Comment #38
joelpittetRerolled and removed the lines I mentioned in comment #25.1 and #25.2.
Comment #39
joelpittetThis is a normal issue.
Comment #41
joelpittetAh, I see why the include was made, put it back. Also changed
\Drupal::root()to$this->rootand rooted thenamespace for \ArrayAccesssince we are in a namespace now.Comment #43
markhalliwellComment #44
kostyashupenkoComment #45
joelpittetThe original patch wasn't mine and this has been sitting around for ages. Thank you for the re-roll @kostyashupenko!
I diffed the diffs and it looks perfect.
Comment #46
alexpottI don't think we can do this in a minor as it could result in code breaking that is relying on it. For example if something calls the deprecated twig_without function. We should file a follow-up to remove this in Drupal 9.
We only deprecate stuff in the next minor release - so 8.6.x.
This needs to trigger a silenced error - see https://www.drupal.org/core/deprecation - we also need a change record.
Comment #48
nickdickinsonwildeComment #49
nickdickinsonwildeDraft Change Record
Also added trigger error and removed potentially breaking changes as noted in #2591515-46: Move twig_without() to the TwigExtension where all the other filters are and deprecate
Comment #50
lauriiiOpened the follow-up and added a todo #3011393: Remove twig_without() and manual inclusion of twig.engine TwigEnvironment to address #46.1.
Comment #51
joelpittetSmall change but otherwise RTBC.
This error needs to be updated to the new method name
withoutFilter().Comment #52
lauriiiThanks for the review @joelpittet!
Comment #53
joelpittetThanks, back to RTBC, pending tests
Comment #54
alexpottGiven there is some complexity here of calling with a number of args I think it is worth a deprecation test. I think a kernel test would probably be easiest.
Comment #55
joelpittet@alexpott, Feels like this kind of test doesn't have a great ROI...
Comment #56
alexpott@joelpittet can you add the
@expectedDeprecationannotation to the legacy test. That way we are testing that the code changes don't work and that the expected deprecation message is triggered.Comment #57
joelpittetAdded
Based on some others I found in core that looked potentially similar, LMK if that works?
Comment #58
alexpottThis should be
* @expectedDeprecation twig_without() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Template\TwigExtension::withoutFilter(). See https://www.drupal.org/node/3011154.It needs to be the full deprecation message.
It's worth running tests before you upload them :) ie. something like:
./vendor/bin/phpunit -v -c ./core core/modules/system/tests/src/Kernel/Theme/TwigFilterTest.php --filter testLegacyTwigWithoutFunctionComment #59
joelpittet@alexpott in a rush at work, didn't have time for that. Thought I was just adding effectively a comment so didn't think re-running a green test would do anything.
Comment #60
joelpittetI usually run them if I have time, didn't know that
@expectedDeprecationwas checked I thought is was just like @group legacy where it helped tag it as expecting to be deprecated and removed later...🤦♂️Always new things to keep on top of, a bit tricky to keep up.Comment #61
lauriiiThis addresses feedback from @alexpott! Thank you @joelpittet!
Comment #62
alexpottCommitted 9dfb602 and pushed to 8.7.x. Thanks!
Comment #64
joelpittet🧯💨🔥
Comment #65
andypostRemaining issue for 2.0 compatibility is #2572605: Make Drupal 8 optionally working with Twig 2, while keeping its dependency on Twig 1