function fp_render_form_element
Search API
4.x forms.inc | fp_render_form_element($name, $element, $use_session_submission_values_for_callback = "") |
Returns the HTML to render this form element to the screen. $name is the HTML machine name. $element is an array containing all we need to render it. If you want default values to be taken from the SESSION (because we had form_errors, say, and we want values to keep what we had between submissions) specify the callback to use in the use_session_submission_values_for_callback variable.
1 call to fp_render_form_element()
- fp_render_form in includes/
forms.inc - Render the form array from the callback to the screen, and set the form to save itself in our default submit handler. Valid form_types are: "system_settings" => values automatically saved to variables table. "normal" or BLANK…
File
- includes/
forms.inc, line 301
Code
function fp_render_form_element($name, $element, $use_session_submission_values_for_callback = "") {
$rtn = "";
$type = $element ["type"];
if ($type == "") {
$type = "markup";
}
$value = $element ["value"];
$label = $element ["label"];
$options = $element ["options"];
$description = $element ["description"];
$popup_description = $element ["popup_description"];
$prefix = $element ["prefix"];
$suffix = $element ["suffix"];
$required = $element ["required"];
$no_please_select = $element ["no_please_select"];
if (isset($element ["hide_please_select"])) {
$no_please_select = $element ["hide_please_select"];
}
$confirm = $element ["confirm"];
$attributes = $element ["attributes"];
if (is_array($attributes)) {
// Convert the attributes array into a string.
$new_attr = "";
foreach ($attributes as $key => $val) {
$new_attr .= " $key='$val' ";
}
$attributes = $new_attr;
}
$popup_help_link = "";
if ($popup_description) {
//$popup_help_link = " <a href='javascript: alert(\"" . $popup_description . "\");' class='form-popup-description'>[?]</a>";
$popup_help_link = fp_get_js_alert_link($popup_description, "[?]", "form-popup-description");
}
$element_extra_css = "";
if (is_array($_SESSION ["fp_form_errors"])) {
foreach ($_SESSION ["fp_form_errors"] as $err) {
if ($err ["name"] == $name) {
// There is an error on this element! Add an extra CSS element.
$element_extra_css .= "form-element-error";
}
}
}
if ($use_session_submission_values_for_callback && is_array($_SESSION ["fp_form_submissions"][$use_session_submission_values_for_callback]["values"])) {
// Check the SESSION for a previous value which we should use.
$ignore_types = array("hidden", "markup", "submit", "password");
if (!in_array($type, $ignore_types)) {
$value = $_SESSION ["fp_form_submissions"][$use_session_submission_values_for_callback]["values"][$name];
}
}
if ($type != "markup") {
$rtn .= "<div id='element-wrapper-$name' class='form-element element-type-$type $element_extra_css'>";
}
if ($prefix) {
$rtn .= $prefix;
}
$ast = "";
if ($required) {
$ast = "<span class='form-required-ast'>*</span>";
}
// First of all, what is it's "type"?
if ($type == "markup") {
$rtn .= "$value";
}
else if ($type != "hidden" && $type != "checkbox") {
$rtn .= "<label>$ast$label $popup_help_link</label>";
}
if ($type == "textarea") {
$rows = ($element ["rows"]) ? $element ["rows"] : "5";
$rtn .= "<textarea name='$name' id='element-$name' rows='$rows' $attributes>$value</textarea>";
}
if ($type == "textfield" || $type == "text" || $type == "password") {
if ($type == "textfield") {
$type = "text";
}
$size = ($element ["size"]) ? $element ["size"] : "60";
$maxlength = ($element ["maxlength"]) ? $element ["maxlength"] : "255";
$value = htmlentities($value, ENT_QUOTES);
$rtn .= "<input type='$type' name='$name' id='element-$name' size='$size' maxlength='$maxlength' value='$value' $attributes>";
}
if ($type == "hidden") {
$value = htmlentities($value, ENT_QUOTES);
$rtn .= "<input type='hidden' name='$name' id='element-$name' value='$value'>";
}
if ($type == "file") {
$rtn .= "<input type='file' name='$name' id='element-$name'>";
}
if ($type == "radios") {
$rtn .= "<div class='form-radios form-radios-$name'>";
foreach ($options as $key => $val) {
$checked = "";
if ($value == $key) {
$checked = "checked=checked";
}
$rtn .= "<div class='radio-element radio-element-$key'>
<label class='label-for-radio'><input type='radio' name='$name' id='element-$name-$key' value='$key' $checked $attributes> $val</label>
</div>";
}
$rtn .= "</div>";
}
if ($type == "select") {
$rtn .= "<select name='$name' id='element-$name' $attributes>";
if ($no_please_select != TRUE) {
$rtn .= "<option value=''>- Please select -</option>";
}
foreach ($options as $key => $val) {
$selected = "";
if ($value == $key) {
$selected = "selected";
}
$rtn .= "<option value='$key' $selected>$val</option>";
}
$rtn .= "</select>";
}
// Multiple checkboxes...
if ($type == "checkboxes") {
$rtn .= "<div class='form-checkboxes form-checkboxes-$name'>";
foreach ($options as $key => $val) {
$checked = "";
if (is_array($value) && $value [$key] == $key) {
$checked = "checked=checked";
}
$rtn .= "<div class='checkbox-element checkbox-element-$key'>
<label class='label-for-checkbox'><input type='checkbox' name='$name" . "[$key]' id='element-$name-$key' value='$key' $checked $attributes> $val</label>
</div>";
}
$rtn .= "</div>";
}
// A single checkbox... The values will be with 0 (zero) or 1 (one), and boolean
// values are accepted/saved
if ($type == "checkbox") {
$rtn .= "<div class='form-checkbox form-checkbox-$name'>";
$checked = "";
if ((bool) ($value) == TRUE) {
$checked = "checked=checked";
}
$rtn .= "<div class='checkbox-element'>
<label class='label-for-checkbox'><input type='checkbox' name='$name' id='element-$name' value='1' $checked $attributes> $label</label>
</div>";
$rtn .= "</div>";
}
if ($type == "submit") {
if ($confirm != "") {
$confirm = htmlentities($confirm, ENT_QUOTES);
$confirm = str_replace("\n", "\\n", $confirm);
$attributes .= " onClick='return confirm(\"$confirm\");' ";
}
$rtn .= "<input type='submit' name='$name' value='$value' $attributes>";
}
if ($type == "button") {
$rtn .= "<input type='button' name='$name' value='$value' $attributes>";
}
if ($description) {
$rtn .= "<div class='form-element-description'>$description</div>";
}
if ($suffix) {
$rtn .= $suffix;
}
if ($type != "markup") {
$rtn .= "</div>";
}
return $rtn;
}