Creating a custom form field type for Joomla XML fieldset

What?
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.

Why?
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:
copyraw
SELECT * FROM #__projects ORDER BY name

-- yields all projects irrespective of which user is logged in
  1.  SELECT * FROM #__projects ORDER BY name 
  2.   
  3.  -- yields all projects irrespective of which user is logged in 
What I want:
copyraw
SELECT * FROM #__projects WHERE user_id=<logged_in_user> ORDER BY name
  1.  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.

How?
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):
    copyraw
    <?php
    /**
     * @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> http://www.joellipman.com
     */
    
    defined('JPATH_BASE') or die;
    /*
    jimport('joomla.html.html');
    jimport('joomla.form.formfield');
    jimport('joomla.form.helper');
    */
    JFormHelper::loadFieldClass('sql');
    
    /**
     * 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;
            $db->setQuery($query);
            $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);
        }
    }
    ?>
    1.  <?php 
    2.  /** 
    3.   * @version     1.0 
    4.   * @package     my_component 
    5.   * @copyright   Copyright (C) 2013. All rights reserved. 
    6.   * @license     GNU General Public License version 2 or later; see LICENSE.txt 
    7.   * @author      <your_name> http://www.joellipman.com 
    8.   */ 
    9.   
    10.  defined('JPATH_BASE') or die; 
    11.  /* 
    12.  jimport('joomla.html.html')
    13.  jimport('joomla.form.formfield')
    14.  jimport('joomla.form.helper')
    15.  */ 
    16.  JFormHelper::loadFieldClass('sql')
    17.   
    18.  /** 
    19.   * Supports an HTML select list of options driven by SQL 
    20.   */ 
    21.  class JFormFieldUserprojects extends JFormFieldSQL 
    22.  { 
    23.      /** 
    24.       * The form field type. 
    25.       */ 
    26.      public $type = 'userprojects'
    27.   
    28.      /** 
    29.       * Overrides parent's getinput method 
    30.       */ 
    31.      protected function getInput() 
    32.      { 
    33.          // Initialize variables. 
    34.          $html = array()
    35.          $html[] = '<select id="form_user_projects" name="form_user_projects">'
    36.   
    37.          // Load user 
    38.          $user =& JFactory::getUser()
    39.          $user_id = $user->get('id')
    40.   
    41.          // do the SQL 
    42.          $db =& JFactory::getDbo()
    43.          $query="SELECT 0 AS id, '- default -' AS name UNION ALL SELECT id, name FROM #__projects WHERE created_by=".$user_id; 
    44.          $db->setQuery($query)
    45.          $rows = $db->loadObjectList()
    46.   
    47.          // iterate through returned rows 
    48.          foreach( $rows as $row ){ 
    49.                  $this_project_id = $row->id; 
    50.                  $this_project_name = $row->name; 
    51.                  $html[] = '<option value="'.$this_project_id.'">'.$this_project_name.'</option>'
    52.          } 
    53.   
    54.          // close the HTML select options 
    55.          $html[] = '</select>'
    56.   
    57.          // return the HTML 
    58.          return implode($html)
    59.      } 
    60.  } 
    61.  ?> 
  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:
    copyraw
    <field
            name="user_project"
            type="userprojects"
            label="COM_MYCOMPONENT_LABEL"
            description="COM_MYCOMPONENT_DESC"
            required="required" />
    1.  <field 
    2.          name="user_project" 
    3.          type="userprojects" 
    4.          label="COM_MYCOMPONENT_LABEL" 
    5.          description="COM_MYCOMPONENT_DESC" 
    6.          required="required" /> 

Additional
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.

Category: Joomla :: Article: 530

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

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:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.