Yes, first $audio is a structure item and you have to call the individual fields. Secondly, content from the files/pages/users fields is stored as yaml, so you have to use the toFile() method in this case to get the file from the content:
The if statement is necessary to make sure that the file exists before we call a method like url(). Otherwise, PHP would throw an error in case the file was renamed or deleted at a later point in time.
Wow, thank you guys for your quick responses! It is working now.
Yes, normally I use the toFile() method (i.e. with images) to get at the file object. In this case I totally forgot it, must be due to the heat here in Germany I guess
Good suggestion to use a template, I’ll try that next.
Since this is your first post here (hello! ), I’ll just assume you’re new to Kirby.
If you aren’t, you might already know this…
Otherwise, this might interest you:
You can also bind data to files.
Since the presence of a structure item in your page now completely depends on the file being there, you could consider to move the data regarding the file (titel, desc), into the metadata of each file.
You would need to setup a file blueprint and change your page blueprint.
Meta data are a good way to store additional data for files. However, sometimes people don’t like the workflow for adding meta data too much, because you have to go to the file view first before you can enter the data. In that case, your structure field approach might work better. It really depends.
@rasteiner: Thanks! I read in the docs that this is possible, but I am not experienced enough (yet) to make the mental leap in order to apply it in practice. This is my first K3 project – have used K2 for projects in the past.
It’s a good idea, but for my particular project I tend to go with with texnixe’s advice – a structure field is more “immediate” and my client is not very computer literate. I also use a structure field on another page in the project, so there’s some sort of familiarity as far as the UX is concerned.
The question remains, what can be done about the “broken document” icons?
I tried to use the filename with “text: {{ file.filename }}” as a field option for this files field, but it doesn’t work. It is this some sort of internal referencing in the panel going wrong?
What’s your Kirby version? I don’t get that icon but the image src uses the audio file’s url which doesn’t make sense. Strangely, setting image: false doesn’t work either. Might be a bug.
Yes, same here – the audio file’s url is in the src of the image in the cell of the field.
Then again, when I access a row containing audio to edit it, everything displays fine (truncated screenshot):
Yes, that’s exactly the same then apart from the icon. I guess that could be solved with a custom preview for the files field in a structure field depending on mime type.
Unfortunately, setting image: false results in an error:
But maybe the issue is more the preview rather than the field itself.