Follow-up to #2568181: [META] Update to Twig 2.x in Drupal 9

Change Record

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

Comments

joelpittet created an issue. See original summary.

joelpittet’s picture

StatusFileSize
new3.24 KB

Here is that patch only.

joelpittet’s picture

Issue summary: View changes
StatusFileSize
new487.78 KB
star-szr’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -rc target +rc target triage

Yup.

alexpott’s picture

Issue tags: -rc target triage

Discussed 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.

joelpittet’s picture

@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.

star-szr’s picture

Actually 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.

star-szr’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new4.3 KB
new1.06 KB

Splitting #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.

Status: Needs review » Needs work

The last submitted patch, 8: move_twig_without_to-2591515-8.patch, failed testing.

star-szr’s picture

Status: Needs work » Needs review
StatusFileSize
new3.96 KB
new1.39 KB

Just an idea to make it a bit less disruptive. Will look at that failing test.

star-szr’s picture

StatusFileSize
new4.71 KB
new777 bytes

Not 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.

The last submitted patch, 10: move_twig_without_to-2591515-9.patch, failed testing.

star-szr’s picture

joelpittet’s picture

Thanks for trying to make this less disruptive @Cottser!

  1. +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
    @@ -27,6 +27,8 @@ class TwigEnvironment extends \Twig_Environment {
    +  protected $templateClassPrefix = '__TwigTemplate_';
    +
    

    This looks like it snuck into the patch.

  2. +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
    @@ -43,10 +45,6 @@ class TwigEnvironment extends \Twig_Environment {
       public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, \Twig_LoaderInterface $loader = NULL, $options = array()) {
    -    // Ensure that twig.engine is loaded, given that it is needed to render a
    -    // template because functions like TwigExtension::escapeFilter() are called.
    -    require_once $root . '/core/themes/engines/twig/twig.engine';
    

    Love that this gets removed!

  3. +++ b/core/modules/system/src/Tests/Theme/TwigWhiteListTest.php
    @@ -123,6 +123,10 @@ public function testWhiteListChaining() {
    +    // Include the Twig engine so we can call twig_render_template().
    +    require_once \Drupal::root() . '/core/themes/engines/twig/twig.engine';
    

    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.

joelpittet’s picture

Status: Needs review » Needs work
star-szr’s picture

#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?

joelpittet’s picture

@Cottser Yeah that doesn't read right #14.3

twig_render_template() maybe go with the rest of the fire too?

star-szr’s picture

I 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.

xjm’s picture

Issue tags: +rc target triage
star-szr’s picture

Status: Needs work » Needs review

I think the feedback from #14 was addressed without changing any code, if I'm wrong please let me know :)

Status: Needs review » Needs work

The last submitted patch, 11: move_twig_without_to-2591515-11.patch, failed testing.

The last submitted patch, 10: move_twig_without_to-2591515-9.patch, failed testing.

The last submitted patch, 8: move_twig_without_to-2591515-8.patch, failed testing.

The last submitted patch, 2: move_twig_without_to-2591515-2.patch, failed testing.

joelpittet’s picture

  1. +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
    @@ -27,6 +27,8 @@ class TwigEnvironment extends \Twig_Environment {
    +  protected $templateClassPrefix = '__TwigTemplate_';
    +
    

    If this didn't sneak in and was intentional, it at the very least needs a docblock saying what it's for.

  2. +++ b/core/modules/system/src/Tests/Theme/TwigWhiteListTest.php
    @@ -123,6 +123,10 @@ public function testWhiteListChaining() {
    +    // Include the Twig engine so we can call twig_render_template().
    +    require_once \Drupal::root() . '/core/themes/engines/twig/twig.engine';
    

    Really wish we didn't need to add this.

  3. +++ b/core/themes/engines/twig/twig.engine
    @@ -133,20 +133,13 @@ function twig_render_template($template_file, array $variables) {
     function twig_without($element) {
    

    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.

alexpott’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs work » Postponed

Discussed 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.

star-szr’s picture

At 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.

xjm’s picture

Title: Move twig_without() to the TwigExtension where all the other filters are. » Copt twig_without() to the TwigExtension where all the other filters are, and deprecate

@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.

star-szr’s picture

Yep, that's totally fine with me :) thanks!

joelpittet’s picture

Title: Copt twig_without() to the TwigExtension where all the other filters are, and deprecate » Move twig_without() to the TwigExtension where all the other filters are and deprecate
joelpittet’s picture

Changed 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

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

xjm’s picture

Issue tags: -rc target triage
joelpittet’s picture

Status: Postponed » Needs work

Unpostponing so we can tidy the feedback up in #25 and keep on trucking

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new3.73 KB

Rerolled and removed the lines I mentioned in comment #25.1 and #25.2.

joelpittet’s picture

Priority: Major » Normal

This is a normal issue.

Status: Needs review » Needs work

The last submitted patch, 38: 2591515-38.patch, failed testing. View results

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new1.26 KB
new4.43 KB

Ah, I see why the include was made, put it back. Also changed \Drupal::root() to $this->root and rooted the namespace for \ArrayAccess since we are in a namespace now.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

markhalliwell’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
kostyashupenko’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new4.39 KB
joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

The 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.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record
  1. +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
    @@ -58,10 +58,6 @@ class TwigEnvironment extends \Twig_Environment {
    -    // Ensure that twig.engine is loaded, given that it is needed to render a
    -    // template because functions like TwigExtension::escapeFilter() are called.
    -    require_once $root . '/core/themes/engines/twig/twig.engine';
    
    +++ b/core/tests/Drupal/KernelTests/Core/Theme/TwigWhiteListTest.php
    @@ -113,6 +113,9 @@ protected function setUp() {
       public function testWhiteListChaining() {
    +    // Include the Twig engine so we can call twig_render_template().
    +    require_once $this->root . '/core/themes/engines/twig/twig.engine';
    

    I 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.

  2. +++ b/core/themes/engines/twig/twig.engine
    @@ -133,20 +133,13 @@ function twig_render_template($template_file, array $variables) {
    + * @deprecated in Drupal 8.5.x and will be removed before 9.0.0. Use
    + *   \Drupal\Core\Template\TwigExtension::withoutFilter() instead.
    

    We only deprecate stuff in the next minor release - so 8.6.x.

  3. +++ b/core/themes/engines/twig/twig.engine
    @@ -133,20 +133,13 @@ function twig_render_template($template_file, array $variables) {
    +  /** @var \Drupal\Core\Template\TwigExtension $extension */
    +  $extension = \Drupal::service('twig.extension');
    +
    +  return call_user_func_array([$extension, 'withoutFilter'], func_get_args());
    

    This needs to trigger a silenced error - see https://www.drupal.org/core/deprecation - we also need a change record.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nickdickinsonwilde’s picture

Assigned: Unassigned » nickdickinsonwilde
nickdickinsonwilde’s picture

Assigned: nickdickinsonwilde » Unassigned
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs change record
StatusFileSize
new3.11 KB

Draft 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

lauriii’s picture

StatusFileSize
new3.81 KB
new702 bytes
joelpittet’s picture

Status: Needs review » Needs work

Small change but otherwise RTBC.

+++ b/core/themes/engines/twig/twig.engine
@@ -133,20 +133,14 @@ function twig_render_template($template_file, array $variables) {
+  @trigger_error('twig_without() is deprecated in Drupal 8.7.x and will be removed before Drupal 9.0.0. Use \Drupal\Core\Template\TwigExtension::without(). See https://www.drupal.org/node/3011154.', E_USER_DEPRECATED);

This error needs to be updated to the new method name withoutFilter().

lauriii’s picture

Status: Needs work » Needs review
StatusFileSize
new3.82 KB
new967 bytes

Thanks for the review @joelpittet!

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, back to RTBC, pending tests

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/themes/engines/twig/twig.engine
@@ -133,20 +133,14 @@ function twig_render_template($template_file, array $variables) {
+  @trigger_error('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.', E_USER_DEPRECATED);
+  /** @var \Drupal\Core\Template\TwigExtension $extension */
+  $extension = \Drupal::service('twig.extension');
+
+  return call_user_func_array([$extension, 'withoutFilter'], func_get_args());

Given 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.

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new1.71 KB
new5.53 KB

@alexpott, Feels like this kind of test doesn't have a great ROI...

alexpott’s picture

@joelpittet can you add the @expectedDeprecation annotation to the legacy test. That way we are testing that the code changes don't work and that the expected deprecation message is triggered.

joelpittet’s picture

StatusFileSize
new5.65 KB

Added

The twig_without function is deprecated since version 8.7.x and will be removed in 9.0.0.

Based on some others I found in core that looked potentially similar, LMK if that works?

alexpott’s picture

Status: Needs review » Needs work
+++ b/core/modules/system/tests/src/Kernel/Theme/TwigFilterTest.php
@@ -120,4 +121,39 @@ public function testTwigWithoutFilter() {
+   * @expectedDeprecation The twig_without function is deprecated since version 8.7.x and will be removed in 9.0.0.

This 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 testLegacyTwigWithoutFunction

joelpittet’s picture

@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.

joelpittet’s picture

Status: Needs work » Needs review
StatusFileSize
new5.75 KB

I usually run them if I have time, didn't know that @expectedDeprecation was 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.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

This addresses feedback from @alexpott! Thank you @joelpittet!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 9dfb602 and pushed to 8.7.x. Thanks!

  • alexpott committed 9dfb602 on 8.7.x
    Issue #2591515 by joelpittet, Cottser, lauriii, NickWilde,...
joelpittet’s picture

🧯💨🔥

andypost’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.