protected \Joomla\CMS\Form\Form
loadForm
(mixed $name, mixed $source = null, mixed $options = array(), mixed $clear = false, mixed $xpath = null)
/**
* Method to get a form object.
*
* @param string $name The name of the form.
* @param string $source The form source. Can be XML string if file flag is set to false.
* @param array $options Optional array of options for the form creation.
* @param boolean $clear Optional argument to force load a new form.
* @param string $xpath An optional xpath to search for the fields.
*
* @return Form
*
* @see Form
* @since 4.0.0
* @throws \Exception
*/
protected function loadForm($name, $source = null, $options = array(), $clear = false, $xpath = null)
{
// Handle the optional arguments.
$options['control'] = ArrayHelper::getValue((array) $options, 'control', false);
// Create a signature hash. But make sure, that loading the data does not create a new instance
$sigoptions = $options;
if (isset($sigoptions['load_data'])) {
unset($sigoptions['load_data']);
}
$hash = md5($source . serialize($sigoptions));
// Check if we can use a previously loaded form.
if (!$clear && isset($this->_forms[$hash])) {
return $this->_forms[$hash];
}
// Get the form.
Form::addFormPath(JPATH_COMPONENT . '/forms');
Form::addFormPath(JPATH_COMPONENT . '/models/forms');
Form::addFieldPath(JPATH_COMPONENT . '/models/fields');
Form::addFormPath(JPATH_COMPONENT . '/model/form');
Form::addFieldPath(JPATH_COMPONENT . '/model/field');
try {
$formFactory = $this->getFormFactory();
} catch (\UnexpectedValueException $e) {
$formFactory = Factory::getContainer()->get(FormFactoryInterface::class);
}
$form = $formFactory->createForm($name, $options);
// Load the data.
if (substr($source, 0, 1) === '<') {
if ($form->load($source, false, $xpath) == false) {
throw new \RuntimeException('Form::loadForm could not load form');
}
} else {
if ($form->loadFile($source, false, $xpath) == false) {
throw new \RuntimeException('Form::loadForm could not load file');
}
}
if (isset($options['load_data']) && $options['load_data']) {
// Get the data for the form.
$data = $this->loadFormData();
} else {
$data = array();
}
// Allow for additional modification of the form, and events to be triggered.
// We pass the data because plugins may require it.
$this->preprocessForm($form, $data);
// Load the data into the form after the plugins have operated.
$form->bind($data);
// Store the form for later.
$this->_forms[$hash] = $form;
return $form;
}