Problem/Motivation

For some time we've been talking about moving away from preprocess - via Twig autoescape and other things. This didn't really happen to the extent people hoped during the 8.x cycle though.

So at this point we have a lot of preprocess implementations, and a lot of templates relying on variables prepared by them.

We can't remove preprocess in 8.x, and we can't make variables that were previously available to templates unavailable.

However, we can potentially rationalize specific preprocess implementations, deprecate the use of particular variables etc.

template_preprocess() gets called for every single template render, to generate exactly the same variables to pass in - this would be a good candidate to remove.

Additionally we may be able to define twig variables as objects for other things, so that they're used on demand in the template, rather than always generating strings in preprocess as we currently do. Something like node $submitted would be a good candidate for that.

Proposed resolution

Replace the variables added by template_preprocess() with Twig global variables: http://symfony.com/doc/current/cookbook/templating/global_variables.html

Remaining tasks

User interface changes

API changes

Data model changes

Comments

catch created an issue. See original summary.

catch’s picture

Issue tags: +Twig, +Performance
Wim Leers’s picture

Issue tags: +needs profiling

+1 for the principle, but not yet sure how we'll gain much:

  1. Not sure yet whether using Twig's "global variables" feature will gain us performance
  2. Not sure yet whether it's all that valuable to get rid of just one preprocess function

Definitely an interesting idea that's worth exploring :)

star-szr’s picture

Title: Replace template_process() and possibly other things with Twig global variables » Replace template_preprocess() and possibly other things with Twig global variables
Related issues: +#2611246: [PP-1] Document the recommended method of creating file URLs to theme assets (e.g. images)

Title fix and adding a related issues that talked about using Twig global vars.

dawehner’s picture

Well for 8.1.x it could be tricky, given that other preprocess functions might rely on the existence of those variables, like 'attributes'.

catch’s picture

Attributes will be hard to change, but I think something like $is_front is unlikely to be changed in preprocess, and something like node $submitted we might be able to consider the specific variables passed to a preprocess function @internal.

Another way to do it would be to have Stable provide the preprocessed variables since that's the bc layer for themes.

So there might be a way to get around it.

joelpittet’s picture

Regarding attributes yes it's tricky, but not impossible. I've got the content_attributes and title_attributes mostly sorted here #2108771: Remove special cased title_attributes and content_attributes for Attribute creation

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.

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.

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.

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.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Title: Replace template_preprocess() and possibly other things with Twig global variables » Replace template_preprocess() and possibly other things with Twig global variables/functions
catch’s picture

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

catch’s picture