summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
Diffstat (limited to 'includes')
-rw-r--r--includes/form.inc65
1 files changed, 57 insertions, 8 deletions
diff --git a/includes/form.inc b/includes/form.inc
index 96a182f64..20b3230b9 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -1107,14 +1107,6 @@ function form_builder($form_id, $element, &$form_state) {
// Internet Explorer button-click scenario.
_form_builder_ie_cleanup($element, $form_state);
- // We should keep the buttons array until the IE clean up function
- // has recognized the submit button so the form has been marked
- // as submitted. If we already know which button was submitted,
- // we don't need the array.
- if (!empty($form_state['submitted'])) {
- unset($form_state['buttons']);
- }
-
// If some callback set #cache, we need to flip a flag so later it
// can be found.
if (!empty($element['#cache'])) {
@@ -1291,6 +1283,63 @@ function _form_builder_ie_cleanup($form, &$form_state) {
}
/**
+ * Removes internal Form API elements and buttons from submitted form values.
+ *
+ * This function can be used when a module wants to store all submitted form
+ * values, for example, by serializing them into a single database column. In
+ * such cases, all internal Form API values and all form button elements should
+ * not be contained, and this function allows to remove them before the module
+ * proceeds to storage. Next to button elements, the following internal values
+ * are removed:
+ * - form_id
+ * - form_token
+ * - form_build_id
+ * - op
+ *
+ * @param &$form_state
+ * A keyed array containing the current state of the form, including
+ * submitted form values; altered by reference.
+ */
+function form_state_values_clean(&$form_state) {
+ // Remove internal Form API values.
+ unset($form_state['values']['form_id'], $form_state['values']['form_token'], $form_state['values']['form_build_id'], $form_state['values']['op']);
+
+ // Remove button values.
+ // form_builder() collects all button elements in a form, keyed by button
+ // type. We remove the button value separately for each button element.
+ foreach ($form_state['buttons'] as $button_type => $buttons) {
+ foreach ($buttons as $button) {
+ // Remove this button's value from the submitted form values by finding
+ // the value corresponding to this button.
+ // We iterate over the #parents of this button and move a reference to
+ // each parent in $form_state['values']. For example, if #parents is:
+ // array('foo', 'bar', 'baz')
+ // then the corresponding $form_state['values'] part will look like this:
+ // array(
+ // 'foo' => array(
+ // 'bar' => array(
+ // 'baz' => 'button_value',
+ // ),
+ // ),
+ // )
+ // We start by (re)moving 'baz' to $last_parent, so we are able unset it
+ // at the end of the iteration. Initially, $values will contain a
+ // reference to $form_state['values'], but in the iteration we move the
+ // reference to $form_state['values']['foo'], and finally to
+ // $form_state['values']['foo']['bar'], which is the level where we can
+ // unset 'baz' (that is stored in $last_parent).
+ $parents = $button['#parents'];
+ $values = &$form_state['values'];
+ $last_parent = array_pop($parents);
+ foreach ($parents as $parent) {
+ $values = &$values[$parent];
+ }
+ unset($values[$last_parent]);
+ }
+ }
+}
+
+/**
* Helper function to determine the value for an image button form element.
*
* @param $form