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:
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.
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:
- 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):<?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); } } ?> - 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:
<field name="user_project" type="userprojects" label="COM_MYCOMPONENT_LABEL" description="COM_MYCOMPONENT_DESC" 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.
Discussion
Comments
Questions, corrections and useful additions are reviewed before appearing here.
No comments yet. Start the discussion.