For my Project i need to have a Custom Field that gets all "Sponsors" from a related table, the user can then select the items and link them to a "location" so define the sponsors for a location. Nothing fancy In J3 I've done such things a lot but i have no clue (nor guides) on how to render the customfield correctly in J4.
form xmls got stored in /administrator/components/com_comp/forms
<!-- Repeatable Subform -->
<form>
<field name="sponsor" type="sponsorselection" label="Sponsor" />
</form>
Fields got stored in /administrator/components/com_comp/src/Field
namespace NXD\Component\FootballManager\Form;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField as ListField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseInterface;
class SponsorselectionField extends ListField
{
protected $type = 'sponsorselection';
protected function getOptions()
{
$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db->getQuery(true)
->select($db->quoteName(['id', 'title']))
->from($db->quoteName('#__footballmanager_sponsors'))
->order($db->quoteName('title'));
$db->setQuery($query);
$results = $db->loadObjectList();
$options = array();
foreach ($results as $result)
{
$options[] = HTMLHelper::_('select.option', $result->id, $result->name);
}
return $options;
}
}
When loading this field i receive an error 500 including the info that this name (SponsorselectionField) is already in use. So the Repeatable Subform might be the issue > OK but how to handle that in the Field Class? As far as i can see there are no good example for J4 - yeah i could go the JForm J3 Way but i dont want to if there is a newer / better way...