Convert theme hooks (defined by hook_theme()) to be objects

Created on 24 March 2017, over 7 years ago
Updated 30 January 2023, almost 2 years ago

Problem/Motivation

There have been multiple issues lately involving the theme registry.
Because the theme hook arrays are passed around to many places, it is very difficult to track down which code is modifying which key.
It is also hard to full understand the ways that certain keys interact together.

This is very similar to the issues with the $form_state arrays in D7 and before.

Proposed resolution

Mimic the same solution used for $form_state: create a class to hold these theme hooks.
This time, we'll need full BC.

Remaining tasks

N/A

User interface changes

N/A

API changes

API addition and deprecation, no breaking changes
hook_theme() should now return an array of \Drupal\Core\Theme\ThemeHook objects
ThemeHook implements ArrayAccess for BC purposes.

Data model changes

N/A

📌 Task
Status

Needs work

Version

10.1

Component
Theme 

Last updated 1 day ago

Created by

🇺🇸United States tim.plunkett Philadelphia

Live updates comments and jobs are added and updated live.
  • Blocks-Layouts

    Blocks and Layouts Initiative. See the #2811175 Add layouts to Drupal issue.

  • Needs performance review

    It is used to alert the performance topic maintainer(s) that an issue significantly affects (or has the potential to affect) the performance of Drupal, and their signoff is needed. See the governance policy draft and Drupal Core gate - performance for more information.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇫🇷France andypost

    Starting with #129 the patch after re-roll became x2 smaller

  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • 🇮🇳India sourabhjain

    Trying to fix to custom command failed issue of #132

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Removing credit for #134, 131, 130, 129, 123, 122 as it is expected to check a patch before uploading. You can check for build errors make sure to run ./core/scripts/dev/commit-code-check.sh before uploading a patch to make sure there are no issues with code formatting. see https://www.drupal.org/docs/develop/development-tools/running-core-devel...

    Hiding those patches as well.

    This was previously tagged for issue summary update which still needs to happen.

    Also as @andypost pointed out the patch got cut in half. How come?

  • Assigned to tim.plunkett
  • 🇺🇸United States tim.plunkett Philadelphia

    The entire ThemeHook.php was dropped from the patches. Working on this.

  • 🇺🇸United States tim.plunkett Philadelphia

    Switched to an MR.
    This will probably break things, leaving assigned for now.

    Fixed a bunch of credit.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,356 pass, 8 fail
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    106 pass, 3,733 fail
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,379 pass
  • last update over 1 year ago
    29,444 pass
  • last update over 1 year ago
    29,812 pass
  • Status changed to Needs review over 1 year ago
  • 🇫🇮Finland lauriii Finland

    Tried to rebase the MR. Needs performance testing still.

  • 🇩🇪Germany donquixote

    Needs performance testing still.

    Just a quick note not for performance but memory/storage.

    I was suspecting that these objects significantly increase the size of the registry when serialized in cache, due to all the unused properties.
    But actually, uninitialized properties take zero space when serialized: https://3v4l.org/eoLeo

    Therefore the increase in cache space is much smaller than I previously expected, at least for plain core.
    For me, the string length in the `cache_default` table grows from ~83.000 to ~100.000 for theme_registry:olivero.

  • 🇫🇷France andypost

    It's getting bigger just because of namespaced class but as I see it has \0 code so sqlite truncating it

    /var/www/html/web $ drush sqlq "select * from cache_default where cid like 'theme_registry%';"
    theme_registry:build:modules|a:147:{s:26:"big_pipe_interface_preview";O:27:"Drupal\Core\Theme\ThemeHook":7:{s:7:"|-1|1690406986.891|1||0
    theme_registry:claro|a:177:{s:26:"big_pipe_interface_preview";O:27:"Drupal\Core\Theme\ThemeHook":7:{s:7:"|-1|1690406986.915|1||0
    theme_registry:olivero|a:181:{s:26:"big_pipe_interface_preview";O:27:"Drupal\Core\Theme\ThemeHook":7:{s:7:"|-1|1690406989.703|1||0
    

    my numbers

    /var/www/html/web $ drush sql:query "select cid, length(cast(data as blob)) from cache_default where cid like 'theme_registry%';"
    theme_registry:build:modules|74958
    theme_registry:claro|99383
    theme_registry:olivero|99111
    
    /var/www/html/web $ drush sql:query 'delete from cache_default;'
    
    ... removing patch
    
    /var/www/html/web $ drush sql:query "select cid, length(cast(data as blob)) from cache_default where cid like 'theme_registry%';"
    theme_registry:build:modules|61296
    theme_registry:claro|82337
    theme_registry:olivero|82029
    
  • Issue was unassigned.
  • 🇺🇸United States tim.plunkett Philadelphia
  • Status changed to Needs work over 1 year ago
  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • last update about 1 year ago
    Custom Commands Failed
  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Seems rebase had some errors.

  • 🇳🇴Norway steinmb

    This issue seems to have stalled out. Reading through and I get a feeling that it might be close to ready, but is in dire need of a manual rebase as #146 indicate. On the performance profiling, should not our Gander setup now be able to tell us if we have regressions based on changed to the registry size?

  • 🇳🇱Netherlands casey

    Just like alexpott in #63 I am not too keen on the name ThemeHook.

    What about ThemeableTemplate?

  • 🇫🇷France andypost

    Good point but it's really about implementation so TemplateImplementation or TemplateDefinition

  • Pipeline finished with Failed
    12 days ago
    Total: 154s
    #328489
  • Pipeline finished with Running
    12 days ago
    #328523
  • Pipeline finished with Failed
    12 days ago
    Total: 750s
    #328524
  • 🇫🇷France andypost

    Rebased and fixed CS/phpstan but lots of tests still fail

  • Pipeline finished with Failed
    12 days ago
    Total: 828s
    #328536
  • Pipeline finished with Failed
    12 days ago
    Total: 750s
    #328540
  • 🇺🇸United States nicxvan

    Is this issue only about replacing what hook theme returns?
    Or is this about how to define preprocess hooks and hooks implemented by themes?

    If this is the latter has anyone looked at 📌 OOP hooks using attributes and event dispatcher Needs review , a somewhat consistent way to implement these would be great, there could also be some confusion calling this ThemeHook since hook_theme will likely be placed in src/Hook/ThemeHook.php but return a different ThemeHook class.

    Though I'm not sure what you would call it, maybe it's ok to overload it a bit.

    There are some related thoughts on preprocess hooks here too 📌 [PP-1] Explore converting Install and Theme hook implementations to OOP Active in Followup 2. If this isn't the place let me know.

    Finally can someone add some before / after examples to the IS, it would make it easier to review the MR.

Production build 0.71.5 2024