Homepage with random selected images / Kirby 2

Hello,

I try to have on the home page of the site, a random image among a selection of images coming from different portfolio pages of the site (www.oliviergassies.fr, for the moment the site shows random images of the homepage).

For that I thought to create via blueprint for the portfolio template a toogle field for images. I toggle images I want for homepage. And then one toggled image is selected randomly and showed on hompage. But…

Here details

In the portfolio.yml blueprint :

files:
  sortable: true
  fields:
    featured:
      label: Featured
      type: toggle

On the home.php page :

<?php $featured = $pages->children()->visible()->images()->shuffle('true')->first(); ?>

Thank you for your help,
Best

I’m a bit confused, what is files, how can files have a field?

Ah, sorry, Kirby 2…

You would have to get the images of all pages filtered by the toggle field

<?php $featured = $pages->children()->visible()->images()->filterBy('featured', true)->shuffle()->first(); ?>

Here is my entire blueprint

title: Portfolio

files:
  sortable: true
  fields:
    featured:
      label: Featured
      type: toggle

pages: false

fields:
  title:
    label: Title
    type:  text
    width: 1/2
  private:
    label: Private
    type: checkbox
    text: Private page
    width: 1/2
  types:
    label: Type
    type: tags
    width: 1/2
  editors:
    label: Editors
    type: tags
    width: 1/2
  years:
    label: Years
    type: tags
    width: 1/2
  series:
    label: Series
    type: tags
    width: 1/2
  description:
    label: Text
    type: textarea 
  tag:
    label: Description for robots
    type: textarea

Thank you for the answer.
I can not make it work.

What does the toggle field actually store in the files meta data?

And what exactly is the issue? No image at all, wrong image?

An exemple of the content in the file metadata :

Featured: true

All pictures are shown.

But first() returns only a single image, so it can’t really show all images. Or do you mean, a random single image out of all images, not only the featured ones.

Anyway, try with a string instead of a boolean:

<?php $featured = $pages->children()->visible()->images()->filterBy('featured', 'true')->shuffle()->first(); ?>

Sorry, i mean a random image out of all images.
This works,
Thank you !
Best