Skip to content

Instantly share code, notes, and snippets.

@psynaptic
Created July 26, 2012 14:18
Show Gist options
  • Select an option

  • Save psynaptic/3182297 to your computer and use it in GitHub Desktop.

Select an option

Save psynaptic/3182297 to your computer and use it in GitHub Desktop.
<?php
/**
* Format a general form item.
*
* @param $title
* The label for the form item.
* @param $value
* The contents of the form item.
* @param $description
* Explanatory text to display after the form item.
* @param $id
* A unique identifier for the form item.
* @param $required
* Whether the user must fill in this form element before submitting the form.
* @param $error
* An error message to display alongside the form element.
* @return
* A themed HTML string representing the form item.
*/
function form_item($title, $value, $description = NULL, $id = NULL, $required = FALSE, $error = FALSE) {
return theme('form_element', $title, $value, $description, $id, $required, $error);
}
/**
* Format a group of form items.
*
* @param $legend
* The label for the form item group.
* @param $group
* The form items within the group, as an HTML string.
* @param $description
* Explanatory text to display after the form item group.
* @return
* A themed HTML string representing the form item group.
*/
function form_group($legend, $group, $description = NULL) {
return '<fieldset>' . ($legend ? '<legend>'. $legend .'</legend>' : '') . $group . ($description ? '<div class="description">'. $description .'</div>' : '') . "</fieldset>\n";
}
/**
* Format a radio button.
*
* @param $title
* The label for the radio button.
* @param $name
* The internal name used to refer to the button.
* @param $value
* The value that the form element takes on when selected.
* @param $checked
* Whether the button will be initially selected when the page is rendered.
* @param $description
* Explanatory text to display after the form item.
* @param $attributes
* An associative array of HTML attributes to add to the button.
* @param $required
* Whether the user must select this radio button before submitting the form.
* @return
* A themed HTML string representing the radio button.
*/
function form_radio($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE) {
$element = '<input type="radio" class="'. _form_get_class('form-radio', $required, _form_get_error($name)) .'" name="edit['. $name .']" value="'. $value .'"'. ($checked ? ' checked="checked"' : '') . drupal_attributes($attributes) .' />';
if (!is_null($title)) {
$element = '<label class="option">'. $element .' '. $title .'</label>';
}
return theme('form_element', NULL, $element, $description, $name, $required, _form_get_error($name));
}
/**
* Format a set of radio buttons.
*
* @param $title
* The label for the radio buttons as a group.
* @param $name
* The internal name used to refer to the buttons.
* @param $value
* The currently selected radio button's key.
* @param $options
* An associative array of buttons to display. The keys in this array are
* button values, while the values are the labels to display for each button.
* @param $description
* Explanatory text to display after the form item.
* @param $required
* Whether the user must select a radio button before submitting the form.
* @param $attributes
* An associative array of HTML attributes to add to each button.
* @return
* A themed HTML string representing the radio button set.
*/
function form_radios($title, $name, $value, $options, $description = NULL, $required = FALSE, $attributes = NULL) {
if (count($options) > 0) {
$choices = '';
foreach ($options as $key => $choice) {
$choices .= '<label class="option"><input type="radio" class="form-radio" name="edit['. $name .']" value="'. $key .'"'. ($key == $value ? ' checked="checked"' : ''). drupal_attributes($attributes) .' /> '. $choice .'</label><br />';
}
return theme('form_element', $title, $choices, $description, NULL, $required, _form_get_error($name));
}
}
/**
* Format a checkbox.
*
* @param $title
* The label for the checkbox.
* @param $name
* The internal name used to refer to the button.
* @param $value
* The value that the form element takes on when selected.
* @param $checked
* Whether the button will be initially selected when the page is rendered.
* @param $description
* Explanatory text to display after the form item.
* @param $attributes
* An associative array of HTML attributes to add to the button.
* @param $required
* Whether the user must check this box before submitting the form.
* @return
* A themed HTML string representing the checkbox.
*/
function form_checkbox($title, $name, $value = 1, $checked = FALSE, $description = NULL, $attributes = NULL, $required = FALSE) {
$element = '<input type="checkbox" class="'. _form_get_class('form-checkbox', $required, _form_get_error($name)) .'" name="edit['. $name .']" id="edit-'. $name .'" value="'. $value .'"'. ($checked ? ' checked="checked"' : '') . drupal_attributes($attributes) .' />';
if (!is_null($title)) {
$element = '<label class="option">'. $element .' '. $title .'</label>';
}
// Note: because unchecked boxes are not included in the POST data, we include
// a form_hidden() which will be overwritten for a checked box.
return form_hidden($name, 0) . theme('form_element', NULL, $element, $description, $name, $required, _form_get_error($name));
}
/**
* Format a set of checkboxes.
*
* @param $title
* The label for the checkboxes as a group.
* @param $name
* The internal name used to refer to the buttons.
* @param $values
* A linear array of keys of the initially checked boxes.
* @param $options
* An associative array of buttons to display. The keys in this array are
* button values, while the values are the labels to display for each button.
* @param $description
* Explanatory text to display after the form item.
* @param $attributes
* An associative array of HTML attributes to add to each button.
* @param $required
* Whether the user must check a box before submitting the form.
* @return
* A themed HTML string representing the radio button set.
*/
function form_checkboxes($title, $name, $values, $options, $description = NULL, $attributes = NULL, $required = FALSE) {
if (count($options) > 0) {
if (!isset($values) || $values == 0) {
$values = array();
}
$choices = '';
foreach ($options as $key => $choice) {
$choices .= '<label class="option"><input type="checkbox" class="form-checkbox" name="edit['. $name .'][]" value="'. $key .'"'. (in_array($key, $values) ? ' checked="checked"' : ''). drupal_attributes($attributes) .' /> '. $choice .'</label><br />';
}
// Note: because unchecked boxes are not included in the POST data, we
// include a form_hidden() which will be overwritten as soon as there is at
// least one checked box.
return form_hidden($name, 0) . theme('form_element', $title, $choices, $description, NULL, $required, _form_get_error($name));
}
}
/**
* Format a single-line text field.
*
* @param $title
* The label for the text field.
* @param $name
* The internal name used to refer to the field.
* @param $value
* The initial value for the field at page load time.
* @param $size
* A measure of the visible size of the field (passed directly to HTML).
* @param $maxlength
* The maximum number of characters that may be entered in the field.
* @param $description
* Explanatory text to display after the form item.
* @param $attributes
* An associative array of HTML attributes to add to the form item.
* @param $required
* Whether the user must enter some text in the field.
* @return
* A themed HTML string representing the field.
*/
function form_textfield($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) {
$size = $size ? ' size="'. $size .'"' : '';
return theme('form_element', $title, '<input type="text" maxlength="'. $maxlength .'" class="'. _form_get_class('form-text', $required, _form_get_error($name)) .'" name="edit['. $name .']" id="edit-'. $name .'"'. $size .' value="'. check_plain($value) .'"'. drupal_attributes($attributes) .' />', $description, 'edit-'. $name, $required, _form_get_error($name));
}
/**
* Format a single-line text field that does not display its contents visibly.
*
* @param $title
* The label for the text field.
* @param $name
* The internal name used to refer to the field.
* @param $value
* The initial value for the field at page load time.
* @param $size
* A measure of the visible size of the field (passed directly to HTML).
* @param $maxlength
* The maximum number of characters that may be entered in the field.
* @param $description
* Explanatory text to display after the form item.
* @param $attributes
* An associative array of HTML attributes to add to the form item.
* @param $required
* Whether the user must enter some text in the field.
* @return
* A themed HTML string representing the field.
*/
function form_password($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) {
$size = $size ? ' size="'. $size .'"' : '';
return theme('form_element', $title, '<input type="password" class="'. _form_get_class('form-password', $required, _form_get_error($name)) .'" maxlength="'. $maxlength .'" name="edit['. $name .']" id="edit-'. $name .'"'. $size .' value="'. check_plain($value) .'"'. drupal_attributes($attributes) .' />', $description, 'edit-'. $name, $required, _form_get_error($name));
}
/**
* Format a multiple-line text field.
*
* @param $title
* The label for the text field.
* @param $name
* The internal name used to refer to the field.
* @param $value
* The initial value for the field at page load time.
* @param $cols
* The width of the field, in columns of text.
* @param $rows
* The height of the field, in rows of text.
* @param $description
* Explanatory text to display after the form item.
* @param $attributes
* An associative array of HTML attributes to add to the form item.
* @param $required
* Whether the user must enter some text in the field.
* @return
* A themed HTML string representing the field.
*/
function form_textarea($title, $name, $value, $cols, $rows, $description = NULL, $attributes = NULL, $required = FALSE) {
$cols = $cols ? ' cols="'. $cols .'"' : '';
$pre = '';
$post = '';
// optionally plug in a WYSIWYG editor
foreach (module_list() as $module_name) {
if (module_hook($module_name, 'textarea')) {
$pre .= module_invoke($module_name, 'textarea', 'pre', $name);
$post .= module_invoke($module_name, 'textarea', 'post', $name);
}
}
return theme('form_element', $title, $pre .'<textarea wrap="virtual"'. $cols .' rows="'. $rows .'" name="edit['. $name .']" id="edit-'. $name .'" class="'. _form_get_class('textarea', $required, _form_get_error($name)) .'"'. drupal_attributes($attributes) .'>'. check_plain($value) .'</textarea>'. $post, $description, 'edit-'. $name, $required, _form_get_error($name));
}
/**
* Format a dropdown menu or scrolling selection box.
*
* @param $title
* The label for the form element.
* @param $name
* The internal name used to refer to the form element.
* @param $value
* The key of the currently selected item, or a linear array of keys of all the
* currently selected items if multiple selections are allowed.
* @param $options
* An associative array of buttons to display. The keys in this array are
* button values, while the values are the labels to display for each button.
* @param $description
* Explanatory text to display after the form item.
* @param $extra
* Additional HTML to inject into the select element tag.
* @param $multiple
* Whether the user may select more than one item.
* @param $required
* Whether the user must select a value before submitting the form.
* @return
* A themed HTML string representing the 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.
*/
function form_select($title, $name, $value, $options, $description = NULL, $extra = 0, $multiple = FALSE, $required = FALSE) {
$select = '';
foreach ($options as $key => $choice) {
if (is_array($choice)) {
$select .= '<optgroup label="'. $key .'">';
foreach ($choice as $key => $choice) {
$select .= '<option value="'. $key .'"'. (is_array($value) ? (in_array($key, $value) ? ' selected="selected"' : '') : ($value == $key ? ' selected="selected"' : '')) .'>'. check_plain($choice) .'</option>';
}
$select .= '</optgroup>';
}
else {
$select .= '<option value="'. $key .'"'. (is_array($value) ? (in_array($key, $value) ? ' selected="selected"' : '') : ($value == $key ? ' selected="selected"' : '')) .'>'. check_plain($choice) .'</option>';
}
}
return theme('form_element', $title, '<select name="edit['. $name .']'. ($multiple ? '[]' : '') .'"'. ($multiple ? ' multiple="multiple" ' : '') . ($extra ? ' '. $extra : '') .' id="edit-'. $name .'">'. $select .'</select>', $description, 'edit-'. $name, $required, _form_get_error($name));
}
/**
* Format a file upload field.
*
* @param $title
* The label for the file upload field.
* @param $name
* The internal name used to refer to the field.
* @param $size
* A measure of the visible size of the field (passed directly to HTML).
* @param $description
* Explanatory text to display after the form item.
* @param $required
* Whether the user must upload a file to the field.
* @return
* A themed HTML string representing the field.
*
* For assistance with handling the uploaded file correctly, see the API
* provided by file.inc.
*/
function form_file($title, $name, $size, $description = NULL, $required = FALSE) {
return theme('form_element', $title, '<input type="file" class="'. _form_get_class('form-file', $required, _form_get_error($name)) .'" name="edit['. $name .']" id="edit-'. $name .'" size="'. $size ."\" />\n", $description, 'edit-'. $name, $required, _form_get_error($name));
}
/**
* Store data in a hidden form field.
*
* @param $name
* The internal name used to refer to the field.
* @param $value
* The stored data.
* @return
* A themed HTML string representing the hidden field.
*
* This function can be useful in retaining information between page requests,
* but be sure to validate the data on the receiving page as it is possible for
* an attacker to change the value before it is submitted.
*/
function form_hidden($name, $value) {
return '<input type="hidden" name="edit['. $name .']" value="'. check_plain($value) ."\" />\n";
}
/**
* Format an action button.
*
* @param $value
* Both the label for the button, and the value passed to the target page
* when this button is clicked.
* @param $name
* The internal name used to refer to the button.
* @param $type
* What type to pass to the HTML input tag.
* @param $attributes
* An associative array of HTML attributes to add to the form item.
* @return
* A themed HTML string representing the button.
*/
function form_button($value, $name = 'op', $type = 'submit', $attributes = NULL) {
return '<input type="'. $type .'" class="form-'. $type .'" name="'. $name .'" value="'. check_plain($value) .'" '. drupal_attributes($attributes) ." />\n";
}
/**
* Format a form submit button.
*
* @param $value
* Both the label for the button, and the value passed to the target page
* when this button is clicked.
* @param $name
* The internal name used to refer to the button.
* @param $attributes
* An associative array of HTML attributes to add to the form item.
* @return
* A themed HTML string representing the button.
*/
function form_submit($value, $name = 'op', $attributes = NULL) {
return form_button($value, $name, 'submit', $attributes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment