4.7.x form.inc theme_select($element)
5.x form.inc theme_select($element)
6.x form.inc theme_select($element)
7.x form.inc theme_select($variables)

Returns HTML for a select form element.

It is possible to group options together; to do this, change the format of $options to an associative array in which the keys are group labels, and the values are associative arrays in the normal $options format.

Parameters

$variables: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #title, #value, #options, #description, #extra, #multiple, #required, #name, #attributes, #size.

Related topics

1 theme call to theme_select()
system_element_info in modules/system/system.module
Implements hook_element_info().

File

includes/form.inc, line 2783
Functions for form and batch generation and processing.

Code

function theme_select($variables) {
  $element = $variables['element'];
  element_set_attributes($element, array(
    'id',
    'name',
    'size',
  ));
  _form_set_class($element, array(
    'form-select',
  ));
  return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
}

Comments

charlie-s’s picture

Notice how the argument $variables is an associative array where 'element' equals the array of identifiers.

This will not work:

print theme_select(array($array));

This will work:

print theme_select(array('element' => $array));
loganfsmyth’s picture

In general, you should not be executing this function directly.

Instead use the theme() function.

print theme('select', array('element' => $array));

or use #type => 'select' within a renderable array.

avner’s picture

<?php
print theme('select', array('element' => array('#options' => $array));
?>

checkout the code at :

function form_select_options($element, $choices = NULL) {
  if (!isset($choices)) {
    $choices = $element['#options'];
  }
...
patpluspun’s picture

#type => 'select' does NOT work with a renderable array, only if being called through drupal_get_form.

The correct way to pass a select list through a render array without invoking FAPI directly is to completely remove the element key altogether. I battled this for hours today.

thtas’s picture

Yup. it works like this:


$render_array = array(
    '#theme' => 'select',
    '#options' => $options,
  );

andykisaragi’s picture

To get a multi select directly from theme_select it seems you have to do

'#attributes' => array('multiple' => 'multiple'),

rather than

'#multiple' => true,

as you might expect if familiar with FAPI

dark_kz’s picture

How to do it in Drupal 8, am I need to build a form instead?