Author
Users in Drupal are another type of entity. This means we can theme them by using twig template suggestions. We'll start by adding a couple of fields that by default User profiles don't have. Then we will add a new view mode for users which will help us create the right template suggestion.
Add new fields to the User profile
As we just saw in the Card Wide variant, we are displaying the article's author information. By default Drupal does not have a full name or title fields as part of user's profiles. Let's add these fields so we can use them in our cards.
From Drupal's admin toolbar, select Configuration > People > Account Settings
Click Manage fields
Click Add field
Add the following fields and their properties
Label | Machine name | Field type | Allowed number of values |
---|---|---|---|
Full name |
| Text(plain) | 1 |
Title |
| Text(plain) | 1 |
New User view mode
We'll create a new view mode to control which user's fields we can display.
While still within the People Account Settings screen, click Manage Display
Under the Default view mode, expand the CUSTOM DISPLAY SETTINGS fieldset
Click Manage view modes
Scroll down to the Users section and click Add new User view mode link
Type Author as the view mode name and click the Save button
Repeat steps 1 & 2 from Add new fields to the User Profile section at the top of the page
Expand the CUSTOM DISPLAY SETTINGS fieldset
Check Author and click the Save button
Customize Author view mode
From the list of view modes now available (Default, Author, and Compact), click Author
Drag the Member for field into the Disabled section
For the remaining fields, hide their labels by selecting Hidden in the Label's dropdowns
Click the Save button
Remove the link from the Author's image
While still in the People Account Settings screen, click the cogwheel icon to the right of the Image field
Change the view mode to Thumbnail (100 x 100)
Change the Link image to dropdown to Nothing
Click the Update button
Click the Save button
Update the Article content type to use the Author view mode
Go to Blog content type's manage display screen (Structure > Content types > Article > Manage display)
Click the Featured view mode
For the Author field, ensure Rendered entity is set as the format
Click the little cogwheel icon to the right of the Author field
Change the view mode to Author
Click the Update then Save buttons
Changing the user's view mode to Author will help us create a new template suggestion for users.
Last updated