3

I can set up a view mode for the user profile, but I don't know how to create a template targeting it. user-profile.tpl.php is the template used, but I can't figure out how to make a view-mode targeted version of it.

Any suggestion?

  • 1
    What does devel_themer tell you? drupal.org/project/devel_themer Also, is there any reason you can't use display suite to achieve what you're trying to do with view modes instead of .tpl.php files? – David Meister Jul 29 '12 at 9:00
3

You already figured out how to create an additional view mode for the user entity:

/**
 * Implements hook entity info alter
 * Adds additional view mode for user entity
 */
function MYCUSTOMMODULE_entity_info_alter(&$entity_info) {
  $entity_info['user']['view modes']['teaser'] = array(
      'label' => t('Teaser'),
      'custom settings' => TRUE,
  );
}

Now you can define theme suggestions for that view mode using HOOK_preprocess_user_profile:

/**
* Implements HOOK_preprocess_user_profile()
* Adds theme suggestions for the user view mode teaser
*/
function MYCUSTOMMODULE_preprocess_user_profile(&$vars) {
  if ($vars['elements']['#view_mode'] == 'teaser') {
    $vars['theme_hook_suggestions'][] = 'user_profile__teaser';
  }

After clearing the caches Drupal will search in the theme folder for a file called user-profile--teaser.tpl.php, which could look like this example*:

<section class="user-teaser">
  <?php print render($user_profile['field_last_name'][0]); ?>
  <div class="user-image">
    <figure><?php print render($user_profile['field_user_picture'][0]); ?></figure>
  </div>
  <div class="user-body">
    <?php print render($user_profile['field_biography']); ?>
    <?php print render($user_profile['field_birthdate']); ?>
    <?php print render($user_profile['field_nationality']); ?>
  </div>
</section>

(* actual code will depend on the fields you defined for the user profile)

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.