I have a site with Drupal 8 and I want to customize the templates of my pages.

I use the Profile module.

How to display the rendered entity of the "compact" display of the user account in the profile ?

This is the template to customize the profile page.

 * @file
 * Default theme implementation for profiles.
 * Available variables:
 * - content: Items for the content of the profile.
 *   Use 'content' to print them all, or print a subset such as
 *   'content.title'. Use the following code to exclude the
 *   printing of a given child element:
 *   @code
 *   {{ content|without('title') }}
 *   @endcode
 * - attributes: HTML attributes for the wrapper.
 * - profile: The profile object.
 * - url: The profile URL, if available.
 * @ingroup themeable
<div{{ attributes.addClass('profile--personnel--teaser') }}>

  <div class="views-row">





<div{{ attributes.addClass('profile--personnel--teaser') }}>

  <div class="row">

    <div class="bs-field-type">
      {{ profile.type.entity.label }}

    <div class="bs-field-image">
      {{ drupal_entity('user', profile.uid.target_id, 'compact') }}

    <h3 class="bs-field-title">
      {{ profile.getOwner().name.value }}

    <div class="bs-field-status">

    <div class="bs-field-sex">
      {{ content.field_personnel_sexe }}

    <div class="bs-field-age">
      {{ content.field_personnel_naissance }}

    <div class="bs-field-address">
      {{ content.field_personnel_adresse }}

    <div class="bs-field-contact">
      <a class="btn btn-success" href="/messenger#new">
        <i class="fas fa-comment fa-lg"></i> Envoyer un message

    <div class="bs-field-more">
      Voir son profil

    <div class="bs-field-link">
      <a href="{{ url }}" title="{{ content.title.value }}"></a>



2 Answers 2


If you want to render a view mode you will need to wirte a MY_MODULE_preprocess_HOOK() and pass it to the TWIG


you can install the Twig tweak module and then you can render by specifying the view mode in the TWIG page itself

{{ drupal_entity('user', profile.uid.target_id, 'compact') }}
  • Thank you it works, but you need permission to see the user account. Is there a way to display the rendered entity without this permission?
    – user84727
    Commented Apr 16, 2018 at 11:31
  • To do that you need to set permissions for anonymous user to View profile entities and View user information in ~/admin/people/permissions Commented Apr 16, 2018 at 11:51
  • I have a last question. In the comment twig, I want to display the rendered entity of the profile (type personal_profile and display teaser). I tried this {{ drupal_entity('profile.profil_personnel', comment.uid.target_id, 'teaser') }} but it does not work. The goal is to display the author's profile, not are user account.
    – user84727
    Commented Apr 16, 2018 at 13:20
  • Try {{ drupal_entity('user', comment.uid.target_id, 'teaser') }} Commented Apr 16, 2018 at 13:27
  • Your code works, but it displays the user's teaser view. That's not what I want. I want to display the teaser view of the author's profile. (Profile module)
    – user84727
    Commented Apr 16, 2018 at 14:05

If I understand your question correctly, you want to change the view mode when a user's profile page is displayed. I recommend to put this kind of logic in a module, not in your theme. One way to do this, is by implementing the hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context):

function YOUR_MODULE_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
  if ($entity->getEntityTypeId() == 'user') {
    $view_mode = 'compact';
  • Not sure what the op was looking for, but this is what I needed. Thanks! Commented Apr 16, 2020 at 20:55

Your Answer

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