Difference between revisions of "How to add custom filters to components"
From Joomla! Documentation
Doub1ejack (talk | contribs) (Created page with "== Overview == This how-to is intended to give a decent description on how to add dropdown form elements to the backend admin page for a component. I worked out the steps primar...") |
Doub1ejack (talk | contribs) |
||
Line 10: | Line 10: | ||
== Extend JFormFieldList (models / fields / fieldname.php) == | == Extend JFormFieldList (models / fields / fieldname.php) == | ||
− | test | + | Frequently your filter fields will be very basic; probably simply a dropdown list of one of the columns being displayed on the current page. Regardless, you will need to create your own field element. This really isn't a big deal - this file will probably be less than 70 lines, including comments. The code below will generate the dropdown element to filter by companies. For more information and complex examples on creating this class see [[Creating_a_custom_form_field_type]]. |
+ | |||
+ | My 'companies' dropdown element: | ||
+ | <source lang="php"> | ||
+ | <?php | ||
+ | |||
+ | defined('JPATH_BASE') or die; | ||
+ | |||
+ | jimport('joomla.html.html'); | ||
+ | jimport('joomla.form.formfield'); | ||
+ | jimport('joomla.form.helper'); | ||
+ | JFormHelper::loadFieldClass('list'); | ||
+ | |||
+ | /** | ||
+ | * Custom Field class for the Joomla Framework. | ||
+ | * | ||
+ | * @package Joomla.Administrator | ||
+ | * @subpackage com_my | ||
+ | * @since 1.6 | ||
+ | */ | ||
+ | class JFormFieldMyCompany extends JFormFieldList | ||
+ | { | ||
+ | /** | ||
+ | * The form field type. | ||
+ | * | ||
+ | * @var string | ||
+ | * @since 1.6 | ||
+ | */ | ||
+ | protected $type = 'MyCompany'; | ||
+ | |||
+ | /** | ||
+ | * Method to get the field options. | ||
+ | * | ||
+ | * @return array The field option objects. | ||
+ | * @since 1.6 | ||
+ | */ | ||
+ | public function getOptions() | ||
+ | { | ||
+ | // Initialize variables. | ||
+ | $options = array(); | ||
+ | |||
+ | $db = JFactory::getDbo(); | ||
+ | $query = $db->getQuery(true); | ||
+ | |||
+ | $query->select('id As value, name As text'); | ||
+ | $query->from('#__my_companies AS a'); | ||
+ | $query->order('a.name'); | ||
+ | $query->where('state = 1'); | ||
+ | |||
+ | // Get the options. | ||
+ | $db->setQuery($query); | ||
+ | |||
+ | $options = $db->loadObjectList(); | ||
+ | |||
+ | // Check for a database error. | ||
+ | if ($db->getErrorNum()) { | ||
+ | JError::raiseWarning(500, $db->getErrorMsg()); | ||
+ | } | ||
+ | |||
+ | return $options; | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Obviously, there is only one section here that needs to be customized: | ||
+ | |||
+ | <source lang="php"> | ||
+ | $query->select('id As value, name As text'); | ||
+ | $query->from('#__my_companies AS a'); | ||
+ | $query->order('a.name'); | ||
+ | $query->where('state = 1'); | ||
+ | </source> | ||
+ | |||
+ | Change the database name and adjust the sql as necessary. This will produce the text and values for the select element options. I highly recommend not changing the value and text names. Although I did not test this theory, it seems likely that ancestor classes are expecting these names as keys in the resulting array. | ||
== Modify Model (models / zzz.php) == | == Modify Model (models / zzz.php) == |
Revision as of 18:27, 19 January 2012
This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Overview[edit]
This how-to is intended to give a decent description on how to add dropdown form elements to the backend admin page for a component. I worked out the steps primarily on my own by dissecting/replicating the Banners component. In particular, the 'banners' section of com_banners - not! to be confused with 'banner').
This tutorial was built on code tested in Joomla 1.7 only.
In general, the process of adding these fields breaks down into the following steps:
- Creating the class that will generate the form element
- Adapting the view to display the form element
- Adapting the model to retrieve data from DB & to set submission values in the Joomla 'state'
Extend JFormFieldList (models / fields / fieldname.php)[edit]
Frequently your filter fields will be very basic; probably simply a dropdown list of one of the columns being displayed on the current page. Regardless, you will need to create your own field element. This really isn't a big deal - this file will probably be less than 70 lines, including comments. The code below will generate the dropdown element to filter by companies. For more information and complex examples on creating this class see Creating_a_custom_form_field_type.
My 'companies' dropdown element:
<?php
defined('JPATH_BASE') or die;
jimport('joomla.html.html');
jimport('joomla.form.formfield');
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
/**
* Custom Field class for the Joomla Framework.
*
* @package Joomla.Administrator
* @subpackage com_my
* @since 1.6
*/
class JFormFieldMyCompany extends JFormFieldList
{
/**
* The form field type.
*
* @var string
* @since 1.6
*/
protected $type = 'MyCompany';
/**
* Method to get the field options.
*
* @return array The field option objects.
* @since 1.6
*/
public function getOptions()
{
// Initialize variables.
$options = array();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id As value, name As text');
$query->from('#__my_companies AS a');
$query->order('a.name');
$query->where('state = 1');
// Get the options.
$db->setQuery($query);
$options = $db->loadObjectList();
// Check for a database error.
if ($db->getErrorNum()) {
JError::raiseWarning(500, $db->getErrorMsg());
}
return $options;
}
}
Obviously, there is only one section here that needs to be customized:
$query->select('id As value, name As text');
$query->from('#__my_companies AS a');
$query->order('a.name');
$query->where('state = 1');
Change the database name and adjust the sql as necessary. This will produce the text and values for the select element options. I highly recommend not changing the value and text names. Although I did not test this theory, it seems likely that ancestor classes are expecting these names as keys in the resulting array.
Modify Model (models / zzz.php)[edit]
test formatting
Modify View (views / zzz / view.html.php)[edit]
test formatting
Modify Template (views / zzz / tmpl / default.php)[edit]
test formatting