Back to Form class

Method loadField

protected \Joomla\CMS\Form\FormField|bool
loadField
(mixed $element, mixed $group = null, mixed $value = null)
Method to load, setup and return a FormField object based on field data.
Parameters
  • string $element The XML element object representation of the form field.
  • string $group The optional dot-separated form group path on which to find the field.
  • mixed $value The optional value to use as the default for the field.
Returns
  • \Joomla\CMS\Form\FormField|bool The FormField object for the field or boolean false on error.
Since
  • 1.7.0
Class: Form
Project: Joomla

Method loadField - Source code

/**
 * Method to load, setup and return a FormField object based on field data.
 *
 * @param   string  $element  The XML element object representation of the form field.
 * @param   string  $group    The optional dot-separated form group path on which to find the field.
 * @param   mixed   $value    The optional value to use as the default for the field.
 *
 * @return  FormField|boolean  The FormField object for the field or boolean false on error.
 *
 * @since   1.7.0
 */
protected function loadField($element, $group = null, $value = null)
{
    // Make sure there is a valid SimpleXMLElement.
    if (!$element instanceof \SimpleXMLElement) {
        throw new \UnexpectedValueException(sprintf('%s::%s `xml` is not an instance of SimpleXMLElement', \get_class($this), __METHOD__));
    }
    // Get the field type.
    $type = $element['type'] ? (string) $element['type'] : 'text';
    // Load the FormField object for the field.
    $field = FormHelper::loadFieldType($type);
    // If the object could not be loaded, get a text field object.
    if ($field === false) {
        $field = FormHelper::loadFieldType('text');
    }
    /*
     * Get the value for the form field if not set.
     * Default to the translated version of the 'default' attribute
     * if 'translate_default' attribute if set to 'true' or '1'
     * else the value of the 'default' attribute for the field.
     */
    if ($value === null) {
        $default = (string) ($element['default'] ? $element['default'] : $element->default);
        if (($translate = $element['translate_default']) && ((string) $translate === 'true' || (string) $translate === '1')) {
            $lang = Factory::getLanguage();
            if ($lang->hasKey($default)) {
                $debug = $lang->setDebug(false);
                $default = Text::_($default);
                $lang->setDebug($debug);
            } else {
                $default = Text::_($default);
            }
        }
        $value = $this->getValue((string) $element['name'], $group, $default);
    }
    // Setup the FormField object.
    $field->setForm($this);
    if ($field->setup($element, $value, $group)) {
        return $field;
    } else {
        return false;
    }
}