CountryField Field Type
CountryField extends Joomla's ListField to render a <select> element populated with every enabled country from #__j2commerce_countries. Options are ordered alphabetically by country name. The stored value is the integer primary key j2commerce_country_id.
Class Reference
| Property | Value |
|---|---|
| Class | CountryField |
| Namespace | J2Commerce\Component\J2commerce\Administrator\Field |
| File | administrator/components/com_j2commerce/src/Field/CountryField.php |
| Extends | Joomla\CMS\Form\Field\ListField |
| Field type token | Country |
| Since | 6.0.7 |
Database Query
The field queries #__j2commerce_countries and returns only enabled rows:
SELECT j2commerce_country_id AS value, country_name AS text
FROM #__j2commerce_countries
WHERE enabled = 1
ORDER BY country_name ASC
The stored value is j2commerce_country_id (integer).
XML Usage
Basic Usage
<field
name="country_id"
type="Country"
label="COM_MYPLUGIN_FIELD_COUNTRY_LABEL"
description="COM_MYPLUGIN_FIELD_COUNTRY_DESC"
/>
With Placeholder Option
Use a child <option> element with an empty value to add a "select" placeholder. This is merged with the database options by the parent ListField.
<field
name="country_id"
type="Country"
label="COM_MYPLUGIN_FIELD_COUNTRY_LABEL"
description="COM_MYPLUGIN_FIELD_COUNTRY_DESC"
required="true">
<option value="">COM_MYPLUGIN_SELECT_COUNTRY</option>
</field>
Supported XML Attributes
All standard ListField attributes are supported. The most relevant ones for this field:
| Attribute | Type | Description |
|---|---|---|
name | string | Form field name and HTML name attribute |
label | string | Language key for the field label |
description | string | Language key for the tooltip |
default | string/integer | Default selected value (country ID) |
required | boolean | Whether a non-empty value is required |
multiple | boolean | Allow multi-select |
class | string | Additional CSS classes on the <select> element |
onchange | string | Inline JavaScript for the onchange event |
Usage in Plugin Forms
Add addfieldprefix on the <form> root element to tell Joomla where to find the custom field class:
<?xml version="1.0" encoding="utf-8"?>
<form addfieldprefix="J2Commerce\Component\J2commerce\Administrator\Field">
<fieldset name="basic">
<field
name="country_id"
type="Country"
label="COM_MYPLUGIN_FIELD_COUNTRY_LABEL"
description="COM_MYPLUGIN_FIELD_COUNTRY_DESC"
default="0">
<option value="0">JOPTION_SELECT_COUNTRY</option>
</field>
</fieldset>
</form>
In a Plugin Manifest
Declare addfieldprefix in the plugin's <config> block:
<config>
<fields name="params" addfieldprefix="J2Commerce\Component\J2commerce\Administrator\Field">
<fieldset name="basic">
<field
name="country_id"
type="Country"
label="COM_MYPLUGIN_FIELD_COUNTRY_LABEL"
/>
</fieldset>
</fields>
</config>
Source Code
// File: administrator/components/com_j2commerce/src/Field/CountryField.php
declare(strict_types=1);
namespace J2Commerce\Component\J2commerce\Administrator\Field;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\ListField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseInterface;
class CountryField extends ListField
{
protected $type = 'Country';
public function getOptions(): array
{
$options = parent::getOptions();
$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db->getQuery(true)
->select([
$db->quoteName('j2commerce_country_id', 'value'),
$db->quoteName('country_name', 'text')
])
->from($db->quoteName('#__j2commerce_countries'))
->where($db->quoteName('enabled') . ' = 1')
->order($db->quoteName('country_name') . ' ASC');
$db->setQuery($query);
$countries = $db->loadObjectList();
foreach ($countries as $country) {
$options[] = HTMLHelper::_('select.option', $country->value, $country->text);
}
return $options;
}
}
Notes
- Only countries with
enabled = 1appear in the dropdown. Disabled countries are hidden. - The stored value is the integer
j2commerce_country_id, not an ISO code. - To trigger a zone dropdown update when the country changes, pair this field with
ZoneFieldusing thecountry_fieldattribute. See ZoneField.
Related
- ZoneField — Linked zone dropdown with AJAX country filtering
- GeoZoneField — Geo-zone dropdown for tax and shipping rules
- ManufacturerCountryField — Countries filtered to those with manufacturers
- VendorCountryField — Countries filtered to those with vendors