2

I have the following field in my module's XML:

<field
    addfieldpath="/modules/mod_xxxxxx/fields"
    name="icon"
    type="filelist"
    label="Icon"
/>

To override this form field in Joomla 3, I would copy:

libraries/joomla/form/fields/filelist.php

to:

/modules/mod_xxxxxx/fields/filelist.php

and make whatever changes I like to the file.

This however, does not work in Joomla 4. The file doesn't even seem to be detected.

According to the Potential backward compatibility issues in Joomla 4, it states:

The libraries/joomla/form/fields and libraries/joomla/form/rules directories are no longer registered to find form classes, all form classes should be autoloaded instead.

But it provides no documentation on how to do so.

Does anyone know how to override a form field in Joomla 4?

3

Apparently, you need to use namespaces in your extension to override fields with the same type. This is because of the class lookup order:

  1. Namespaces added using addfieldprefix attribute or Joomla\CMS\Form\FormHelper::addFieldPrefix() in reverse order.

  2. Joomla\CMS\Form\Field namespace.

  3. J-prefixed autoloaded classes.

  4. J-prefixed classes in directories added using addfieldpath attribute Joomla\CMS\Form\FormHelper::addFieldPath() in reverse order.

To get this working, add <namespace> tag to your extension's manifest file with namespace and optional path relative to your extension's directory, e.g.:

<namespace path="src">Lodder\Module\xxxxxx</namespace>

Field filename and class name should follow this format: [Type]Field where [Type] is the field type. E.g. FilelistField. Note the first uppercase letter. The file should contain namespace declaration. Your "root" namespace should be suffixed with application name (Site or Administrator) depending on whether the module is for frontend or backend. And also contain any subnamespaces. E.g. if the file was in modules/mod_xxxxxx/src/Field/FilelistField.php, namespace declaration should be this:

namespace Lodder\Module\xxxxxx\Site\Field;

Of course, as an alternative to all this you can use a different name for field type.

1
  • Amazing, thanks so much Sharky!
    – Lodder
    Sep 7 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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