Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Kirby CMS: Collect files sharing the same name / different extension for HTML5 Video handling
//build lookup array for files with the same name but a different extension
$fileGroups = [];
foreach($page->files() as $file) {
if (!isset($fileGroups[$file->name()])) {
$fileGroups[$file->name()] = [];
array_push($fileGroups[$file->name()], $file->extension());
//remove all files with only 1 extension – no duplicates, no videos
foreach($fileGroups as $fileName => $extensions) {
if (count($extensions) <= 1) {
//custom filter, as recommended by @distantnative
$GLOBALS['fileGroups'] = $fileGroups; //TODO: hacky, because resorting to globals
$files = $page->files()->filter(function($file) {
$fileGroups = $GLOBALS['fileGroups'];
//filter files with multiple extensions + keep only images
(empty($fileGroups[$file->name()])) and ($file->type() == 'image')
//multiple extensions means: video, means: use only .mp4 file
$file->extension() == 'mp4'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment