All images being shown, not just selected

Issue:
All uploaded images, including images that get de-selected from a list are still appearing in the PHP output when using $project->images()

I cannot find any documentation showing how to show only selected files in the PHP output. Below is my Blueprint, Template and instructions on how to recreate the issue, using Kirby 3

Blueprint:

title: Project
fields:
  images:
    type: files
    layout: cards
    template: image
    required: false
    info: "{{ file.dimensions }}"
    image:
      ratio: 16/9
      cover: true
    min: 1
    size: small

Template:

<? $projects = $site->find('projects')->children()->listed(); ?>

<? foreach ($projects as $project): ?>
  <? foreach ($project->images() as $image): ?>
    <img  src="<?= $image->resize(160)->url(); ?>" />
  <? endforeach ?>
<? end ?>

Example:

  • Two images are uploaded via a project panel.
  • Both images available to select in dropdown menu when using Add > Select.
  • First image is deleted from the list, and shows as unselected in Add > Select.

Expected output in template for $project->images:

 object(Kirby\Cms\Files)#321 (1) { 
  [0]=> string(58) "projects/projectA/image-2.jpg"
 }

Actual output in template for $project->images:

 object(Kirby\Cms\Files)#321 (2) { 
  [0]=> string(58) "projects/projectA/image-1.jpg" 
  [1]=> string(58) "projects/projectA/image-2.jpg"
 }

I figured out a solution for anyone who also has this issue.

I think the problem was that i had a field named β€˜images’ so Kirby’s internal method $page->images() was being called instead of $page->myfieldname() shorthand.

To get around this, as my client had already input content. I chained these methods together:
$page->content()->get('images')->toFiles()

And this produced the expected output.

2 Likes