Files as virtual pages

I am using a model to turn images on page into virtual subpages - the trouble is this removes the abiility to mix normal content pages with them. How can I adapt the model, and keep the images as pages, and make the normal images respect the sort order? I use the sort field on the images to order them - i need the normal content pages to respect that order too. For example, i might have 10 images but i might want the ordinary content page to be 3rd for example, with images either side in the page order, if that makes sense.

Heres my model:

<?php

class ShotsPage extends Page
{


    // Use images as pages
    public function children()
    {

        $images = [];

        foreach ($this->images() as $image) {

          $images[] = [
              'slug'     => $image->name(),
              'num'      => $image->sort()->isNotEmpty() ? $image->sort()->value() : null,
              'template' => 'image',
              'model'    => 'image',
          ];
        }
        
        return Pages::factory($images, $this);
    }

}

How would the normal page know its sorting number? This sounds as if it could get rather tricky.

I thought it might be - would it be easier to list those pages first so then run the image sort order on from there? Or is there a better way - i guess i dont HAVE to use the sort order, thats just how it works right now.

Essentialy i want to be able to order both of them in the panel.

First of all, you need to extend your page model so that your children include both the real pages and the virtual pages.

This sorting stuff, I don’t know if manual sorting is actually such a good idea. Because even if you overwrite the changeSort() method to do one thing for the images and another thing for real pages, this will easily get messed up because the pages themselves could be sorted independently in the parent page.

I wonder if it wouldn’t be a lot easier to create real pages for the images for such a mixed use case… :thinking:

I did it like this because that gets tedious with dozens of images. This way you can just upload 50+ images and not have to worry. For the particular task Im trying to do at the moment i only need 1 real content page which is an information page about the album of images, but i figured i may as well go the whole 9 yards and makeit possible to add more pages.

I guess i could put some fields in the page thats the parent to images, but i really want to be able to hit directly on its own URL.

Its a tricky one :slight_smile:

p.s happy cake day @texnixe :slight_smile: :cake:

Thanks, 6 years on the forum to celebrate :tada:

There’s now a recipe about virtual files