Back to Select class

Method genericlist

public static string
genericlist
(mixed $data, mixed $name, mixed $attribs = null, mixed $optKey = 'value', mixed $optText = 'text', mixed $selected = null, mixed $idtag = false, mixed $translate = false)
Generates an HTML selection list.
Parameters
  • array $data An array of objects, arrays, or scalars.
  • string $name The value of the HTML name attribute.
  • mixed $attribs Additional HTML attributes for the `<select>` tag. This can be an array of attributes, or an array of options. Treated as options if it is the last argument passed. Valid options are: Format options, see {@see \Joomla\CMS\HTML\HTMLHelper:: $formatOptions }. Selection options, see {@see \Joomla\CMS\HTML\Helpers\JHtmlSelect::options()}. list.attr, string|array: Additional attributes for the select element. id, string: Value to use as the select element id attribute. Defaults to the same as the name. list.select, string|array: Identifies one or more option elements to be selected, based on the option key values.
  • string $optKey The name of the object variable for the option value. If set to null, the index of the value array is used.
  • string $optText The name of the object variable for the option text.
  • mixed $selected The key that is selected (accepts an array or a string).
  • mixed $idtag Value of the field id or null by default
  • bool $translate True to translate
Returns
  • string HTML for the select list.
Since
  • 1.5
Class: Select
Project: Joomla

Method genericlist - Source code

/**
 * Generates an HTML selection list.
 *
 * @param   array    $data       An array of objects, arrays, or scalars.
 * @param   string   $name       The value of the HTML name attribute.
 * @param   mixed    $attribs    Additional HTML attributes for the `<select>` tag. This
 *                               can be an array of attributes, or an array of options. Treated as options
 *                               if it is the last argument passed. Valid options are:
 *                               Format options, see {@see HTMLHelper::$formatOptions}.
 *                               Selection options, see {@see JHtmlSelect::options()}.
 *                               list.attr, string|array: Additional attributes for the select
 *                               element.
 *                               id, string: Value to use as the select element id attribute.
 *                               Defaults to the same as the name.
 *                               list.select, string|array: Identifies one or more option elements
 *                               to be selected, based on the option key values.
 * @param   string   $optKey     The name of the object variable for the option value. If
 *                               set to null, the index of the value array is used.
 * @param   string   $optText    The name of the object variable for the option text.
 * @param   mixed    $selected   The key that is selected (accepts an array or a string).
 * @param   mixed    $idtag      Value of the field id or null by default
 * @param   boolean  $translate  True to translate
 *
 * @return  string  HTML for the select list.
 *
 * @since   1.5
 */
public static function genericlist($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false, $translate = false)
{
    // Set default options
    $options = array_merge(HTMLHelper::$formatOptions, array('format.depth' => 0, 'id' => false));
    if (is_array($attribs) && func_num_args() === 3) {
        // Assume we have an options array
        $options = array_merge($options, $attribs);
    } else {
        // Get options from the parameters
        $options['id'] = $idtag;
        $options['list.attr'] = $attribs;
        $options['list.translate'] = $translate;
        $options['option.key'] = $optKey;
        $options['option.text'] = $optText;
        $options['list.select'] = $selected;
    }
    $attribs = '';
    if (isset($options['list.attr'])) {
        if (is_array($options['list.attr'])) {
            $attribs = ArrayHelper::toString($options['list.attr']);
        } else {
            $attribs = $options['list.attr'];
        }
        if ($attribs !== '') {
            $attribs = ' ' . $attribs;
        }
    }
    $id = $options['id'] !== false ? $options['id'] : $name;
    $id = str_replace(array('[', ']', ' '), '', $id);
    // If the selectbox contains "form-select-color-state" then load the JS file
    if (strpos($attribs, 'form-select-color-state') !== false) {
        Factory::getDocument()->getWebAssetManager()->registerScript('webcomponent.select-colour-es5', 'system/fields/select-colour-es5.min.js', ['dependencies' => ['wcpolyfill']], ['defer' => true, 'nomodule' => true])->registerAndUseScript('webcomponent.select-colour', 'system/fields/select-colour.min.js', ['dependencies' => ['webcomponent.select-colour-es5']], ['type' => 'module']);
    }
    $baseIndent = str_repeat($options['format.indent'], $options['format.depth']++);
    $html = $baseIndent . '<select' . ($id !== '' ? ' id="' . $id . '"' : '') . ' name="' . $name . '"' . $attribs . '>' . $options['format.eol'] . static::options($data, $options) . $baseIndent . '</select>' . $options['format.eol'];
    return $html;
}