Joomla prints a short notice regarding the Maximum upload size when including a file upload field with the standard form field file
, see code and image below:
<field
name="image"
type="file"
label="COM_EXAMPLE_PICTURE_LABEL"
description="COM_EXAMPLE_PICTURE_DESC"
accept="image/*"
/>
<field
name="MAX_FILE_SIZE"
type="hidden"
default="2097152"
/>
The Joomla docs for file mentions adding a hidden field to set the maximum file size. It's not entirely clear if Joomla does anything with that field or if all the logic needs to implemented in the controller (e.g. in the save
method).
You can put a soft limit file size by adding a hidden field with name="MAX_FILE_SIZE" and value the maximum allowed bytes which is handled by php, but you must also handle it in your code with or without it.
However, that wouldn't change the way the field is displayed. Is there a way to show a different maximum file size?
It looks like that message is set in layouts/joomla/form/field/file.php
:
...
$maxSize = JHtml::_('number.bytes', JUtility::getMaxUploadSize());
...
<?php echo JText::sprintf('JGLOBAL_MAXIMUM_UPLOAD_SIZE_LIMIT', $maxSize); ?>
PS: I don't what to change the upload_max_filesize
for PHP in the php.ini
, just for this particular form.
Update
Using the approach suggested by Lodder, I added a file components/com_YOURCOMP/layouts/joomla/form/field/file.php
to my component and copied the content of layouts/joomla/form/field/file.php
. Then I replaced the line
$maxSize = JHtml::_('number.bytes', JUtility::getMaxUploadSize());
by
$xmlData = simplexml_load_file(JPATH_COMPONENT . '/models/forms/add-form.xml');
$fieldMaxSize = $xmlData->xpath('//field[@name="MAX_FILE_SIZE"]');
if (isset($fieldMaxSize[0]['default']) && !empty($fieldMaxSize[0]['default']))
{
$sizeValue = $fieldMaxSize[0]['default'];
}
else
{
$sizeValue = JUtility::getMaxUploadSize();
}
$maxSize = JHtml::_('number.bytes', $sizeValue);
$maxSize
). Do I need to set up a custom form field?layouts/joomla/form/field/file.php
FieldsHelper::render
(like mentioned here)? I didn't get it to work.layouts/joomla/form/field/file.php
tocomponents/com_YOURCOMP/layouts/joomla/form/field/file.php
. Or if it's for the form in the administrator part of your component, thenadministrator/components/com_YOURCOMP/layouts/joomla/form/field/file.php
. Overriding this layout will apply for ALL file fields, but only those that are part of your component. If you genuinly need this change for a single file field in your component (assuming you're using this file multiple times), then you'll need to create a custom field