views:

332

answers:

2

i am having an issue where hook_preprocess_page 's changes to &$variables is not being rendered, even though it is the last item under $theme_registry['page']['preprocess functions']. logging contents of $variables to a file show the contents changed, but contents appear unchanged on the site. flushed all cache on drupal, flushed all browser caches and still the same result.

/**
 * Implementation of hook_preprocess_page().
 */
function grinchlist_preprocess_page(&$variables) {

  if (grinchlist_usercheck($variables['user']['uid'])) {
    $variables['scripts'] = preg_replace('/<script[^>]*christmas_snow.*<\/script>/','',$variables['scripts']);
  }
  file_put_contents('/tmp/vars.txt',print_r($variables,true));
}

the /tmp/vars.txt shows the variables properly, but the browser still show the script being loaded.

this may be a silly example, but i've had this issue with the hook_preprocess_page in other instances and it would really help out to understand what is going on here...

thanks.

A: 

I think you probably (assuming this works in the same way as CSS includes) need to call drupal_get_js at the end of your function, like so: $variables['scripts'] = drupal_get_js();.

I can't offer any help in understanding Drupal's preprocess hooks, as I've found them quite confusing myself.

Richard M
richard, thanks for the reply... that line is run by template_preprocess_page, which takes place before my code... looking at the $theme_registry['page']['preproces functions'], i get the following:[0] => template_preprocess[1] => template_preprocess_page[3] => jquery_update_preprocess_page[4] => oumoodle_preprocess_page[5] => phptemplate_preprocess_page[6] => content_profile_template_preprocess[7] => grinchlist_preprocess_pageand by the time it gets to my function, the scripts index is filling... that's what is puzzling... :(
Peter Carrero
I'm not sure if I follow you. My understanding is that you have to rerun the `drupal_get_js()` function after you make changes to the scripts variable. This blog post may be of help: http://www.drupaler.co.uk/blog/joys-preprocessing/70
Richard M
richard, that link is really valuable for the hook_preprocess_page in general (thanks for that, i hadn't found that before! :) ) but what i am trying to do is the opposite here, if i had added another javascript, i would have to reset the ['scripts'] index by evoking the drupal_get_js, what i am trying to do is remove a javascript if certain conditions are met. since the $javascript variable is static, i can't access it outside the function. i had the same issue when i went to d6.13, the query_update module stopped working (it also uses the preprocess_page hook).
Peter Carrero
takpar
Good point! What is the php version that works for you? And the one that doesn't? I will check mine and perhaps we can find a pattern. On another note, while testing an update to Lucid this week (which ships with php 5.3), I had major breakage of drupal as it doesn't support php 5.3 yet...
Peter Carrero
+1  A: 

The reported code contains an error. The IF-statement should be corrected from

if (grinchlist_usercheck($variables['user']['uid'])) {
}

to

if (grinchlist_usercheck($variables['user']->uid)) {
}

I am using hook_preprocess_page() in one of my modules (http://drupal.org/projects/nodewords), and the invoked function does change the content of the variables.

Then, as also Richard M reported, the function should get the list of the included JavaScript files from drupal_get_js().

kiamlaluno

related questions