Commit fb8ef283 authored by Steven Wittens's avatar Steven Wittens
Browse files

#56921: Remove all reference magic from form api. w00t.

parent 8b04c7f0
......@@ -333,12 +333,9 @@ function form_builder($form_id, $form) {
$posted = (isset($_POST['edit']) && ($_POST['edit']['form_id'] == $form_id));
$edit = $posted ? $_POST['edit'] : array();
$ref =& $form_values;
foreach ($form['#parents'] as $parent) {
$edit = isset($edit[$parent]) ? $edit[$parent] : NULL;
$ref =& $ref[$parent];
}
$form['#ref'] = &$ref;
if (!isset($form['#value']) && !array_key_exists('#value', $form)) {
if ($posted) {
switch ($form['#type']) {
......@@ -402,7 +399,7 @@ function form_builder($form_id, $form) {
// We call this after #process gets called so that #process has a
// chance to update #value if desired.
if (isset($form['#input']) && $form['#input']) {
$ref = $form['#value'];
form_set_value($form, $form['#value']);
}
// Recurse through all child elements.
......@@ -436,6 +433,45 @@ function form_builder($form_id, $form) {
return $form;
}
/**
* Use this function to make changes to form values in the form validate
* phase, so they will be available in the submit phase in $form_values.
*
* Specifically, if $form['#parents'] is array('foo', 'bar')
* and $value is 'baz' then this function will make
* $form_values['foo']['bar'] to be 'baz'.
*
* @param $form
* The form item. Keys used: #parents, #value
* @param $value
* The value for the form item.
*/
function form_set_value($form, $value) {
global $form_values;
_form_set_value($form_values, $form, $form['#parents'], $value);
}
/**
* Helper function for form_set_value().
*
* We iterate of $parents and create nested arrays for them
* in $form_values if needed. Then we insert the value in
* the right array.
*/
function _form_set_value(&$form_values, $form, $parents, $value) {
$parent = array_shift($parents);
if (empty($parents)) {
$form_values[$parent] = $value;
}
else {
if (!isset($form_values[$parent])) {
$form_values[$parent] = array();
}
_form_set_value($form_values[$parent], $form, $parents, $value);
}
return $form;
}
/**
* Renders a HTML form given a form tree. Recursively iterates over each of
* the form elements, generating HTML code. This function is usually
......@@ -681,14 +717,14 @@ function password_confirm_validate($form) {
if (isset($form['pass1']['#value'])) {
$pass1 = trim($form['pass1']['#value']);
$pass2 = trim($form['pass2']['#value']);
$form['pass1']['#ref'] = NULL;
$form['pass2']['#ref'] = NULL;
form_set_value($form['pass1'], NULL);
form_set_value($form['pass2'], NULL);
form_set_value($form, $pass1);
if ($pass1 != $pass2) {
form_error($form, t('The specified passwords do not match.'));
form_error($form['pass1']);
form_error($form['pass2']);
}
$form['#ref'] = $pass1;
}
elseif ($form['#required'] && !empty($_POST['edit'])) {
form_set_error('pass1', t('Password field is required.'));
......
......@@ -2170,7 +2170,7 @@ function node_form_alter($form_id, &$form) {
*/
function node_search_validate($form_id, $form_values, $form) {
// Initialise using any existing basic search keywords.
$keys = $form['basic']['inline']['processed_keys']['#ref'];
$keys = $form_values['processed_keys'];
// Insert extra restrictions into the search keywords string.
if (isset($form_values['type']) && is_array($form_values['type'])) {
......@@ -2180,6 +2180,7 @@ function node_search_validate($form_id, $form_values, $form) {
$keys = search_query_insert($keys, 'type', implode(',', array_keys($form_values['type'])));
}
}
if (isset($form_values['category']) && is_array($form_values['category'])) {
$keys = search_query_insert($keys, 'category', implode(',', $form_values['category']));
}
......@@ -2197,7 +2198,7 @@ function node_search_validate($form_id, $form_values, $form) {
$keys .= ' "'. str_replace('"', ' ', $form_values['phrase']) .'"';
}
if (!empty($keys)) {
$form['basic']['inline']['processed_keys']['#ref'] = trim($keys);
form_set_value($form['basic']['inline']['processed_keys'], trim($keys));
}
}
......
......@@ -2170,7 +2170,7 @@ function node_form_alter($form_id, &$form) {
*/
function node_search_validate($form_id, $form_values, $form) {
// Initialise using any existing basic search keywords.
$keys = $form['basic']['inline']['processed_keys']['#ref'];
$keys = $form_values['processed_keys'];
// Insert extra restrictions into the search keywords string.
if (isset($form_values['type']) && is_array($form_values['type'])) {
......@@ -2180,6 +2180,7 @@ function node_search_validate($form_id, $form_values, $form) {
$keys = search_query_insert($keys, 'type', implode(',', array_keys($form_values['type'])));
}
}
if (isset($form_values['category']) && is_array($form_values['category'])) {
$keys = search_query_insert($keys, 'category', implode(',', $form_values['category']));
}
......@@ -2197,7 +2198,7 @@ function node_search_validate($form_id, $form_values, $form) {
$keys .= ' "'. str_replace('"', ' ', $form_values['phrase']) .'"';
}
if (!empty($keys)) {
$form['basic']['inline']['processed_keys']['#ref'] = trim($keys);
form_set_value($form['basic']['inline']['processed_keys'], trim($keys));
}
}
......
......@@ -1006,7 +1006,7 @@ function search_form($action = '', $keys = '', $type = NULL, $prompt = NULL) {
* search form.
*/
function search_form_validate($form_id, $form_values, $form) {
$form['basic']['inline']['processed_keys']['#ref'] = trim($form_values['keys']);
form_set_value($form['basic']['inline']['processed_keys'], trim($form_values['keys']));
}
/**
......
......@@ -1006,7 +1006,7 @@ function search_form($action = '', $keys = '', $type = NULL, $prompt = NULL) {
* search form.
*/
function search_form_validate($form_id, $form_values, $form) {
$form['basic']['inline']['processed_keys']['#ref'] = trim($form_values['keys']);
form_set_value($form['basic']['inline']['processed_keys'], trim($form_values['keys']));
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment