Actions

API16

JHtmlSelect/options

From Joomla! Documentation

< API16:JHtmlSelect
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Generates the option tags for an HTML select list (with no select tag surrounding the options).

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

static options($arr, $optKey= 'value', $optText= 'text', $selected=null, $translate=false)
Parameter Name Default Value Description
$arr An array of objects, arrays, or values.
$optKey 'value' If a string, this is the name of the object variable for the option value. If null, the index of the array of objects is used. If an array, this is a set of options, as key/value pairs. Valid options are:
$optText 'text'
$selected null
$translate false

Returns

string HTML for the select list

Defined in

libraries/joomla/html/html/select.php

Importing

jimport( 'joomla.html.html.select' );

Source Body

public static function options(
        $arr, $optKey = 'value', $optText = 'text', $selected = null, $translate = false
) {
        $options = array_merge(
                JHtml::$formatOptions,
                self::$_optionDefaults['option'],
                array(
                        'format.depth' => 0,
                        'groups' => true,
                        'list.select' => null,
                        'list.translate' => false,
                )
        );
        if (is_array($optKey)) {
                // Set default options and overwrite with anything passed in
                $options = array_merge($options, $optKey);
        } else {
                // Get options from the parameters
                $options['option.key'] = $optKey;
                $options['option.text'] = $optText;
                $options['list.select'] = $selected;
                $options['list.translate'] = $translate;
        }
 
        $html = '';
        $baseIndent = str_repeat($options['format.indent'], $options['format.depth']);
 
        foreach ($arr as $elementKey => &$element)
        {
                $attr = '';
                $extra = '';
                $label = '';
                $id = '';
                if (is_array($element))
                {
                        $key = $options['option.key'] === null
                                ? $elementKey : $element[$options['option.key']];
                        $text = $element[$options['option.text']];
                        if (isset($element[$options['option.attr']])) {
                                $attr = $element[$options['option.attr']];
                        }
                        if (isset($element[$options['option.id']])) {
                                $id = $element[$options['option.id']];
                        }
                        if (isset($element[$options['option.label']])) {
                                $label = $element[$options['option.label']];
                        }
                        if (isset($element[$options['option.disable']]) && $element[$options['option.disable']]) {
                                $extra .= ' disabled="disabled"';
                        }
                } elseif (is_object($element)) {
                        $key = $options['option.key'] === null
                                ? $elementKey : $element->$options['option.key'];
                        $text = $element->$options['option.text'];
                        if (isset($element->$options['option.attr'])) {
                                $attr = $element->$options['option.attr'];
                        }
                        if (isset($element->$options['option.id'])) {
                                $id = $element->$options['option.id'];
                        }
                        if (isset($element->$options['option.label'])) {
                                $label = $element->$options['option.label'];
                        }
                        if (isset($element->$options['option.disable']) && $element->$options['option.disable']) {
                                $extra .= ' disabled="disabled"';
                        }
                } else {
                        // This is a simple associative array
                        $key = $elementKey;
                        $text = $element;
                }
 
                /*
                 * The use of options that contain optgroup HTML elements was
                 * somewhat hacked for J1.5. J1.6 introduces the grouplist() method
                 * to handle this better. The old solution is retained through the
                 * "groups" option, which defaults true in J1.6, but should be
                 * deprecated at some point in the future.
                 */
                $key = (string) $key;
                if ($options['groups'] && $key == '<OPTGROUP>') {
                        $html .= $baseIndent . '<optgroup label="'
                                . ($options['list.translate'] ? JText::_($text) : $text)
                                . '">' . $options['format.eol'];
                        $baseIndent = str_repeat($options['format.indent'], ++$options['format.depth']);
                } else if ($options['groups'] && $key == '</OPTGROUP>') {
                        $baseIndent = str_repeat($options['format.indent'], --$options['format.depth']);
                        $html .= $baseIndent . '</optgroup>' . $options['format.eol'];
                } else {
                        // if no string after hypen - take hypen out
                        $splitText = explode(' - ', $text, 2);
                        $text = $splitText[0];
                        if (isset($splitText[1])) {
                                $text .= ' - ' . $splitText[1];
                        }
 
                        if ($options['list.translate'] && !empty($label)) {
                                $label = JText::_($label);
                        }
                        if ($options['option.label.toHtml']) {
                                $label = htmlentities($label);
                        }
                        if (is_array($attr)) {
                                $attr = JArrayHelper::toString($attr);
                        } else {
                                $attr = trim($attr);
                        }
                        $extra = ($id ? ' id="' . $id . '"' : '')
                                . ($label ? ' label="' . $label . '"' : '')
                                . ($attr ? ' ' . $attr : '')
                                . $extra
                        ;
                        if (is_array($options['list.select']))
                        {
                                foreach ($options['list.select'] as $val)
                                {
                                        $key2 = is_object($val) ? $val->$options['option.key'] : $val;
                                        if ($key == $key2) {
                                                $extra .= ' selected="selected"';
                                                break;
                                        }
                                }
                        } elseif ((string)$key == (string)$options['list.select']) {
                                $extra .= ' selected="selected"';
                        }
 
                        if ($options['list.translate']) {
                                $text = JText::_($text);
                        }
 
                        // Generate the option, encoding as required
                        $html .= $baseIndent . '<option value="'
                                . ($options['option.key.toHtml'] ? htmlspecialchars($key, ENT_COMPAT, 'UTF-8') : $key) . '"'
                                . $extra . '>'
                                . ($options['option.text.toHtml'] ? htmlentities(html_entity_decode($text), ENT_COMPAT, 'UTF-8') : $text)
                                . '</option>'
                                . $options['format.eol']
                        ;
                }
        }
 
        return $html;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />