Twig file filter
Description
It would be nice to have a filter in Twig templates that allows you to retrieve e. g. the path of a file from its uuid.
There is a solution in the 0.1 Theme https://github.com/contao-themes-net/zero-one-theme-bundle/blob/master/src/Twig/FileExtension.php
Could also be a function instead of a filter I think.
What's your use case? Sounds like the endpoint maybe should use the VFS and then support Uuid|string as an input?
My use case would be one of my own templates, e. g. in the RSCE extenstion. Right now I would like to load several videos as background. But I could think of PDF files to show up in an own template.
Just to give you some background why I'm hesitating to just expose getting a UUID without a context: The question is what 'kind of path' you would assume to get back.
For instance, if you directly asked the mount manager to resolve <foo-uuid> you would get a path files/images/foo.jpg (effectively a root-storage-relative path). If you'd instead used $filesStorage->get(<foo-uuid>)->getPath(), you would get images/foo.jpg - the files-storage-relative path. And if you asked the backup storage, you'd get no path at all (wrong scope).
As briefly discussed on slack: We could create readonly view of the files storage and expose that one via the global contao object.
Usage would then be like so:
{{ contao.filesStorage.generatePublicUri(<uuid>) }}
{{ contao.filesStorage.resolveUuid(<uuid>) }}
{# You could also do other things #}
{% set file_item = contao.filesStorage.get('…') %}
{% if file_item.isImage %}
{% with {figure: figure(file_item, [200, 200])} %}
{{ block('figure_component') }}
{% endwith %}
{% endif %}
Things to consider:
- How to deal with exceptions? One solution could be a VFS wrapper, that catches exceptions (or the
UnableToResolveUuidException) and maybe additionally limits the amount of exposed functions. - We are probably going to need a
|string_to_uuidfilter as well, so that people could use their string/binary string uuids as an input.
How to deal with exceptions? One solution could be a VFS wrapper, that catches exceptions … and maybe additionally limits the amount of exposed functions.
Yes that makes most sense I think :+1: