templates - 如何在 TWIG 模型中显示当前用户的图片?
问题描述
我为我的 Drupal 8 站点的主页制作了一个 TWIG 模板。
page--front.html.twig
我想在这个页面上显示当前用户的图片。
如何在 TWIG 模型中显示当前用户的图片?
解决方案
您可以使用此代码获取用户图像:
$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 = '';
}
然后将它作为变量传递给 twig,检查它是否为空,并取决于某个 img 标签内的打印。
更新:
要获取图像样式 url,您可以执行以下操作:
$style = \Drupal::entityTypeManager()->getStorage('image_style')->load('thumbnail');
$url = $style->buildUrl($displayImg);
基于:https ://www.webomelette.com/how-render-your-images-image-styles-drupal-8
你应该从你的主页控制器分配树枝变量。
推荐阅读
- python - 将数据加载到 pandas 数据帧到 ms sql python
- python - AttributeError:“模块”对象没有属性“SSLContext”
- c++ - 指向结构/类的递归成员的指针的可变序列作为模板参数
- javascript - 如何为未知的 JSONData 自动添加制表符列设置
- database - 使用 HTTP 响应实现 Dialogflow
- python - 如何将 Keras 预测输出转换为所需的二进制值
- java - 如何在不向 Src 添加文件的情况下将文件放入 GitLab 测试
- c# - Wavecom 调制解调器 stk 的每个功能第一次都能完美运行,而不是其他时间。使用 AT 命令和 C#
- sql - 从触发器调用过程以验证生日
- ios - 如何获取 iOS BSSID