0

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...

1 Answer 1

1

OK Nevermind, 've started again from scratch and this time it worked:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Repeatable Subform -->
<form>
    <field
        name="sponsor"
        type="sponsors"
        label="COM_FOOTBALLMANAGER_SPONSOR_TITLE"
        />
</form>

Class located in administrator/com_comp/src/Field/SponsorsField.php:


namespace NXD\Component\Footballmanager\Administrator\Field;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\Database\DatabaseInterface;

defined('_JEXEC') or die;

class SponsorsField extends ListField{
    /**
     * The form field type.
     *
     * @var    string
     * @since  1.0.0
     */
    protected $type = 'Sponsors';

    /**
     * Method to get the field input markup.
     *
     * @return  string  The field input markup.
     *
     * @since   1.0.0
     */
    protected function getInput()
    {
        return parent::getInput();
    }

    /**
     * Method to get the field options.
     *
     * @return  array  The field option objects.
     *
     * @since   1.0.0
     */
    protected function getOptions()
    {
        $db = Factory::getContainer()->get(DatabaseInterface::class);
        $query = $db->getQuery(true);
        $query->select('id, title');
        $query->from('#__footballmanager_sponsors');
        $query->order('title ASC');
        $db->setQuery($query);
        $sponsors = $db->loadObjectList();

        foreach ($sponsors as $sponsor)
        {
            $options[] = HTMLHelper::_('select.option', $sponsor->id, $sponsor->title);
        }

        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $options);

        return $options;
    }
}

Your Answer

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

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