如何在Drupal 8中获取多个值字段的值?

时间:2017-04-16 19:27:27

标签: drupal-8

我在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',
);

但是,field_fotografie值是多个值字段,我想在$data数组中获取所有图像的URI。有人知道吗?理想情况下,不到10行无用的OOP jibber-jabber。感谢。

2 个答案:

答案 0 :(得分: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,
  );
}

答案 1 :(得分:0)

$nids = \Drupal::entityQuery('node')->condition('type', 'zbornik_a_foto')->execute();
$nodes = Node::loadMultiple($nids);

$data = array();
foreach ($nodes as $node) {
  $fotoValues = $node->get('field_fotografie')->getValue();
  $myvalues  = array();
  foreach($fotoValues as $val){
    $myvalues[] = $val["value"];
  }
  $data[] = array(
    'rocnik' => $node->get('field_rok')->getValue(),
    'miesto' => $node->get('field_miesto_konania')->getValue(),
    'fotografie' => $node->get('field_zbornik')->getValue(),
    'foto' => $myvalues,
  );
}

return array(
  '#theme' => 'riadky_zazili',
  '#data' => $data,
  '#title' => 'Zažili sme',
);

希望这会对你有所帮助 感谢

相关问题