I've tried to create theme hook suggestions in my template.php to use template files in combination with view modes. My code snippet:

function blogtheme_preprocess_user_profile(&$vars) {
 if ($vars['elements']['#view_mode'] == 'blog_user_preview') {
  $vars['theme_hook_suggestions'][] = 'user__blog_user_preview';
 if ($vars['elements']['#view_mode'] == 'blog_user_full') {
  $vars['theme_hook_suggestions'][] = 'user__blog_user_full';

But it doesn't work – he standard user-profile.tpl.php is always being used for these two view modes. I activated the zen theme to test the function and it worked smoothly. After reading the comments in the bootstrap base theme I've found the following comment in the registry.inc:

// Due to how theme() functions, if a base hook implements // preprocess or process functions, then the base hook info is // used to invoke the necessary phase functions instead of the // suggestion hook info. To get around this, a helper function // must be appended to the base hook info so it can call the // theme suggestion implementation's phase function.

$function = '_bootstrap_' . $phase . '_theme_suggestion';
 if (!in_array($function, $registry[$base_hook][$phase_key])) {
 $registry[$base_hook][$phase_key][] = $function;

Could somebody help me by explaining this function? How can I get it to work?

  • Why are you using view modes AND templates? The view modes will create the templates, and using something like Display Suite will make your life a whole lot easier. – kyletaylored Aug 7 '14 at 21:04
  • I'm using Display Suite to create view modes and then I'm using theme hook suggestions to combine the template and the created view mode. – seadil Aug 7 '14 at 21:37
  • I think what I'm trying to get at is: what are you doing in the template that you can't do in DS? – kyletaylored Aug 7 '14 at 23:13
  • Setting the theme hook suggestion to pinpoint a view mode to a specific template. – seadil Aug 8 '14 at 10:13
  • No, I mean on a much higher level. WHAT in the template is so important or specialized that you NEED to use a template instead of just using the view mode? – kyletaylored Aug 8 '14 at 19:42

The code in your first snippet looks correct to me. I would try the following:

  1. Remove the if statements temporarily and get rid of the second option. It's possible that neither of them are returning true.
  2. Theme functions are cached. Make sure you have cleared the cache before testing.
  3. Double check that your custom template is named user--blog_user_preview.tpl.php.
  • Removing the if statements worked. Debugged even $vars with dpm and it has been stated, that the current used view mode is user__blog_user_preview but it still didn't work (user-profile.tpl.php is being used). – seadil Aug 7 '14 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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