Back to SqlField class

Method getOptions

protected array
getOptions
()
Method to get the custom field options.
Returns
  • array The field option objects.
Since
  • 1.7.0
Class: SqlField
Project: Joomla

Method getOptions - Source code

/**
 * 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   1.7.0
 */
protected function getOptions()
{
    $options = array();
    // Initialize some field attributes.
    $key = $this->keyField;
    $value = $this->valueField;
    $header = $this->header;
    if ($this->query) {
        // Get the database object.
        $db = Factory::getDbo();
        // Set the query and get the result list.
        $db->setQuery($this->query);
        try {
            $items = $db->loadObjectList();
        } catch (ExecutionFailureException $e) {
            Factory::getApplication()->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
        }
    }
    // Add header.
    if (!empty($header)) {
        $header_title = Text::_($header);
        $options[] = HTMLHelper::_('select.option', '', $header_title);
    }
    // Build the field options.
    if (!empty($items)) {
        foreach ($items as $item) {
            if ($this->translate == true) {
                $options[] = HTMLHelper::_('select.option', $item->{$key}, Text::_($item->{$value}));
            } else {
                $options[] = HTMLHelper::_('select.option', $item->{$key}, $item->{$value});
            }
        }
    }
    // Merge any additional options in the XML definition.
    $options = array_merge(parent::getOptions(), $options);
    return $options;
}