Ich weiß nicht, ob es der eleganteste Weg ist, aber ich hab mir jetzt einen eigenen Twig Filter gebaut, der mir den Mimetype im Template ausgibt.
PHP-Code:
<?php
// src/Twig/AppExtension.php
namespace App\Twig;
use Contao\FilesModel;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('contao_get_mime_type', [$this, 'getMimeType']),
];
}
public function getMimeType($uuid)
{
$fileModel = FilesModel::findByUuid($uuid);
if ($fileModel && $fileModel->path) {
$mimeType = mime_content_type($fileModel->path); // Ermittelt den MIME-Typ
return $mimeType ?: null;
}
return null;
}
}
Ich habe das direkt mit einem Filter verbunden, der mir den Pfad einer UUID ausgibt:
PHP-Code:
<?php
// src/Twig/AppExtension.php
namespace App\Twig;
use Contao\FilesModel;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('contao_find_file_by_uuid', [$this, 'findFileByUuid']), // Gib den Pfad einer UUID aus
new TwigFilter('contao_get_mime_type', [$this, 'getMimeType']), // Gib den Mime Type einer UUID aus
];
}
public function findFileByUuid($uuid)
{
$fileModel = FilesModel::findByUuid($uuid);
return $fileModel ? $fileModel->path : null;
}
public function getMimeType($uuid)
{
$fileModel = FilesModel::findByUuid($uuid);
if ($fileModel && $fileModel->path) {
$mimeType = mime_content_type($fileModel->path);
return $mimeType ?: null;
}
return null;
}
}
Danke an Bernhard, der in Slack den entscheidenden Hinweis gab!
Dank dieser Filter kann ich nun <video> Tags mit <source> Tags ausgeben, die sowohl mp4 als auch webm unterstützen:
HTML-Code:
<figure class="video_container">
<video autoplay muted loop style="width:100%">
{% for uuid in video %}
{% set filePath = uuid | contao_find_file_by_uuid %}
{% set mimeType = uuid | contao_get_mime_type %}
<source type="{{ mimeType }}" src="{{ filePath }}">
{% endfor %}
</video>
</figure>