I want the backend user to be able to upload mp3 files that will be triggered when an bell icon on the website is clicked.
I have this yml in blueprints-pages-footer.yml:
title: Footer
fields:
bell:
label: Bell Sound
type: files
max: 1
required: false
accept:
- audio/*
When I try uploading any mp3 in the backend, I get this error:
Object of class Kirby\Content\Field could not be converted to int
Despite this error, the file is uploaded into the content folder but the field in footer.txt remains empty. I can then select the file in the backend and dont encounter the error and observe, that the footer.txt (content-3_footer-footer.txt) now shows:
Bell: - file://f8qb0rZYLCxaDdJZ
Uuid: ZeE4X1seEoyptKSM
What does this error mean, Chatgpt is not able to help.
Thank you for any help or hint!
hard to tell without seeing the code. But in any case, accept
is nit a valid property of the files field. If you want to limit file types that cannbe uploeded, you need to assign a blueprint and set the property there,see files field docs.
1 Like
I do not really need to limit the files, I trust the user to only upload mp3 or wav when its written into the help line.
What code do you mean?
<?php
$footerPage = page('footer');
$bellFile = $footerPage->bell()->toFiles()->first();
?>
<div class="footer">
<img class="svgImage bell" id="bell" src="<?= url('assets/svg/hotel_bell.svg') ?>" alt="Hotel Bell">
</div>
<?php if ($bellFile): ?>
<audio id="bellSound" src="<?= $bellFile->url() ?>" preload="auto"></audio>
<?php endif; ?>
<script>
document.querySelectorAll('.bell').forEach(el => {
el.addEventListener('click', () => {
const audio = document.getElementById('bellSound');
if (audio) {
audio.currentTime = 0;
audio.play().catch(err => console.error('Playback failed', err));
}
});
});
</script>
This is the footers php. (the int error occures even when this php is entirely commented out…)
Sorry if I am unclear, I am just a graphic designer trying to keep up with coding, hope you do not mind redundant questions, thank you for your help Texnixe!
The int error occurs on the template, not in the Panel, correct?
If so, please check if you use the bell
field anywhere else in the template or a snippet. If the error still occurs when you comment out the section, it cannot be caused by it (at least if you use PHP comments and not just wrap the section in HTML comments – PHP code will still be executed within these).
1 Like
Thanks for your suggestion!
After a lot of trial and error, I realised that a plug-in I created crops all uploads to a fixed width.
Since the audio file has no width, the error occures when tring to upload an sound file. I canged the crop plug-in, so that it checks if the upload is an image file before cropping it.
Thank you two for your generous help!
And the remark that a html comment does not stop a php line from executing, I did not know that.
Have a nice week