I created module and want to display on backend in module a list of checkboxes from DB table.
I created custom field and placed it to fields/sqlcheckboxes.php.
Code for file fields/sqlcheckboxes.php:
Code: Select all
<?php
/**
* @package Joomla.Platform
* @subpackage Form\Field
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* @author 3ehrang <[email protected]>
*/
namespace VPJoomla\Module\Faq\Form\Field;
use Joomla\CMS\Form\Field\CheckboxesField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
defined('_JEXEC') or die;
/**
* Supports a custom SQL select list for module settings.
*
* @since Joomla 4.0
*/
class SQLCheckBoxesField extends CheckboxesField
{
/**
* The form field type.
*
* @var string
* @since Joomla 4.0
*/
protected $type = 'SQLCheckBoxes';
/**
* The keyField.
*
* @var string
* @since Joomla 4.0
*/
protected $keyField;
/**
* The valueField.
*
* @var string
* @since Joomla 4.0
*/
protected $valueField;
/**
* The translate.
*
* @var boolean
* @since Joomla 4.0
*/
protected $translate = false;
/**
* The query.
*
* @var string
* @since Joomla 4.0
*/
protected $query;
/**
* Method to get the custom field options.
* Use the query attribute to supply a query to generate the list.
*
* @return array The field option objects.
*
* @since Joomla 4.0
*/
protected function getOptions(): array
{
$options = [];
// Get the database object.
$db = $this->container->get('DatabaseDriver');
// Set the query and get the result list.
$db->setQuery($this->query);
$items = $db->loadObjectList();
// Build the field options.
if (!empty($items)) {
foreach ($items as $item) {
$key = $this->keyField;
$value = $this->valueField;
$optionText = $this->translate ? Text::_($item->$value) : $item->$value;
$options[] = HTMLHelper::_('select.option', $item->$key, $optionText);
}
}
return $options;
}
/**
* Method to setup the field.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
*
* @return boolean True on success.
*
* @since Joomla 4.0
*/
public function setup(\SimpleXMLElement $element, $value, $group = null): bool
{
$return = parent::setup($element, $value, $group);
if ($return) {
$this->keyField = (string) ($element['key_field'] ?? 'value');
$this->valueField = (string) ($element['value_field'] ?? $element['name']);
$this->translate = (bool) ($element['translate'] ?? false);
$this->query = (string) $element['query'];
}
return $return;
}
}
Code: Select all
<fields name="faq" addfieldpath="/modules/mod_faq/fields">
<field name="title"
id="faq-item"
type="SQLCheckBoxes"
translate="types"
query="SELECT id, title FROM #__faq_item WHERE state = 1"
key_field="id"
value_field="title"
required = "true"
multiple="true"
label="MOD_FAQ_SELECT_QUESTION_LABEL"
description="MOD_FAQ_SELECT_QUESTION_DESC"
class="check-dox-faq-item"/>
</fields>
Help me please to fix this. Thanks!