My brain hurts, I'm using a custom listfield inside a subform in multiple mode, the rendering works as expected (two elements in database > two elements in the form). but i'm not able to set the value (selected) state in the listfield. I have no clue on how to pass the data from my subform (that loops over the linking table items) into the field used inside the subform.
This is what i have currently: Custom Subform Field Class:
class SponsorssubformField extends SubformField
{
/**
* The form field type.
*
* @var string
* @since 1.0.0
*/
protected $type = 'Sponsorssubform';
protected $min = 0;
protected $value;
protected function loadSubFormData(Form $subForm)
{
$linked_sponsors = $this->getLinkedSponsors();
$value = $linked_sponsors ? (array) $linked_sponsors : [];
// Simple form, just bind the data and return one row.
if (!$this->multiple) {
$subForm->bind($linked_sponsors);
return [$subForm];
}
// Multiple rows possible: Construct array and bind values to their respective forms.
$forms = [];
$i = 0;
foreach ($linked_sponsors as $linked_sponsor) {
echo '<pre>' . var_export($linked_sponsor, 1) . '</pre>';
$control = $this->name . '[' . $this->fieldname . $i . ']';
$itemForm = Form::getInstance($subForm->getName() . $i, $this->formsource, ['control' => $control]);
$itemForm->bind($linked_sponsor);
$forms[] = $itemForm;
$i++;
}
return $forms;
}
protected function getLinkedSponsors()
{
$parentId = $this->form->getValue('id');
$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db->getQuery(true);
$query->select('id, sponsor_id');
$query->from('#__footballmanager_locations_sponsors');
$query->where('location_id = ' . $parentId);
$query->order('ordering, id');
$db->setQuery($query);
$sponsors = $db->loadObjectList();
foreach ($sponsors as $sponsor)
{
$sponsor->value = $sponsor->sponsor_id;
}
return $sponsors;
}
Custom ListField Class:
class SponsorsField extends ListField{
/**
* The form field type.
*
* @var string
* @since 1.0.0
*/
protected $type = 'Sponsors';
protected $value;
/**
* Method to get the field options.
*
* @return array The field option objects.
*
* @since 1.0.0
*/
protected function getOptions()
{
echo '<pre>' . var_export( null, 1) . '</pre>';
$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;
}
}
Options got loaded, the elements (count) in the repeatable subform are correct but how can i set the value of inside the subform field?
the linking table structure is as follows:
CREATE TABLE IF NOT EXISTS `#__footballmanager_locations_sponsors`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`location_id` int(11) NOT NULL,
`sponsor_id` int(11) NOT NULL,
`ordering` int(11) DEFAULT 0,
PRIMARY KEY (`id`),
...
);