I make a TWIG template for the home page of my Drupal 8 site.
page--front.html.twig
I want to display on this page the picture of the current user.
How to display the picture of the current user in a TWIG model ?
You can use this code to get user image:
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
if (!$user->user_picture->isEmpty()) {
$displayImg = file_create_url($user->user_picture->entity->getFileUri());
}else{
$displayImg = '';
}
Then pass it as variable to twig, check if it's empty or not and depending on that print inside some img tag.
Update:
To get image style url you could do something like:
$style = \Drupal::entityTypeManager()->getStorage('image_style')->load('thumbnail');
$url = $style->buildUrl($displayImg);
Based on: https://www.webomelette.com/how-render-your-images-image-styles-drupal-8
And you should assign twig variable from your home page controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.