Add file to a files collection

I must add a file from the asset folder to my file collection but has no success. Anybody a hint how to do this? Here are my tryouts:

if ( $page->hasImages() ) {
  $produkt_images = $produkt->images();
  if ( $produktart == 'Poster' ) {
    $produkt_images->add($kirby->url('assets') . '/images/poster.jpg');
  }
  foreach ( $produkt_images as $image ) {
    echo $image->url();
  }
}

You can only pass a file/files object or a file id as parameters to add(), not a path to some other file.

But as I know there is a way to convert Assets to a file object?

There is a way to convert a file in the assets folder to an Asset object, but you cannot add an asset object to a files collection.

Maybe via a virtual file object, don’t know.

Or using a generic collection instead of a files collection.

<?php
$coll = new Collection();
$asset = asset('assets/icons/instagram.svg');
$files = $page->files();
$coll = $coll->add($files)->add($asset);
foreach ( $coll as $file ) {
  echo $file;
}
1 Like

Exactly what I was searching for :slight_smile: