Skip to main content

VendorCountryField Field Type

VendorCountryField extends Joomla's ListField to render a <select> containing only the countries that are linked to at least one vendor through the vendor's address record. It is the vendor-side counterpart to ManufacturerCountryField, intended for filter forms where showing the full country list would include irrelevant options. The stored value is the integer primary key j2commerce_country_id.

Class Reference

PropertyValue
ClassVendorCountryField
NamespaceJ2Commerce\Component\J2commerce\Administrator\Field
Fileadministrator/components/com_j2commerce/src/Field/VendorCountryField.php
ExtendsJoomla\CMS\Form\Field\ListField
Field type tokenVendorCountry
Since6.0.7

Database Query

The field performs a three-way join to find distinct countries used by any vendor:

SELECT DISTINCT c.j2commerce_country_id AS value, c.country_name AS text
FROM #__j2commerce_vendors AS v
INNER JOIN #__j2commerce_addresses AS a
ON a.j2commerce_address_id = v.address_id
INNER JOIN #__j2commerce_countries AS c
ON c.j2commerce_country_id = a.country_id
WHERE c.enabled = 1
AND a.country_id IS NOT NULL
AND a.country_id != ''
ORDER BY c.country_name ASC

The stored value is j2commerce_country_id (integer), consistent with CountryField.

XML Usage

Basic Usage

<field
name="vendor_country_id"
type="VendorCountry"
label="COM_MYPLUGIN_FIELD_COUNTRY_LABEL"
description="COM_MYPLUGIN_FIELD_COUNTRY_DESC"
/>

With "All Countries" Placeholder

<field
name="vendor_country_id"
type="VendorCountry"
label="COM_MYPLUGIN_FIELD_COUNTRY_LABEL">
<option value="0">JOPTION_SELECT_COUNTRY</option>
</field>

Supported XML Attributes

AttributeTypeDescription
namestringForm field name
labelstringLanguage key for the field label
descriptionstringLanguage key for the tooltip
defaultintegerDefault selected country ID
requiredbooleanWhether a value is required
multiplebooleanAllow multi-select
classstringAdditional CSS classes on the <select> element

Usage in Plugin Forms

The primary use case is a report or filter plugin that scopes data to a vendor's country:

<?xml version="1.0" encoding="utf-8"?>
<form addfieldprefix="J2Commerce\Component\J2commerce\Administrator\Field">

<fieldset name="filters">

<field
name="vendor_country_id"
type="VendorCountry"
label="COM_MYPLUGIN_FILTER_COUNTRY_LABEL"
description="COM_MYPLUGIN_FILTER_COUNTRY_DESC"
default="0">
<option value="0">JOPTION_SELECT_COUNTRY</option>
</field>

</fieldset>

</form>

Difference from CountryField

FieldOptions shownUse when
CountryFieldAll enabled countriesYou need the full country list
VendorCountryFieldOnly countries with vendorsFiltering vendor-related data

Source Code

// File: administrator/components/com_j2commerce/src/Field/VendorCountryField.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 VendorCountryField extends ListField
{
protected $type = 'VendorCountry';

public function getOptions(): array
{
$options = parent::getOptions();

$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db->getQuery(true)
->select('DISTINCT ' . $db->quoteName('c.j2commerce_country_id', 'value'))
->select($db->quoteName('c.country_name', 'text'))
->from($db->quoteName('#__j2commerce_vendors', 'v'))
->join(
'INNER',
$db->quoteName('#__j2commerce_addresses', 'a'),
$db->quoteName('a.j2commerce_address_id') . ' = ' . $db->quoteName('v.address_id')
)
->join(
'INNER',
$db->quoteName('#__j2commerce_countries', 'c'),
$db->quoteName('c.j2commerce_country_id') . ' = ' . $db->quoteName('a.country_id')
)
->where($db->quoteName('c.enabled') . ' = 1')
->where($db->quoteName('a.country_id') . ' IS NOT NULL')
->where($db->quoteName('a.country_id') . ' != ' . $db->quote(''))
->order($db->quoteName('c.country_name') . ' ASC');

$db->setQuery($query);

foreach ($db->loadObjectList() as $country) {
$options[] = HTMLHelper::_('select.option', $country->value, $country->text);
}

return $options;
}
}

Notes

  • Only countries where c.enabled = 1 AND the vendor has a non-empty address_id with a valid country_id are included.
  • A country appears only once (DISTINCT) even if multiple vendors share the same country.
  • If no vendors exist or none have country addresses set, the dropdown will contain only any static child <option> elements you define.
  • The query structure is identical to ManufacturerCountryField, with #__j2commerce_vendors substituted for #__j2commerce_manufacturers.