Add multiple file/images in unlimited fields (multi valued fields) programatically?

  • Open
  • shahnawazmalik
    Keymaster
    January 14, 2020 at 6:09 am #4722

    foreach ($data as $key => $item)
    {
    $FilePaths_keys = array_keys(array_column($item[‘bitstreams’], ‘format’), ‘PDF’, false);
    foreach ($FilePaths_keys as $path){
    {
    $get_data_contents = file_get_contents($item[‘bitstreams’][$path][‘retrieveLink’]);
    $file = file_save_data($get_data_contents, ‘public://’.$item[‘bitstreams’][$path][‘name’], FILE_EXISTS_REPLACE);
    }

    $node = Node::create([
    ‘type’ => ‘my_contentType’,
    ‘title’ => $item[‘name’],
    ‘body’ => $item[‘body’],
    ‘field_digital_files’ => [
    ‘target_id’ => $file->id(),
    ‘alt’ => $item[‘bitstreams’][$path][‘name’],
    ‘title’ => $item[‘bitstreams’][$path][‘name’]
    ],
    ]);
    $node->save();
    }

    This code saves all the files in public folder but only one file get mapped in node. Please help me how to save these multiple images dynamically at the time node creation?