Problem/Motivation

It should be caused by carelessness.

\Drupal\Core\Theme\Registry::postProcessExtension
the line 721:
if (isset($cache[$hook]['preprocess functions']) && !in_array($hook, $cache[$hook]['preprocess functions'])) {

It should be:
if (isset($cache[$hook]['preprocess functions']) && !in_array($preprocessor, $cache[$hook]['preprocess functions']))

The in_array will always fail and the preprocess hook is added to the cache, creating duplicates. Ultimately, this is not a problem because later in the method array_unique is used to remove duplicates.

      // Ensure uniqueness.
      if (isset($cache[$hook]['preprocess functions'])) {
        $cache[$hook]['preprocess functions'] = array_unique($cache[$hook]['preprocess functions']);
      }

Steps to reproduce

Read the code

Proposed resolution

Fix the needle in the in_array to $preprocess
if (isset($cache[$hook]['preprocess functions']) && !in_array($preprocessor, $cache[$hook]['preprocess functions']))

Remaining tasks

Patch
Review
Commit

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#8 2967627-8.patch840 bytesquietone

Comments

yunke created an issue. See original summary.

cilefen’s picture

@yunke If possible, would you please post patches? The issues will be fixed faster that way.

Version: 8.5.0 » 8.5.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

quietone’s picture

Title: [bug]the service "theme.registry" line 721 » Hook preprocess function always added
Version: 9.4.x-dev » 9.5.x-dev
Priority: Normal » Minor
Issue summary: View changes
Status: Active » Needs review
Issue tags: -theme.registry +Bug Smash Initiative
StatusFileSize
new840 bytes

Writing an automated test to prove there are no duplicates in the array isn't possible because an array_unique is run on the array later in the method. To have a test the method would have to be split into two which, to me, seems unnecessary for this fix.

To see what happens, I removed the array_unique and then \Drupal\KernelTests\Core\Theme\RegistryTest::testThemeTemplatesRegisteredByModules failed.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me. Clean and easy to read

pappis’s picture

I use module_preprocess_node__some_page to add a js library to that specific page with
$variables['#attached']['library'][] = 'module/js_library';
and sometimes the js code from the library gets loaded twice.
Maybe this is the reason ?

  • catch committed 356e9e1 on 10.0.x
    Issue #2967627 by quietone, yunke: Hook preprocess function always added...
  • catch committed 511e9fa on 10.1.x
    Issue #2967627 by quietone, yunke: Hook preprocess function always added
    
  • catch committed 53a64bc on 9.5.x
    Issue #2967627 by quietone, yunke: Hook preprocess function always added...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.1.x, cherry-picked to 10.0.x and 9.5.x, thanks!

Status: Fixed » Closed (fixed)

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