Created
June 22, 2016 10:10
-
-
Save Stolzenhain/981258d79b22b78de022cfd148ae2ec7 to your computer and use it in GitHub Desktop.
Kirby CMS: Collect files sharing the same name / different extension for HTML5 Video handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//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) { | |
unset($fileGroups[$fileName]); | |
} | |
} | |
//custom filter, as recommended by @distantnative | |
//http://forum.getkirby.com/t/filter-files-by-multiple-file-extensions/697/4 | |
$GLOBALS['fileGroups'] = $fileGroups; //TODO: hacky, because resorting to globals | |
$files = $page->files()->filter(function($file) { | |
$fileGroups = $GLOBALS['fileGroups']; | |
return | |
//filter files with multiple extensions + keep only images | |
(empty($fileGroups[$file->name()])) and ($file->type() == 'image') | |
or | |
//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