codit_menu_or_array_as_select_list() - Generate a select list for a form.
This function appears in the Codit Module
It can take a menu or simply an array of items and build a select form element (not the entire form, just the select). It has the following features.
- Support for full hml 5 options on a select (multiple, size, required, disabled, autofocus)
- Pull a menu in with path values.
- Mix other menu items to intermingle with the menu items
- Add specific items above the main items
- Add specific items below the main items
- Control which value is selected
- Control the NULL item
- Heavy cache or not. (complete with a html comment indicator declaring whether it is from cache or not)
<?php
/**
* Accepts a menu name or array(s) of items and returns a select list.
*
* All items in param are Optional
*
* @param array $a_select_items
* Items to be included in the select list and options for additional items
* - $a_select_items['sMenuName'] = '';// string The machine name of menu to
* load into select list.
* - $a_select_items['aItemsToLoad']['top'] = '';// array of key value pairs
* ('value' => 'test to display') to load and display above the menu items
* and mixed items.
* - $a_select_items['aItemsToLoad']['mixed'] = '';// array of key value
* pairs ('value' => 'test to display') to load and mix and sort with the
* menu loaded.
* - $a_select_items['aItemsToLoad']['bottom'] = '';// array of key value
* pairs ('value' => 'test to display') to load and display below the menu
* items and mixed items.
* - $a_select_items['sSelected'] = '';// string The value (not the visible
* text) of the item to list as selected [defaults to NULLitem]
* - $a_select_items['sNULLitem'] = '';// string The name of the first
* unselected item in the list. It gets a value of ' '.
* - $a_select_items['iSize'] = ;// integer The number of items to show in
* the select list before adding scroll bars.
* - $a_select_items['bRequired'] = ; boolean Whether the select should have
* the 'required' attribute or not (TRUE | FALSE default = FALSE)
* - $a_select_items['bMultiple'] = ; boolean Whether the select should allow
* multiple selections or not (TRUE | FALSE default = FALSE)
* - $a_select_items['bAutoFocus'] = ; boolean Whether the select get focus
* upon pageload (TRUE | FALSE default = FALSE)
* - $a_select_items['bDisabled'] = ; boolean Whether the select should
* disabled or not (TRUE | FALSE default = FALSE)
* WARNING if you are going to toggle this menu on or off, do not cached
* - $a_select_items['sSelectName'] = ''; string of the Name attribute for
* the select element (default = '')
* <select name="THIS-NAME-WOULD-APPEAR-HERE" >
* - $a_select_items['sSelectForm'] = ''; string of the Name of the form(s)
* this select list should be associated with (default = '')
* <select form="FORM-NAME-WOULD-APPEAR-HERE" >
*
* @param string $s_unique_cache_name
* The name to use to uniquely cache this item. If empty, this will not be
* heavy cached. (default empty)
*
* @return string
* The html of a fully formed select list with all the associated options as
* set within $a_select_items
*/
function codit_menu_or_array_as_select_list($a_select_items = array(), $s_unique_cache_name = '') {
// See the codit module for the actual code that resides here.
}
?>
Sample Output
<select name="city_select">
<option value=" " selected>Select a PA City</option>
<option value="/cities/allentown">Allentown</option>
<option value="/cities/altoona">Altoona</option>
<option value="/cities/coudersport">Coudersport</option>
<option value="/cities/dubois">DuBois</option>
</select>
<!-- Not from Cache -->
If you do not need all the power of this function, the Drupal theme function can be a lighter alternative (although without heavy caching it may actually be heavier)
theme_select()
http://api.drupal.org/api/drupal/includes%21form.inc/function/theme_sele...
<?php
$elements = array();
$elements[] = array(
'#title' => ' ',
'#value' => ' ',
'#options' => ' ',
'#description' => ' ',
'#extra' => ' ',
'#multiple' => ' ',
'#required' => ' ',
'#name' => ' ',
'#attributes' => ' ',
'#size' => ' '.
);
//This is the preferred method
$select_html_string = theme('select', array('element' => $elements));
///This also works
$select_html_string = theme_select(array('element' => $elements));
?>