2017-04-16 17 views
1

私はDrupal 8に複数の値の画像フィールドを持っています。私は、twigテンプレートの出力用にControllerに値を用意したいと思います。Drupal 8の複数の値フィールドの値を取得する方法は?

それは、単一の値のフィールドのために(だけでなく、私たちは "でき-DO-すべてイン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を取得したいと考えています。誰がどのように知っていますか?理想的には、無駄なOOPジブバージャバーの10行未満です。ありがとう。これは おかげ

答えて

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

希望、それはあなたのために役に立つかもしれません。

$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

は、この方法のように試してみてください、あなたを助けます

関連する問題