获取用户的个人资料图片网址

问题描述:

我使用Drupal 8和嫩枝工作。我有一个领域的“团队成员”,也就是一个实体引用,以用户的内容类型。获取用户的个人资料图片网址

Entity Reference to User

在我设定的团队成员此内容类型的内容。

Team Members set on Content

我怎样才能得到这些用户的个人资料图片的网址,在我的树枝模板?


我曾尝试:

{% for key, item in content.field_team_members if key|first != '#' %} 
    <p>{{ dump(item['#options'].entity.user_picture.0|keys) }}</p> 
{% endfor %} 

导致:

array(5) { 
    [0]=> 
    string(9) "target_id" 
    [1]=> 
    string(3) "alt" 
    [2]=> 
    string(5) "title" 
    [3]=> 
    string(5) "width" 
    [4]=> 
    string(6) "height" 
} 

我能得到user_picture实体,但不能看到一种方式来获得从资料图片网址这

我还认为,只是做:

{% for key, item in content.field_team_members if key|first != '#' %} 
    {{ item['#options'].entity.user_picture.0 }} 
{% endfor %} 

会显示图像,但我收到一个错误。

该网站遇到意外错误。请稍后再试。

异常:无法打印Drupal \ image \ Plugin \ Field \ FieldType \ ImageItem类型的对象。在Drupal \核心\模板\ TwigExtension-> escapeFilter()(行441>核心/ LIB/Drupal的/核心/模板/ TwigExtension.php)。

此外,我已经注意到,“content.field_team_members”数组包含不同的数据,具体取决于我是否登录到Drupal。 for循环甚至不循环,如果我没有登录,这意味着这种做法不会为工作游客到我的网站,所以必须有一个不同的方法。

我解决了这个问题,在我的mythemename.theme文件中添加了一个预处理函数,它将配置文件图片URL添加到暴露给Twig的$ variables数组中。

预处理功能:

function mythemename_preprocess_ds_1col__node_portfolio_item(&$variables) { 

    $teamMembers = array_filter(array_keys($variables['content']['field_team_members']), 'is_int'); 
    $teamMembersCount = count($teamMembers); 

    for ($i = 0; $i < $teamMembersCount; $i++) { 
    $user = &$variables['content']['field_team_members'][$i]; 
    $user['imgUrl'] = file_create_url($user['#options']['entity']->user_picture->entity->getFileUri()); 
    } 

所以URL可以通过枝条进行访问:

{% for key, item in content.field_team_members if key|first != '#' %} 
    <img src="{{ item['imgUrl'] }}" /> 
{% endfor %} 

第二个问题

“content.field_team_members” 数组包含不同数据取决于我是否登录到D. rupal

根据权限解决。在example.com/admin/people/permissions“查看用户信息”字段必须检查匿名用户

Permission Setting

我有类似的东西用{{ entity.field.value }}{{ entity.field.0.value }}更换{{ entity.field.0 }}工作。

所以,在你的情况{{ item['#options'].entity.user_picture.value }}

我不确定这是否是“正确”的方法,但它似乎与我的数据一起工作。