Back to SubformField class

Method loadSubFormData

protected \Joomla\CMS\Form\Form[]
loadSubFormData
(\Joomla\CMS\Form\Form $subForm)
Binds given data to the subform and its elements.
Parameters
  • \Joomla\CMS\Form\Form $subForm Form instance of the subform.
Returns
  • \Joomla\CMS\Form\Form[] Array of Form instances for the rows.
Since
  • 3.9.7
Class: SubformField
Project: Joomla

Method loadSubFormData - Source code

/**
 * Binds given data to the subform and its elements.
 *
 * @param   Form  $subForm  Form instance of the subform.
 *
 * @return  Form[]  Array of Form instances for the rows.
 *
 * @since   3.9.7
 */
protected function loadSubFormData(Form $subForm)
{
    $value = $this->value ? (array) $this->value : array();
    // Simple form, just bind the data and return one row.
    if (!$this->multiple) {
        $subForm->bind($value);
        return array($subForm);
    }
    // Multiple rows possible: Construct array and bind values to their respective forms.
    $forms = array();
    $value = array_values($value);
    // Show as many rows as we have values, but at least min and at most max.
    $c = max($this->min, min(\count($value), $this->max));
    for ($i = 0; $i < $c; $i++) {
        $control = $this->name . '[' . $this->fieldname . $i . ']';
        $itemForm = Form::getInstance($subForm->getName() . $i, $this->formsource, array('control' => $control));
        if (!empty($value[$i])) {
            $itemForm->bind($value[$i]);
        }
        $forms[] = $itemForm;
    }
    return $forms;
}