Drupal hook_preprocess_page does not work as expected

I am having a problem where hook_preprocess_page changes to $ variables and is not displayed, although this is the last element in $ theme_registry ['page'] ['preprocess functions']. writing the contents of the $ variables to a file indicates a change in the content, but the content remains unchanged on the site. flushed the entire cache to drupal, flushed all browser caches and all 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)); } 

/tmp/vars.txt shows the correct variables, but the browser still shows the loadable script.

This might be a dumb example, but I had this problem with the hook_preprocess_page page in other cases, and it would really help to understand what is going on here ...

thanks.

+4
source share
3 answers

The specified code contains an error. IF statement should be fixed from

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

to

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

I use hook_preprocess_page() in one of my modules , and the function called changes the contents of the variables.

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

+3
source

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

+2
source

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

I think jquery_update is causing this.

jquery_update implements a hook_theme_registry_alter that modifies $ theme_registry so that jquery_update_preprocess_page works last. This is despite what Peter sees in $ theme_registry, because the change happens after he looks at it.

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

I'm not sure what the perfect solution is. I do not think that we really should directly contact the string of scripts. I have a special one-page case, so I'm probably going to make a somewhat bad call to my code with jquery_update_preprocess_page. jquery_update for Drupal 6 is unlikely to update. This seems better than getting into the battle for the last duel.

0
source

All Articles