thekid / dialog

Dialog photoblog
2 stars 1 forks source link

Select preview image from latest child element if none available #49

Closed thekid closed 1 year ago

thekid commented 1 year ago

In a nutshell

For a journey, the top images (of which the first is used for previewing) will most probably not be set until after the journey has finished. Instead of leaving the preview image empty, we choose the first image from the latest child content. This will update the preview image every time new content is added.

Migration script

All entries are now expected to have a preview element. It can be created by either re-importing everything or by running this script:

use com\mongodb\MongoConnection;
use util\cmd\Console;

$conn= new MongoConnection($argv[0]);
$collection= $conn->database($argv[1])->collection('entries');
foreach ($collection->find() as $entry) {
  Console::writeLine('> ', $entry['title']);

  // Create preview field and set it to the first image
  $preview= empty($entry['images']) ? null : ['slug' => $entry['slug'], ...$entry['images'][0]];
  $collection->update($entry->id(), ['$set' => ['preview' => $preview]]);
}

Note this script only sets the preview field, it does not query the children as the import API does. This is because I haven't encountered this situation before 😉

thekid commented 1 year ago

Released in https://github.com/thekid/dialog/releases/tag/v1.14.0