Problem/Motivation

Originally, this issue was about deprecating file and include keys in hook_theme. That turned out to be hard. But for a long time, the last thing they were used for were putting template preprocess functions in conditionally loaded include files.

Since template_preprocess files are now fully converted and deprecated, we can formally deprecate them and file/include keys together.

Steps to reproduce

Proposed resolution

This includes:

* 3 deprecations for those 3 things: file, includes and automatic discovery of template_preprocess functions
* A special case to not trigger the deprecation for fully converted theme hook definitions, so that we do not need to convert those two
* That doesn't apply to the file deprecation, so they are moved into an always loaded file
* A deprecation test, I picked a test module that already exists, but turns out it's quite a mess because the defined functions are bogus (invalid arguments, return values, function names, ..) so I ended up adding new ones anyway.
* Convert missed leftovers.

Remaining tasks

Confirm decision to not deprecate these keys when the function is already deprecated

User interface changes

N/A

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

Original issue summary

_theme_process_registry() includes any include file (could be a template or foo.admin.inc) when building the registry. It looks like including the file isn't necessary.

I traced the include back to #168028: both theme functions and templates may need include files, when that patch went in hook_theme() and especially include file/template handling was very different, and as far as I can tell it's no longer needed. Originally the include had a comment 'just to be safe', this was lost in the past three years.

This saves at least 40ms off a registry rebuild, but more importantly 5mb of memory, with just core modules enabled. Sites with lots of modules, lots of includes and lots of templates are going to see considerably bigger savings.

As well as this, all those included files, many of which are never going to be used if they're templates, will pollute opcode caches - potentially causing evictions etc. - so this should help on cold starts a fair bit.

Theme tests pass but didn't run the whole suite.

If I run drush cc all then hit the front page without apc enabled, that requires 45mb of memory with all the caches being rebuilt.

I confirmed that the total memory usage with this patch dropped to 41mb. So if these includes really aren't needed, that may end up helping with issues like #1008362: 32M is sometimes not enough memory to install Drupal 7, module page submit - anywhere else where a single request can end up building all global caches.

If this doesn't break things horribly, it may win the award for most kilobytes of memory saved per line of patch ;)


Issue fork drupal-1177762

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

catch’s picture

This may not be possible due to process functions - at least not if we're relying on function_exists() to see whether they exist or not in include files, see what the bot says though.

Status: Needs review » Needs work

The last submitted patch, files.patch, failed testing.

catch’s picture

Status: Needs work » Closed (works as designed)

Shame.

catch’s picture

Status: Closed (works as designed) » Active

This patch won't work, but we could find out if the functions exist another way, such as using the registry regexp.

joelpittet’s picture

Issue summary: View changes
joelpittet’s picture

Status: Active » Needs review
StatusFileSize
new605 bytes

@catch this may still valid in 8.1.x but not 8.0.x. Maybe you have another way of dealing but I'm reposting the patch for our current 8.0.x

joelpittet’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs review » Active

Bumping

Status: Active » Needs work

The last submitted patch, 6: theme_registry_rebuild-1177762-6.patch, failed testing.

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.

catch’s picture

Title: Theme registry rebuild needlessly loads dozens of include files » Remove 'includes' and possibly 'file' support from hook_theme()
Category: Bug report » Task
Issue tags: +Bug Smash Initiative

This code still exists, but 'includes' isn't documented in hook_theme() at all: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...

And I'm not convinced we need to support 'file' any more now that actual theme implementations are in templates.

Soo.. maybe we can deprecate both keys for removal in 10.x? Or at least 'includes' if file is still used somehow.

catch’s picture

Status: Needs work » Needs review
StatusFileSize
new675 bytes
new783 bytes

See if any of this is dead code first..

catch’s picture

StatusFileSize
new875 bytes
new826 bytes
andypost’s picture

3k failed tests looks promising!

longwave’s picture

StatusFileSize
new1.35 KB

I don't think we can get rid of 'file' easily as there are plenty of usages in core modules but there only seems to be one use of 'includes' that we can refactor away.

longwave’s picture

StatusFileSize
new1.29 KB
longwave’s picture

Turns out Views uses it as well, but we might be able to remove that too.

catch’s picture

OK to do 'files' we'll probably need #1804614: [meta] Consolidate similar twig templates and properly use theme suggestions in core and various related issues done first, but good spot on the one remaining usage of 'includes', looks like we just need to move the preprocess out of that file, and possibly a follow-up to refactor the remaining function in it.

catch’s picture

Title: Remove 'includes' and possibly 'file' support from hook_theme() » Remove 'includes' support from hook_theme()
longwave’s picture

Status: Needs review » Needs work

1291 failures, I think this is all because of

      // For the views module we ensure views.theme.inc is included.
      if ($def['provider'] == 'views') {
        if (!isset($hooks[$def['theme']]['includes'])) {
          $hooks[$def['theme']]['includes'] = [];
        }
        if (!in_array('views.theme.inc', $hooks[$def['theme']]['includes'])) {
          $hooks[$def['theme']]['includes'][] = $module_dir . '/views.theme.inc';
        }
      }
longwave’s picture

Status: Needs work » Needs review
StatusFileSize
new1.99 KB

Nope, there is a bigger issue: 'file' sets up 'includes' for later cached calls:

        if (isset($info['file'])) {
          $include_file = isset($info['path']) ? $info['path'] : $path;
          $include_file .= '/' . $info['file'];
          include_once $this->root . '/' . $include_file;
          $result[$hook]['includes'][] = $include_file;
        }

We can still deprecate 'includes' from being declared in the first place though.

Status: Needs review » Needs work

The last submitted patch, 30: 1177762-30.patch, failed testing. View results

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.

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.

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.

berdir’s picture

Status: Needs work » Postponed

#3495943: Handle module preprocess functions as OOP hooks should make this pretty straight-forward. Once we converted the remaining preprocess functions in .inc files in core like system and views, we should be able to trigger deprecations if this is set.

nicxvan’s picture

ThemeCommonElements uses it too, but that should be able to be cleaned up too.

berdir’s picture

Title: Remove 'includes' support from hook_theme() » Deprecate 'includes' and 'file' support from hook_theme()

Still postponed, but wanted to have a try, all we have to do now is trigger a deprecation if one of the two keys is set, started a MR based on #3504381: [meta] Convert Template Preprocess hooks to OOP equivalent which removes those in system module and a quickfix for authorize report that I might move to #3518822: Convert template preprocess hooks in core/includes/theme.inc, there shouldn't be too much left after that.

larowlan’s picture

Status: Postponed » Active

Blocker has been committed

berdir’s picture

Status: Active » Postponed

Still postponed on at 50% of #3504381: [meta] Convert Template Preprocess hooks to OOP equivalent, template_preprocess is actually the main thing here, module preprocess aren't really in those files. I'm aggregating the relevant ones from the meta to check what's left, but keeping at postponed.

berdir’s picture

This is still postponed and contains 4 or so other issues and a bunch of manual fixes, but with all that, it is now green. We might need to add a BC test though for this. could be combined with a BC test for template_preprocess.

berdir’s picture

Title: Deprecate 'includes' and 'file' support from hook_theme() » Deprecate automatic template_preprocess discovery and 'includes'/'file' support from hook_theme()

This postponed on one last issue: #3346394: Replace BlockContentController::add with EntityController::addPage via the AdminHtmlRouteProvider route provider.

The deprecation for template_preprocess is in place and is correctly picking up some bits we missed. There's one in field_ui that looks converted because it's already a one-liner calling the service, but it's a redirection to another preprocess, this can now actually directly use that.

And one in views was missed too, that seems completely untested as the old function had been deprecated.

berdir’s picture

Issue summary: View changes
Status: Postponed » Needs review

This months theme is finishing 2011 issues.

This can finally be reviewed.

Updated the issue summary

berdir’s picture

Issue summary: View changes
berdir’s picture

Issue summary: View changes
berdir’s picture

Issue summary: View changes
nicxvan’s picture

I think this is almost ready, just one question on testing the last deprecation path.

berdir’s picture

I can add includes, would require another hook definition and include file. The only argument to not do it is the fact that we didn't have any actual test coverage in core for it. It wasn't used at all, tests or otherwise. Directly at least, having a file key is just a shorthand for having an include.

nicxvan’s picture

Fair enough, I'll review again without it.

nicxvan’s picture

Status: Needs review » Reviewed & tested by the community

I think this is almost ready then, one last question.

I see you are only deprecating if it's not already deprecated. At first I thought this was a nice little optimization, but can we end up in a bit of a bind.

E.g. we are removing this support in 12 someone could deprecate their template preprocess and try adding support for 12 and this would not notify them.

Without that check it's a double deprecation, but that might be preferable unless I'm missing something.

Other than that.
Took another look, all deprecations line up.

We usually don't test deprecation pathways but this is unique enough it is warranted.

Everything else is just a couple that were still in the include file or were using the deprecated fallback.

50 convinced me we don't need a test for the third pathway.

nicxvan’s picture

Status: Reviewed & tested by the community » Needs review

Whoops, didn't mean too rtbc before that question was addressed.

berdir’s picture

The intention of the deprecated check isn't to avoid double deprecation, I don't really care about that and they have a different target audience (one is for the code defining the theme hook and one is for those using it).

The reason is just to avoid having to convert those already deprecated template preprocess functions into OOP, so we end up having a deprecated functional BC function calling into an also deprecated OOP method.

It's IMHO unlikely to effect anything else or cause problems with different versions, even if we deprecate more template preprocess functions, they're already converted now in core.

> The only argument to not do it is the fact that we didn't have any actual test coverage in core for it. It wasn't used at all, tests or otherwise

This is wrong, it was used in the maintenance page template, I'm not sure if we have deprecation test coverage for that though. I'm open to adding a deprecation test example for that

> We usually don't test deprecation pathways but this is unique enough it is warranted.

I would actually invert it. We test deprecated pathways *unless* they are trivial. It just so happens that many of them are in fact trivial.

The test coverage is primarily about making sure that the code, while deprecated, still works, because it's still heavily used in contrib (template_preprocess functions). A kernel test can only partially guarantee that because the includes are not only for discovery but also rendering the templates, which we could only test with a functional test and doing that on a different request.

Either way, if you or a committer thinks it would be valuable to extend the coverage then I'm happy to do so.

nicxvan’s picture

Status: Needs review » Needs work

Had a brief chat in slack:

We're going to add test coverage for include, but leave the deprecation exclusion for already deprecated functions.

berdir’s picture

Status: Needs work » Needs review

Done.

nicxvan’s picture

Issue summary: View changes
Status: Needs review » Active

I took a pass at updating the CR and IS

nicxvan’s picture

Status: Active » Reviewed & tested by the community

Cr could use another review but I think this is ready now.

  • catch committed cfcb78ce on 11.x
    Issue #1177762 by catch, longwave, berdir, nicxvan: Deprecate automatic...
catch’s picture

Status: Reviewed & tested by the community » Fixed

This looks great. Agreed with the double deprecation check it will save a lot of churn in things that are going to be removed anyway.

Committed/pushed to 11.x, thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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