Commit a3f84c77 authored by Dries's avatar Dries

- Patch #38349 by hunmonk:

   1. hook_form_validate_alter and hook_form_execute_alter have been eliminated. there usage was limiting and a bit confusing. now, only hook_form_alter remains
   2. replacing those hooks are the #validate and #execute attributes. these can be used either when building the form in a module, or via hook_form_alter. these attributes are built in array form, to allow for flexibility in how they are called. for example, a module could simply add an element to the #validate array via hook_form_alter if it wished to perform additional validation, or it could redeclare the entire #validate array if it wanted to override any existing validation
   3. attributes #validation_arguments and #execution_arguments have been added to allow args to be used w/ custom validation and execution functions. this are also declared in array format
   4. renamed #valid attribute to #validation. this doesn't cause any conflicts, and makes the naming convention more consistent
   5. renamed the existing #execute attribute associated w/ submit buttons to #submit
parent f532273a
...@@ -52,9 +52,9 @@ function element_children($element) { ...@@ -52,9 +52,9 @@ function element_children($element) {
* *
*/ */
function drupal_get_form($form_id, &$form, $callback = NULL) { function drupal_get_form($form_id, &$form, $callback = NULL) {
global $form_values, $form_execute; global $form_values, $form_submitted;
$form_values = array(); $form_values = array();
$form_execute = FALSE; $form_submitted = FALSE;
$form['#type'] = 'form'; $form['#type'] = 'form';
if (isset($form['#token'])) { if (isset($form['#token'])) {
...@@ -70,6 +70,24 @@ function drupal_get_form($form_id, &$form, $callback = NULL) { ...@@ -70,6 +70,24 @@ function drupal_get_form($form_id, &$form, $callback = NULL) {
$form = array_merge(_element_info('form'), $form); $form = array_merge(_element_info('form'), $form);
if (!isset($form['#validate'])) {
if (function_exists($form_id .'_validate')) {
$form['#validate'] = array($form_id .'_validate');
}
elseif (function_exists($callback .'_validate')) {
$form['#validate'] = array($callback .'_validate');
}
}
if (!isset($form['#execute'])) {
if (function_exists($form_id .'_execute')) {
$form['#execute'] = array($form_id .'_execute');
}
elseif (function_exists($callback .'_execute')) {
$form['#execute'] = array($callback .'_execute');
}
}
foreach (module_implements('form_alter') as $module) { foreach (module_implements('form_alter') as $module) {
$function = $module .'_form_alter'; $function = $module .'_form_alter';
$function($form_id, $form); $function($form_id, $form);
...@@ -79,7 +97,7 @@ function drupal_get_form($form_id, &$form, $callback = NULL) { ...@@ -79,7 +97,7 @@ function drupal_get_form($form_id, &$form, $callback = NULL) {
if (!empty($_POST['edit']) && (($_POST['edit']['form_id'] == $form_id) || ($_POST['edit']['form_id'] == $callback))) { if (!empty($_POST['edit']) && (($_POST['edit']['form_id'] == $form_id) || ($_POST['edit']['form_id'] == $callback))) {
drupal_validate_form($form_id, $form, $callback); drupal_validate_form($form_id, $form, $callback);
if ($form_execute && !form_get_errors()) { if ($form_submitted && !form_get_errors()) {
drupal_execute_form($form_id, $form, $callback); drupal_execute_form($form_id, $form, $callback);
} }
} }
...@@ -103,33 +121,22 @@ function drupal_validate_form($form_id, &$form, $callback = NULL) { ...@@ -103,33 +121,22 @@ function drupal_validate_form($form_id, &$form, $callback = NULL) {
} }
} }
foreach (module_implements('form_validate_alter') as $module) {
$function = $module .'_form_validate_alter';
$function($form_id, $form_values);
}
_form_validate($form); _form_validate($form);
if (function_exists($form_id . '_validate')) {
call_user_func($form_id . '_validate', $form_id, $form_values);
}
elseif (function_exists($callback . '_validate')) {
call_user_func($callback . '_validate', $form_id, $form_values);
}
} }
function drupal_execute_form($form_id, $form, $callback = NULL) { function drupal_execute_form($form_id, $form, $callback = NULL) {
global $form_values; global $form_values;
foreach (module_implements('form_execute_alter') as $module) {
$function = $module .'_form_execute_alter';
$function($form_id, $form_values);
}
if (function_exists($form_id . '_execute')) { if (isset($form['#execute'])) {
call_user_func($form_id . '_execute', $form_id, $form_values); foreach ($form['#execute'] as $key => $function) {
} if (isset($form['#execution_arguments'][$key])) {
elseif (function_exists($callback . '_execute')) { $function_args = array_merge(array($form_id, $form_values), $form['#execution_arguments'][$key]);
call_user_func($callback . '_execute', $form_id, $form_values); call_user_func_array($function, $function_args);
}
else {
call_user_func($function, $form_id, $form_values);
}
}
} }
} }
...@@ -149,19 +156,19 @@ function _form_validate($elements) { ...@@ -149,19 +156,19 @@ function _form_validate($elements) {
if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0') { if ($elements['#required'] && empty($elements['#value']) && $elements['#value'] !== '0') {
form_error($elements, t('%name field is required', array('%name' => $elements['#title']))); form_error($elements, t('%name field is required', array('%name' => $elements['#title'])));
} }
if ($elements['#valid']) { if (isset($elements['#validate'])) {
if (is_array($elements['#valid'])) { if (is_array($elements['#validate'])) {
foreach ($elements['#valid'] as $key => $valid) { foreach ($elements['#validate'] as $key => $validate) {
$args = is_array($elements['#validation_arguments'][$key]) ? $elements['#validation_arguments'][$key] : array(); $args = is_array($elements['#validate_arguments'][$key]) ? $elements['#validate_arguments'][$key] : array();
if (function_exists($valid . '_valid')) { if (function_exists($validate)) {
call_user_func_array($valid . '_valid', array_merge(array($elements), $args)); call_user_func_array($validate, array_merge(array($elements), $args));
} }
} }
} }
else { else {
$args = is_array($elements['#validation_arguments']) ? $elements['#validation_arguments'] : array(); $args = is_array($elements['#validate_arguments']) ? $elements['#validate_arguments'] : array();
if (function_exists($elements['#valid'] . '_valid')) { if (function_exists($elements['#validate'])) {
call_user_func_array($elements['#valid'] . '_valid', array_merge(array($elements), $args)); call_user_func_array($elements['#validate'], array_merge(array($elements), $args));
} }
} }
} }
...@@ -223,7 +230,7 @@ function form_error(&$element, $message) { ...@@ -223,7 +230,7 @@ function form_error(&$element, $message) {
*/ */
function _form_builder($form_id, $form) { function _form_builder($form_id, $form) {
global $form_values; global $form_values;
global $form_execute; global $form_submitted;
/* Use element defaults */ /* Use element defaults */
if ((!empty($form['#type'])) && ($info = _element_info($form['#type']))) { if ((!empty($form['#type'])) && ($info = _element_info($form['#type']))) {
$form += $info; $form += $info;
...@@ -257,9 +264,9 @@ function _form_builder($form_id, $form) { ...@@ -257,9 +264,9 @@ function _form_builder($form_id, $form) {
$form['#value'] = $form['#default_value']; $form['#value'] = $form['#default_value'];
} }
} }
if (isset($form['#execute'])) { if (isset($form['#form_submitted'])) {
if ($_POST[$form['#name']] == $form['#value']) { if ($_POST[$form['#name']] == $form['#value']) {
$form_execute = $form_execute || $form['#execute']; $form_submitted = $form_submitted || $form['#form_submitted'];
} }
} }
......
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