2

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">

      {{ ??? RENDERED ENTITY USER COMPACT ??? }}

  </div>

</div>

UPDATE

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

  <div class="row">

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

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

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

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

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

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

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

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

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

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

  </div>

</div>
0

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

OR

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 Apr 16 '18 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 – Razeem Ahmad Apr 16 '18 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 Apr 16 '18 at 13:20
  • Try {{ drupal_entity('user', comment.uid.target_id, 'teaser') }} – Razeem Ahmad Apr 16 '18 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 Apr 16 '18 at 14:05
0

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';
  }
}

Your Answer

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