4

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.

googletorp
  • 33,075
  • 15
  • 67
  • 82
Peter Carrero
  • 1,596
  • 2
  • 13
  • 13

3 Answers3

3

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, 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().

apaderno
  • 28,547
  • 16
  • 75
  • 90
2

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();.

Richard M
  • 14,244
  • 6
  • 52
  • 48
  • 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_page and by the time it gets to my function, the scripts index is filling... that's what is puzzling... :( – Peter Carrero Dec 23 '09 at 21:29
  • 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 Dec 23 '09 at 21:43
  • 1
    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 Dec 23 '09 at 21:54
  • i also have the same problem. &vars does not work. it works on local fine but has problem on server. i think the problem is related to php installed on the server. – Alexar May 24 '10 at 09:44
  • 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 May 27 '10 at 11:14
0

I know this is an old question but I just struck it and I think I know the answer.

I think jquery_update is causing this.

jquery_update implements hook_theme_registry_alter which changes $theme_registry so that jquery_update_preprocess_page runs last. This is despite what Peter sees in $theme_registry because the alter happens after he looks at it.

jquery_update gets $scripts from drupal_add_js(), fiddles with the array and then resets $variables['scripts'] which overwrites any changes made earlier.

I'm not sure what the perfect solution is. I don't think we're really supposed to mess with the scripts string directly. I have a special one page case so I'm probably going to do the somewhat bad thing of calling my code from jquery_update_preprocess_page. jquery_update for Drupal 6 is unlikely to updated now. That seems better than getting into a dueling battle of who comes last.

tetranz
  • 1,932
  • 3
  • 24
  • 32