1

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"
/>

enter image description here

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);
7
  • You will have to handle the max upload size yourself. You'll also need to override that layout if you wish to change or remove the text
    – Lodder
    Nov 17, 2020 at 19:19
  • I do already handle the max upload size in the save method of the controller, that works (I was just wondering if anything is already implemented that I missed). I'm looking for the proper way to change that one line (or the value of $maxSize). Do I need to set up a custom form field?
    – jost21
    Nov 18, 2020 at 7:46
  • You'll need to override this layout layouts/joomla/form/field/file.php
    – Lodder
    Nov 18, 2020 at 8:30
  • I appreciate your help, but I already guessed that I somehow have to override that layout, hence I mentioned it in the question. But I do not know what the appropriate way is to accomplish that. With FieldsHelper::render (like mentioned here)? I didn't get it to work.
    – jost21
    Nov 18, 2020 at 10:10
  • 1
    Copy layouts/joomla/form/field/file.php to components/com_YOURCOMP/layouts/joomla/form/field/file.php. Or if it's for the form in the administrator part of your component, then administrator/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
    – Lodder
    Nov 18, 2020 at 10:29

1 Answer 1

1

If you'd like to perform this override for all file form fields only within your component, then you can override the layout at component level.

To do this copy the following layout:

layouts/joomla/form/field/file.php

to:

components/com_YOURCOMP/layouts/joomla/form/field/file.php

If the form field reside in the administrator views, then copy the layout to:

administrator/components/com_YOURCOMP/layouts/joomla/form/field/file.php

Once done, you can make the necessary changes to the layout without it affecting the visual aspects anywhere else on the site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.