API16

JForm/loadFieldType

From Joomla! Documentation

< API16:JForm
Revision as of 21:53, 13 May 2013 by JoomlaWikiBot (talk | contribs) (removing red link to edit, no existant pages)

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Method to load a form field object.

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax[edit]

loadFieldType($type, $new=true)
Parameter Name Default Value Description
$type $type The field type.
$new true $new Flag to toggle whether we should get a new instance of the object.

Returns[edit]

mixed Field object on success, false otherwise.

Defined in[edit]

libraries/joomla/form/form.php

Importing[edit]

jimport( 'joomla.form.form' );

Source Body[edit]

public function loadFieldType($type, $new = true)
{
        $key    = md5($type);
        $class  = 'JFormField'.ucfirst($type);

        // Return the field object if it already exists and we don't need a new one.
        if (isset($this->_fieldTypes[$key]) && $new === false) {
                return $this->_fieldTypes[$key];
        }

        if (!class_exists('JFormField')) {
                jimport('joomla.form.formfield');
        }

        if (!class_exists('JFormFieldList')) {
                require_once dirname(__FILE__).'/fields/list.php';
        }

        if (!class_exists($class)) {
                $paths = self::addFieldPath();

                // If the type is complex, add the base type to the paths.
                if ($pos = strpos($type, '_')) {
                        // Add the complex type prefix to the paths.
                        for ($i = 0, $n = count($paths); $i < $n; $i++) {
                                // Derive the new path.
                                $path = $paths[$i].DS.strtolower(substr($type, 0, $pos));

                                // If the path does not exist, add it.
                                if (!in_array($path, $paths)) {
                                        array_unshift($paths, $path);
                                }
                        }

                        // Break off the end of the complex type.
                        $type = substr($type, $pos+1);
                }

                // Try to find the field file.
                jimport('joomla.filesystem.path');
                if ($file = JPath::find($paths, strtolower($type).'.php')) {
                        require_once $file;
                } else {
                        return false;
                }

                // Check once and for all if the class exists.
                if (!class_exists($class)) {
                        return false;
                }
        }

        // Instantiate a new field object.
        $this->_fieldTypes[$key] = new $class($this);

        return $this->_fieldTypes[$key];
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples[edit]

<CodeExamplesForm />