0

How do I control which view mode of a user account appears on different pages?

I have a Compact user view mode with two fields I want to appear in forum posts and comments. I then have a more complete user view mode I would like to have appear on the users home page and eventually i would like a different view mode for article bylines.

I cannot, for the life of me though, figure out how to actually have any of these display.

I want to modify the layout of user.html.twig for the user home pages to have additional headings and information. But then it gets used wherever the user entity appears. Am I suppose to create specific twig templates for every user appearance or is there some more sensible way to control the fields that appear?

I tried accessing specific user entity fields and styling them in templates such as node--forum.html.twig but I can only seem to access the complete user entity with {{ author_picture }} - why is it named that if it is the entire entity? or the user name with {{ author_name }}

How do I do this?

1

The variables {{ author_picture }} and {{ author_name }} are prepared in preprocess in the hook template_preprocess_node(). You can modify these variables or add your own in a custom preprocess hook.

Or you can make the field uid configurable in UI by modifying the field definition in the node entity:

mymodule.module

function mymodule_entity_base_field_info_alter(&$fields, $entity_type) {
  if ($entity_type->id() == 'node') {
    if (isset($fields['uid'])) {
      $fields['uid']->setDisplayConfigurable('view', TRUE);
    }
  }
}

and you'll find the Authored by field in the Manage Display page, where you can configure how the referenced user entity is formatted (which view mode is used).

A third and IMHO perferable option is to use the module Display Suite.

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.