Creating a custom form field type for Joomla XML fieldset

I'm writing a Joomla 2.5.x component for logging time and projects and need a dropdown to have selectable options restricted to the logged-in user. This is specified in the XML file of the custom Joomla component and needs some SQL dependent on some dynamic variables.

In Joomla, the XML type of "sql" is extremely limited. The component I'm making has to ensure user's can only see their own projects and not everyone's:
SELECT * FROM #__projects ORDER BY name

-- yields all projects irrespective of which user is logged in
What I want:
SELECT * FROM #__projects WHERE user_id=<logged_in_user> ORDER BY name

Please Note: This article focuses on a front-end form dropdown. If you would like to see how to do modals for the admin panel, see my article Joomla article modal with clear button for Joomla article selection.

We have to create a custom form field type. In the following example, I'm going to create a dropdown which selects all projects associated with a particular user.

Method #1:
  1. Important! It took me two days to realize that underscores are not accepted in the type name (despite copying "custom_field.php" and wanting to call my type "user_projects")!!! My recommendation is to remove any non-alphanumeric characters...

    In your component, create a PHP file in /administrator/models/fields/<name_of_file.php> (eg. "userprojects.php"). Rename "userprojects" with the name of your field type):
     * @version     1.0
     * @package     my_component
     * @copyright   Copyright (C) 2013. All rights reserved.
     * @license     GNU General Public License version 2 or later; see LICENSE.txt
     * @author      <your_name>
    defined('JPATH_BASE') or die;
     * Supports an HTML select list of options driven by SQL
    class JFormFieldUserprojects extends JFormFieldSQL
         * The form field type.
        public $type = 'userprojects';
         * Overrides parent's getinput method
        protected function getInput()
            // Initialize variables.
            $html = array();
            $html[] = '<select id="form_user_projects" name="form_user_projects">';
            // Load user
            $user =& JFactory::getUser();
            $user_id = $user->get('id');
            // do the SQL
            $db =& JFactory::getDbo();
            $query="SELECT 0 AS id, '- default -' AS name UNION ALL SELECT id, name FROM #__projects WHERE created_by=".$user_id;
            $rows = $db->loadObjectList();
            // iterate through returned rows
            foreach( $rows as $row ){
                    $this_project_id = $row->id;
                    $this_project_name = $row->name;
                    $html[] = '<option value="'.$this_project_id.'">'.$this_project_name.'</option>';
            // close the HTML select options
            $html[] = '</select>';
            // return the HTML
            return implode($html);
  2. Now use the type in the XML form file (in my case for tasks and projects » /site/models/forms/task.xml) and specify the type:
            required="required" />

If there is something wrong with the code, it will simply display a text input field.

Also there appears to be an issue if you switch on debug mode and navigate to the admin page which uses this field type... But the important part is that it worked on the front-end "site". I will update this article if I ever find out why.

Also, not sure if you need the jimport lines (which I commented out in the code above) but add if you want.

Related Articles

Joes Revolver Map


Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Donate to Joel Lipman via PayPal

Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.