function form_clean_id
6.x form.inc | form_clean_id($id = NULL, $flush = FALSE) |
Prepare an HTML ID attribute string for a form item.
Remove invalid characters and guarantee uniqueness.
Parameters
$id: The ID to clean.
$flush: If set to TRUE, the function will flush and reset the static array which is built to test the uniqueness of element IDs. This is only used if a form has completed the validation process. This parameter should never be set to TRUE if this function is being called to assign an ID to the #ID element.
Return value
The cleaned ID.
Related topics
6 calls to form_clean_id()
- drupal_prepare_form in drupal-6.x/
includes/ form.inc - Prepares a structured form array by adding required elements, executing any hook_form_alter functions, and optionally inserting a validation token to prevent tampering.
- drupal_process_form in drupal-6.x/
includes/ form.inc - This function is the heart of form API. The form gets built, validated and in appropriate cases, submitted.
- expand_radios in drupal-6.x/
includes/ form.inc - Roll out a single radios element to a list of radios, using the options array as index.
- filter_form in drupal-6.x/
modules/ filter/ filter.module - Generates a selector for choosing a format in a form.
- template_preprocess_page in drupal-6.x/
includes/ theme.inc - Process variables for page.tpl.php
File
- drupal-6.x/
includes/ form.inc, line 2285
Code
function form_clean_id($id = NULL, $flush = FALSE) {
static $seen_ids = array();
if ($flush) {
$seen_ids = array();
return;
}
$id = str_replace(array('][', '_', ' '), '-', $id);
// Ensure IDs are unique. The first occurrence is held but left alone.
// Subsequent occurrences get a number appended to them. This incrementing
// will almost certainly break code that relies on explicit HTML IDs in
// forms that appear more than once on the page, but the alternative is
// outputting duplicate IDs, which would break JS code and XHTML
// validity anyways. For now, it's an acceptable stopgap solution.
if (isset($seen_ids[$id])) {
$id = $id . '-' . $seen_ids[$id]++;
}
else {
$seen_ids[$id] = 1;
}
return $id;
}