我在Drupal 8中有多个值图像字段,我想在Controller中准备值以在树枝模板中输出。
对于单个值字段,它很简单(好吧,如果我们可以称呼荒谬而又复杂的“让OOP尽其所能,尽管它毫无用处”, Drupal 8种方式都很简单):
$nids = \Drupal::entityQuery('node')->condition('type', 'zbornik_a_foto')->execute();
$nodes = Node::loadMultiple($nids);
$data = array();
foreach ($nodes as $node) {
$data[] = array(
'rocnik' => $node->get('field_rok')->getValue(),
'miesto' => $node->get('field_miesto_konania')->getValue(),
'fotografie' => $node->get('field_zbornik')->getValue(),
'foto' => $node->get('field_fotografie')->getValue(),
);
}
return array(
'#theme' => 'riadky_zazili',
'#data' => $data,
'#title' => 'Zažili sme',
);
Run Code Online (Sandbox Code Playgroud)
但是,field_fotografie值是多个值字段,我想在$data数组中获取所有图像的URI 。有人知道吗?理想情况下,少于10行的无用的OOP跳线。谢谢。
小智 5
以这种方式尝试,这可能对您有用。
$data = array();
foreach($nodes as $node) {
$photo = array();
foreach($node->get('field_image')->getValue() as $file){
$fid = $file['target_id']; // get file fid;
$photo[] = \Drupal\file\Entity\File::load($fid)->getFileUri();
}
$data[] = array(
'rocnik' => $node->get('field_rok')->getValue(),
'miesto' => $node->get('field_miesto_konania')->getValue(),
'fotografie' => $node->get('field_zbornik')->getValue(),
'foto' => $photo,
);
}
Run Code Online (Sandbox Code Playgroud)