Skip to main content

ManufacturerCountryField Field Type

ManufacturerCountryField extends Joomla's ListField to render a <select> containing only the countries that are linked to at least one manufacturer through the manufacturer's address record. It is purpose-built for filter forms (such as report plugins or admin list filters) where a full country list would include irrelevant options. The stored value is the integer primary key j2commerce_country_id.

Class Reference

PropertyValue
ClassManufacturerCountryField
NamespaceJ2Commerce\Component\J2commerce\Administrator\Field
Fileadministrator/components/com_j2commerce/src/Field/ManufacturerCountryField.php
ExtendsJoomla\CMS\Form\Field\ListField
Field type tokenManufacturerCountry
Since6.0.6

Database Query

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

SELECT DISTINCT c.j2commerce_country_id AS value, c.country_name AS text
FROM #__j2commerce_manufacturers AS m
INNER JOIN #__j2commerce_addresses AS a
ON a.j2commerce_address_id = m.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="manufacturer_country_id"
type="ManufacturerCountry"
label="COM_MYPLUGIN_FIELD_COUNTRY_LABEL"
description="COM_MYPLUGIN_FIELD_COUNTRY_DESC"
/>

With "All Countries" Placeholder

A typical filter form pattern:

<field
name="manufacturer_country_id"
type="ManufacturerCountry"
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 plugin that filters data by manufacturer country:

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

<fieldset name="filters">

<field
name="manufacturer_country_id"
type="ManufacturerCountry"
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
ManufacturerCountryFieldOnly countries with manufacturersFiltering manufacturer-related data

Source Code

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

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_manufacturers', 'm'))
->join(
'INNER',
$db->quoteName('#__j2commerce_addresses', 'a'),
$db->quoteName('a.j2commerce_address_id') . ' = ' . $db->quoteName('m.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 manufacturer has a non-empty address_id with a valid country_id are included.
  • A country appears only once (DISTINCT) even if multiple manufacturers share the same country.
  • If no manufacturers exist or none have country addresses, the dropdown will contain only any static child <option> elements you add.
  • The since version for this field is 6.0.6, one minor version earlier than most other entity fields.