Commit d9017f58 authored by webchick's avatar webchick

#591794 follow-up by effulgentsia: Reduce performance overhead of drupal_alter().

parent 099d72a9
......@@ -4700,27 +4700,37 @@ function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1)
* keyed array as described above.
*/
function drupal_alter($type, &$data, &$context1 = NULL, &$context2 = NULL) {
$hook = $type . '_alter';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$function($data, $context1, $context2);
}
// Allow the theme to alter variables after the theme system has been
// initialized.
global $theme, $base_theme_info;
if (isset($theme)) {
$theme_keys = array();
foreach ($base_theme_info as $base) {
$theme_keys[] = $base->name;
$functions = &drupal_static(__FUNCTION__, array());
// Some alter hooks are invoked many times per page request, so statically
// cache the list of functions to call, and on subsequent calls, iterate
// through them quickly.
if (!isset($functions[$type])) {
$functions[$type] = array();
$hook = $type . '_alter';
foreach (module_implements($hook) as $module) {
$functions[$type][] = $module . '_' . $hook;
}
$theme_keys[] = $theme;
foreach ($theme_keys as $theme_key) {
$function = $theme_key . '_' . $hook;
if (function_exists($function)) {
$function($data, $context1, $context2);
// Allow the theme to alter variables after the theme system has been
// initialized.
global $theme, $base_theme_info;
if (isset($theme)) {
$theme_keys = array();
foreach ($base_theme_info as $base) {
$theme_keys[] = $base->name;
}
$theme_keys[] = $theme;
foreach ($theme_keys as $theme_key) {
$function = $theme_key . '_' . $hook;
if (function_exists($function)) {
$functions[$type][] = $function;
}
}
}
}
foreach ($functions[$type] as $function) {
$function($data, $context1, $context2);
}
}
/**
......
......@@ -383,6 +383,7 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) {
$implementations = array();
cache_set('module_implements', array());
drupal_static_reset('module_hook_info');
drupal_static_reset('drupal_alter');
cache_clear_all('hook_info', 'cache');
return;
}
......
......@@ -85,6 +85,9 @@ function drupal_theme_initialize() {
$ancestor = $themes[$ancestor]->base_theme;
}
_drupal_theme_initialize($themes[$theme], array_reverse($base_theme));
// Themes can have alter functions, so reset the drupal_alter() cache.
drupal_static_reset('drupal_alter');
}
/**
......
......@@ -758,7 +758,8 @@ class NodeAccessRecordsUnitTest extends DrupalWebTestCase {
$web_user = $this->drupalCreateUser(array('access content'));
foreach ($operations as $op) {
$grants = node_test_node_grants($op, $web_user);
$altered_grants = drupal_alter($grants, $web_user, $op);
$altered_grants = $grants;
drupal_alter('node_grants', $altered_grants, $web_user, $op);
$this->assertNotEqual($grants, $altered_grants, t('Altered the %op grant for a user.', array('%op' => $op)));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment