Pho*_*vin 1 drupal drupal-8 drupal-entities
我试图弄清楚如何从 Drupal 8 中的实体获取图像的路径。我原以为 get()->value 可以做到这一点,但这只返回一个空白字符串。
我有一个测试功能:
function getValueTest ($profile_id, $field)
{
$profile_storage = \Drupal::entityManager()->getStorage('profile');
$profile = $profile_storage->load($profile_id);
if ($profile != null)
{
if ($profile->hasField ($field))
{
return $profile->get ($field)->value;
}
}
return "No field" . $field;
}
Run Code Online (Sandbox Code Playgroud)
假设某个配置文件 ID 3 有两个字段 field_first_name 和 field_mugshot。如果我打电话:
dpm ($this->getValueTest (3, 'field_first_name'));
dpm ($this->getValueTest (3, 'field_mugshot'));
Run Code Online (Sandbox Code Playgroud)
第一个调用在消息区域中正确显示第一个名称,但第二个调用仅给出一个空白字符串。我需要图像的路径,以便我可以对其内容进行一些处理。
您可以使用以下方法获取 uri 或 url:
$entity->get('field_image')->entity->getFileUri();
$entity->get('field_image')->entity->url();
Run Code Online (Sandbox Code Playgroud)
这是因为 value() 方法应该返回字段(也称为 fid)的值,在本例中字段是实体引用。
归档时间: |
|
查看次数: |
3581 次 |
最近记录: |