diff --git a/core/lib/Drupal/Core/Theme/Registry.php b/core/lib/Drupal/Core/Theme/Registry.php index ab64409..38ab0b7 100644 --- a/core/lib/Drupal/Core/Theme/Registry.php +++ b/core/lib/Drupal/Core/Theme/Registry.php @@ -340,9 +340,10 @@ protected function build() { $this->processExtension($cache, $this->theme->getEngine(), 'theme_engine', $this->theme->getName(), $this->theme->getPath()); } - // Finally, hooks provided by the theme itself. + // Hooks provided by the theme itself. $this->processExtension($cache, $this->theme->getName(), 'theme', $this->theme->getName(), $this->theme->getPath()); + // Discover and add all preprocess functions for theme hook suggestions. $this->postProcessExtension($cache, $this->theme); // Let modules and themes alter the registry. @@ -562,14 +563,16 @@ protected function processExtension(array &$cache, $name, $type, $theme, $path) } /** - * This completes the theme registry adding missing functions and hooks. + * This completes the theme registry adding discovered functions and hooks. * * @param array $cache - * @see ::processExtension() - * @param object $theme + * The theme registry. + * @param \Drupal\Core\Theme\ActiveTheme $theme * Current active theme. + * + * @see ::processExtension() */ - protected function postProcessExtension(array &$cache, $theme) { + protected function postProcessExtension(array &$cache, ActiveTheme $theme) { $grouped_functions = drupal_group_functions_by_prefix(); // Gather prefixes. This will be used to limit the found functions to the @@ -594,24 +597,24 @@ protected function postProcessExtension(array &$cache, $theme) { if (!isset($grouped_functions[$first_prefix])) { continue; } - + // Add the function and the name of the associated theme hook to the list + // of processors if a matching base hook is found. foreach ($grouped_functions[$first_prefix] as $candidate) { - if (preg_match("/^{$prefix}_preprocess_(.*)(__)?/", $candidate, $matches)) { - if (isset($cache[$matches[1]])) { - $processors[] = $candidate; + if (preg_match("/^{$prefix}_preprocess_(((?:[^_]++|_(?!_))+)__.*)/", $candidate, $matches)) { + if (isset($cache[$matches[2]])) { + $processors[$candidate] = $matches[1]; } } } } // Add missing variable processors. This is needed for hooks that do not - // explictly register the hook. For example, when a theme contains a + // explicitly register the hook. For example, when a theme contains a // variable process function but it does not implement a template, it will // go missing. This will add the expected function. It also allows modules // or themes to have a variable process function based on a pattern even if // the hook does not exist. - foreach ($processors as $processor) { - $hook = substr($processor, strpos($processor, 'preprocess_') + strlen('preprocess_')); + foreach ($processors as $processor => $hook) { if (isset($cache[$hook]['preprocess functions']) && !in_array($hook, $cache[$hook]['preprocess functions'])) { // Add missing processor to existing hook. $cache[$hook]['preprocess functions'][] = $processor;