0

I'm trying to pass a variable to an user template, here is my code:

function template-name_preprocess_user(&$variables) {
  $variables['foo'] = 'foo';
}

It doesn't work. I tried adding these line in the core User module preprocess and it works!

1
  • What do you mean by template-name in template-name_preprocess_user()? Drupal doesn't use a name like that for template hooks. It uses template_preprocess_user(), THEMENAME_preprocess_user(), or MODULENAME_preprocess_user().
    – apaderno
    Nov 23, 2016 at 8:05

1 Answer 1

5

Template hooks like template_preprocess_user() are implemented by custom module/theme by replacing template with [MODULE_NAME/THEME_NAME]. So, for example, if your theme short name is bartik, then the function name will be bartik_preprocess_user() which will be implemented in the bartik.theme file.

function [MODULE/THEME_NAME]_preprocess_user(&$variables) {
  $variables['foo'] = 'foo';
}
1
  • 1
    Yeah, either the hook is misnamed or caches weren't cleared.
    – Berdir
    Nov 23, 2016 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.