Custom sorting with virtual pages

Hi everyone, I have a setup for a website with virtual pages from a CSV file and it all works great. But we would like to have the option to custom sort these pages via the panel, which doesnt seem to work (I can drag the pages around in the panel but the order isn’t stored anywhere). Is there a way I can achieve that with virtual pages?

I found this recipe on merging content sources from virtual pages and “real” pages. But my CSV has about a 1000 entries so manually creating a folder for each entry is a bit of a problem :slight_smile:

Would really appreciate any help! Thanks

Is it possible perhaps to turn virtual pages into “real” pages?

Only if you store these changes somewhere, would make more sense for data from a database then from a csv file.

Yes, you can loop through the csv data and create pages from them programmatically.

Hi @moonwalk, thanks for the reply. could you help out with how would I do that? not sure how.
(would that be the best way or storing the sorting data somewhere is preferable?)

This is my helper plugin

<?php
function csv(string $file, string $delimiter = ','): array
{
  $lines = file($file);

  $lines[0] = str_replace("\xEF\xBB\xBF", '', $lines[0]);

  $csv = array_map(function($d) use($delimiter) {
    return str_getcsv($d, $delimiter);
  }, $lines);

  array_walk($csv, function(&$a) use ($csv) {
   $a = array_combine($csv[0], $a);
  });

  array_shift($csv);

  return $csv;
}

And the models/scans.php

<?php

class ScansPage extends Page
{

  public function children()
  {
    $csv      = csv($this->site() . '/database/scans.csv', ';');
    $children = array_map(function ($scan) {
      return [
        'slug'     => Str::slug($scan['Image ID']),
        'template' => 'scan',
        'model'    => 'scan',
        'num'      => 0,
        'content'  => [
          'imageId'  => $scan['Image ID'],
          'bookTitle'=> $scan['Book Title'],
          'author'   => $scan['Author'],
          'refNo'    => $scan['Ref No.'],
          'page'     => $scan['Page'],
          'shelfEn'  => $scan['Library Shelf (EN)'],
          'libCatEn' => $scan['Library Category (EN)'],
          'photoscan' => Str::lower($scan['Image ID']),
        ]
      ];
    }, $csv);

    return Pages::factory($children, $this);
  }

}

thanks :pray: