webmaster@1: NULL, 'submitted' => FALSE); webmaster@1: webmaster@1: $args = func_get_args(); webmaster@1: $cacheable = FALSE; webmaster@1: webmaster@1: if (isset($_SESSION['batch_form_state'])) { webmaster@1: // We've been redirected here after a batch processing : the form has webmaster@1: // already been processed, so we grab the post-process $form_state value webmaster@1: // and move on to form display. See _batch_finished() function. webmaster@1: $form_state = $_SESSION['batch_form_state']; webmaster@1: unset($_SESSION['batch_form_state']); webmaster@1: } webmaster@1: else { webmaster@1: // If the incoming $_POST contains a form_build_id, we'll check the webmaster@1: // cache for a copy of the form in question. If it's there, we don't webmaster@1: // have to rebuild the form to proceed. In addition, if there is stored webmaster@1: // form_state data from a previous step, we'll retrieve it so it can webmaster@1: // be passed on to the form processing code. webmaster@1: if (isset($_POST['form_id']) && $_POST['form_id'] == $form_id && !empty($_POST['form_build_id'])) { webmaster@1: $form = form_get_cache($_POST['form_build_id'], $form_state); webmaster@1: } webmaster@1: webmaster@1: // If the previous bit of code didn't result in a populated $form webmaster@1: // object, we're hitting the form for the first time and we need webmaster@1: // to build it from scratch. webmaster@1: if (!isset($form)) { webmaster@1: $form_state['post'] = $_POST; webmaster@1: // Use a copy of the function's arguments for manipulation webmaster@1: $args_temp = $args; webmaster@1: $args_temp[0] = &$form_state; webmaster@1: array_unshift($args_temp, $form_id); webmaster@1: webmaster@1: $form = call_user_func_array('drupal_retrieve_form', $args_temp); webmaster@1: $form_build_id = 'form-'. md5(mt_rand()); webmaster@1: $form['#build_id'] = $form_build_id; webmaster@1: drupal_prepare_form($form_id, $form, $form_state); webmaster@1: // Store a copy of the unprocessed form for caching and indicate that it webmaster@1: // is cacheable if #cache will be set. webmaster@1: $original_form = $form; webmaster@1: $cacheable = TRUE; webmaster@1: unset($form_state['post']); webmaster@1: } webmaster@1: $form['#post'] = $_POST; webmaster@1: webmaster@1: // Now that we know we have a form, we'll process it (validating, webmaster@1: // submitting, and handling the results returned by its submission webmaster@1: // handlers. Submit handlers accumulate data in the form_state by webmaster@1: // altering the $form_state variable, which is passed into them by webmaster@1: // reference. webmaster@1: drupal_process_form($form_id, $form, $form_state); webmaster@1: if ($cacheable && !empty($form['#cache'])) { webmaster@1: // Caching is done past drupal_process_form so #process callbacks can webmaster@1: // set #cache. By not sending the form state, we avoid storing webmaster@1: // $form_state['storage']. webmaster@1: form_set_cache($form_build_id, $original_form, NULL); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Most simple, single-step forms will be finished by this point -- webmaster@1: // drupal_process_form() usually redirects to another page (or to webmaster@1: // a 'fresh' copy of the form) once processing is complete. If one webmaster@1: // of the form's handlers has set $form_state['redirect'] to FALSE, webmaster@1: // the form will simply be re-rendered with the values still in its webmaster@1: // fields. webmaster@1: // webmaster@1: // If $form_state['storage'] or $form_state['rebuild'] have been webmaster@1: // set by any submit or validate handlers, however, we know that webmaster@1: // we're in a complex multi-part process of some sort and the form's webmaster@1: // workflow is NOT complete. We need to construct a fresh copy of webmaster@1: // the form, passing in the latest $form_state in addition to any webmaster@1: // other variables passed into drupal_get_form(). webmaster@1: webmaster@1: if (!empty($form_state['rebuild']) || !empty($form_state['storage'])) { webmaster@1: $form = drupal_rebuild_form($form_id, $form_state, $args); webmaster@1: } webmaster@1: webmaster@1: // If we haven't redirected to a new location by now, we want to webmaster@1: // render whatever form array is currently in hand. webmaster@1: return drupal_render_form($form_id, $form); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Retrieves a form, caches it and processes it with an empty $_POST. webmaster@1: * webmaster@1: * This function clears $_POST and passes the empty $_POST to the form_builder. webmaster@1: * To preserve some parts from $_POST, pass them in $form_state. webmaster@1: * webmaster@1: * If your AHAH callback simulates the pressing of a button, then your AHAH webmaster@1: * callback will need to do the same as what drupal_get_form would do when the webmaster@1: * button is pressed: get the form from the cache, run drupal_process_form over webmaster@1: * it and then if it needs rebuild, run drupal_rebuild_form over it. Then send webmaster@1: * back a part of the returned form. webmaster@1: * $form_state['clicked_button']['#array_parents'] will help you to find which webmaster@1: * part. webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * The unique string identifying the desired form. If a function webmaster@1: * with that name exists, it is called to build the form array. webmaster@1: * Modules that need to generate the same form (or very similar forms) webmaster@1: * using different $form_ids can implement hook_forms(), which maps webmaster@1: * different $form_id values to the proper form constructor function. Examples webmaster@1: * may be found in node_forms(), search_forms(), and user_forms(). webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. Most webmaster@1: * important is the $form_state['storage'] collection. webmaster@1: * @param $args webmaster@1: * Any additional arguments are passed on to the functions called by webmaster@1: * drupal_get_form(), plus the original form_state in the beginning. If you webmaster@1: * are getting a form from the cache, use $form['#parameters'] to shift off webmaster@1: * the $form_id from its beginning then the resulting array can be used as webmaster@1: * $arg here. webmaster@1: * @param $form_build_id webmaster@1: * If the AHAH callback calling this function only alters part of the form, webmaster@1: * then pass in the existing form_build_id so we can re-cache with the same webmaster@1: * csid. webmaster@1: * @return webmaster@1: * The newly built form. webmaster@1: */ webmaster@1: function drupal_rebuild_form($form_id, &$form_state, $args, $form_build_id = NULL) { webmaster@1: // Remove the first argument. This is $form_id.when called from webmaster@1: // drupal_get_form and the original $form_state when called from some AHAH webmaster@1: // callback. Neither is needed. After that, put in the current state. webmaster@1: $args[0] = &$form_state; webmaster@1: // And the form_id. webmaster@1: array_unshift($args, $form_id); webmaster@1: $form = call_user_func_array('drupal_retrieve_form', $args); webmaster@1: webmaster@1: if (!isset($form_build_id)) { webmaster@1: // We need a new build_id for the new version of the form. webmaster@1: $form_build_id = 'form-'. md5(mt_rand()); webmaster@1: } webmaster@1: $form['#build_id'] = $form_build_id; webmaster@1: drupal_prepare_form($form_id, $form, $form_state); webmaster@1: webmaster@1: // Now, we cache the form structure so it can be retrieved later for webmaster@1: // validation. If $form_state['storage'] is populated, we'll also cache webmaster@1: // it so that it can be used to resume complex multi-step processes. webmaster@1: form_set_cache($form_build_id, $form, $form_state); webmaster@1: webmaster@1: // Clear out all post data, as we don't want the previous step's webmaster@1: // data to pollute this one and trigger validate/submit handling, webmaster@1: // then process the form for rendering. webmaster@1: $_POST = array(); webmaster@1: $form['#post'] = array(); webmaster@1: drupal_process_form($form_id, $form, $form_state); webmaster@1: return $form; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Fetch a form from cache. webmaster@1: */ webmaster@1: function form_get_cache($form_build_id, &$form_state) { webmaster@1: if ($cached = cache_get('form_'. $form_build_id, 'cache_form')) { webmaster@1: $form = $cached->data; webmaster@1: if ($cached = cache_get('storage_'. $form_build_id, 'cache_form')) { webmaster@1: $form_state['storage'] = $cached->data; webmaster@1: } webmaster@1: return $form; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Store a form in the cache webmaster@1: */ webmaster@1: function form_set_cache($form_build_id, $form, $form_state) { webmaster@7: // 6 hours cache life time for forms should be plenty. webmaster@7: $expire = 21600; webmaster@1: webmaster@7: cache_set('form_'. $form_build_id, $form, 'cache_form', time() + $expire); webmaster@1: if (!empty($form_state['storage'])) { webmaster@7: cache_set('storage_'. $form_build_id, $form_state['storage'], 'cache_form', time() + $expire); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Retrieves a form using a form_id, populates it with $form_state['values'], webmaster@1: * processes it, and returns any validation errors encountered. This webmaster@1: * function is the programmatic counterpart to drupal_get_form(). webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * The unique string identifying the desired form. If a function webmaster@1: * with that name exists, it is called to build the form array. webmaster@1: * Modules that need to generate the same form (or very similar forms) webmaster@1: * using different $form_ids can implement hook_forms(), which maps webmaster@1: * different $form_id values to the proper form constructor function. Examples webmaster@1: * may be found in node_forms(), search_forms(), and user_forms(). webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. Most webmaster@1: * important is the $form_state['values'] collection, a tree of data webmaster@1: * used to simulate the incoming $_POST information from a user's webmaster@1: * form submission. webmaster@1: * @param ... webmaster@1: * Any additional arguments are passed on to the functions called by webmaster@1: * drupal_execute(), including the unique form constructor function. webmaster@1: * For example, the node_edit form requires that a node object be passed webmaster@1: * in here when it is called. webmaster@1: * For example: webmaster@1: * webmaster@1: * // register a new user webmaster@1: * $form_state = array(); webmaster@1: * $form_state['values']['name'] = 'robo-user'; webmaster@1: * $form_state['values']['mail'] = 'robouser@example.com'; webmaster@1: * $form_state['values']['pass'] = 'password'; webmaster@1: * $form_state['values']['op'] = t('Create new account'); webmaster@1: * drupal_execute('user_register', $form_state); webmaster@1: * webmaster@1: * // Create a new node webmaster@1: * $form_state = array(); webmaster@1: * module_load_include('inc', 'node', 'node.pages'); webmaster@1: * $node = array('type' => 'story'); webmaster@1: * $form_state['values']['title'] = 'My node'; webmaster@1: * $form_state['values']['body'] = 'This is the body text!'; webmaster@1: * $form_state['values']['name'] = 'robo-user'; webmaster@1: * $form_state['values']['op'] = t('Save'); webmaster@1: * drupal_execute('story_node_form', $form_state, (object)$node); webmaster@1: */ webmaster@1: function drupal_execute($form_id, &$form_state) { webmaster@1: $args = func_get_args(); webmaster@1: $form = call_user_func_array('drupal_retrieve_form', $args); webmaster@1: $form['#post'] = $form_state['values']; webmaster@1: drupal_prepare_form($form_id, $form, $form_state); webmaster@1: drupal_process_form($form_id, $form, $form_state); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Retrieves the structured array that defines a given form. webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * The unique string identifying the desired form. If a function webmaster@1: * with that name exists, it is called to build the form array. webmaster@1: * Modules that need to generate the same form (or very similar forms) webmaster@1: * using different $form_ids can implement hook_forms(), which maps webmaster@1: * different $form_id values to the proper form constructor function. webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. webmaster@1: * @param ... webmaster@1: * Any additional arguments needed by the unique form constructor webmaster@1: * function. Generally, these are any arguments passed into the webmaster@1: * drupal_get_form() or drupal_execute() functions after the first webmaster@1: * argument. If a module implements hook_forms(), it can examine webmaster@1: * these additional arguments and conditionally return different webmaster@1: * builder functions as well. webmaster@1: */ webmaster@1: function drupal_retrieve_form($form_id, &$form_state) { webmaster@1: static $forms; webmaster@1: webmaster@1: // We save two copies of the incoming arguments: one for modules to use webmaster@1: // when mapping form ids to constructor functions, and another to pass to webmaster@1: // the constructor function itself. We shift out the first argument -- the webmaster@1: // $form_id itself -- from the list to pass into the constructor function, webmaster@1: // since it's already known. webmaster@1: $args = func_get_args(); webmaster@1: $saved_args = $args; webmaster@1: array_shift($args); webmaster@1: if (isset($form_state)) { webmaster@1: array_shift($args); webmaster@1: } webmaster@1: webmaster@1: // We first check to see if there's a function named after the $form_id. webmaster@1: // If there is, we simply pass the arguments on to it to get the form. webmaster@1: if (!function_exists($form_id)) { webmaster@1: // In cases where many form_ids need to share a central constructor function, webmaster@1: // such as the node editing form, modules can implement hook_forms(). It webmaster@1: // maps one or more form_ids to the correct constructor functions. webmaster@1: // webmaster@1: // We cache the results of that hook to save time, but that only works webmaster@1: // for modules that know all their form_ids in advance. (A module that webmaster@1: // adds a small 'rate this comment' form to each comment in a list webmaster@1: // would need a unique form_id for each one, for example.) webmaster@1: // webmaster@1: // So, we call the hook if $forms isn't yet populated, OR if it doesn't webmaster@1: // yet have an entry for the requested form_id. webmaster@1: if (!isset($forms) || !isset($forms[$form_id])) { webmaster@1: $forms = module_invoke_all('forms', $form_id, $args); webmaster@1: } webmaster@1: $form_definition = $forms[$form_id]; webmaster@1: if (isset($form_definition['callback arguments'])) { webmaster@1: $args = array_merge($form_definition['callback arguments'], $args); webmaster@1: } webmaster@1: if (isset($form_definition['callback'])) { webmaster@1: $callback = $form_definition['callback']; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: array_unshift($args, NULL); webmaster@1: $args[0] = &$form_state; webmaster@1: webmaster@1: // If $callback was returned by a hook_forms() implementation, call it. webmaster@1: // Otherwise, call the function named after the form id. webmaster@1: $form = call_user_func_array(isset($callback) ? $callback : $form_id, $args); webmaster@1: webmaster@1: // We store the original function arguments, rather than the final $arg webmaster@1: // value, so that form_alter functions can see what was originally webmaster@1: // passed to drupal_retrieve_form(). This allows the contents of #parameters webmaster@1: // to be saved and passed in at a later date to recreate the form. webmaster@1: $form['#parameters'] = $saved_args; webmaster@1: return $form; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * This function is the heart of form API. The form gets built, validated and in webmaster@1: * appropriate cases, submitted. webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * The unique string identifying the current form. webmaster@1: * @param $form webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. This webmaster@1: * includes the current persistent storage data for the form, and webmaster@1: * any data passed along by earlier steps when displaying a webmaster@1: * multi-step form. Additional information, like the sanitized $_POST webmaster@1: * data, is also accumulated here. webmaster@1: */ webmaster@1: function drupal_process_form($form_id, &$form, &$form_state) { webmaster@1: $form_state['values'] = array(); webmaster@1: webmaster@1: $form = form_builder($form_id, $form, $form_state); webmaster@1: // Only process the form if it is programmed or the form_id coming webmaster@1: // from the POST data is set and matches the current form_id. webmaster@1: if ((!empty($form['#programmed'])) || (!empty($form['#post']) && (isset($form['#post']['form_id']) && ($form['#post']['form_id'] == $form_id)))) { webmaster@1: drupal_validate_form($form_id, $form, $form_state); webmaster@1: webmaster@1: // form_clean_id() maintains a cache of element IDs it has seen, webmaster@1: // so it can prevent duplicates. We want to be sure we reset that webmaster@1: // cache when a form is processed, so scenerios that result in webmaster@1: // the form being built behind the scenes and again for the webmaster@1: // browser don't increment all the element IDs needlessly. webmaster@1: form_clean_id(NULL, TRUE); webmaster@1: webmaster@1: if ((!empty($form_state['submitted'])) && !form_get_errors() && empty($form_state['rebuild'])) { webmaster@1: $form_state['redirect'] = NULL; webmaster@1: form_execute_handlers('submit', $form, $form_state); webmaster@1: webmaster@1: // We'll clear out the cached copies of the form and its stored data webmaster@1: // here, as we've finished with them. The in-memory copies are still webmaster@1: // here, though. webmaster@1: if (variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED && !empty($form_state['values']['form_build_id'])) { webmaster@1: cache_clear_all('form_'. $form_state['values']['form_build_id'], 'cache_form'); webmaster@1: cache_clear_all('storage_'. $form_state['values']['form_build_id'], 'cache_form'); webmaster@1: } webmaster@1: webmaster@1: // If batches were set in the submit handlers, we process them now, webmaster@1: // possibly ending execution. We make sure we do not react to the batch webmaster@1: // that is already being processed (if a batch operation performs a webmaster@1: // drupal_execute). webmaster@1: if ($batch =& batch_get() && !isset($batch['current_set'])) { webmaster@1: // The batch uses its own copies of $form and $form_state for webmaster@1: // late execution of submit handers and post-batch redirection. webmaster@1: $batch['form'] = $form; webmaster@1: $batch['form_state'] = $form_state; webmaster@1: $batch['progressive'] = !$form['#programmed']; webmaster@1: batch_process(); webmaster@1: // Execution continues only for programmatic forms. webmaster@1: // For 'regular' forms, we get redirected to the batch processing webmaster@1: // page. Form redirection will be handled in _batch_finished(), webmaster@1: // after the batch is processed. webmaster@1: } webmaster@1: webmaster@1: // If no submit handlers have populated the $form_state['storage'] webmaster@1: // bundle, and the $form_state['rebuild'] flag has not been set, webmaster@1: // we're finished and should redirect to a new destination page webmaster@1: // if one has been set (and a fresh, unpopulated copy of the form webmaster@1: // if one hasn't). If the form was called by drupal_execute(), webmaster@1: // however, we'll skip this and let the calling function examine webmaster@1: // the resulting $form_state bundle itself. webmaster@1: if (!$form['#programmed'] && empty($form_state['rebuild']) && empty($form_state['storage'])) { webmaster@1: drupal_redirect_form($form, $form_state['redirect']); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Prepares a structured form array by adding required elements, webmaster@1: * executing any hook_form_alter functions, and optionally inserting webmaster@1: * a validation token to prevent tampering. webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * A unique string identifying the form for validation, submission, webmaster@1: * theming, and hook_form_alter functions. webmaster@1: * @param $form webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. Passed webmaster@1: * in here so that hook_form_alter() calls can use it, as well. webmaster@1: */ webmaster@1: function drupal_prepare_form($form_id, &$form, &$form_state) { webmaster@1: global $user; webmaster@1: webmaster@1: $form['#type'] = 'form'; webmaster@1: $form['#programmed'] = isset($form['#post']); webmaster@1: webmaster@1: if (isset($form['#build_id'])) { webmaster@1: $form['form_build_id'] = array( webmaster@1: '#type' => 'hidden', webmaster@1: '#value' => $form['#build_id'], webmaster@1: '#id' => $form['#build_id'], webmaster@1: '#name' => 'form_build_id', webmaster@1: ); webmaster@1: } webmaster@1: webmaster@1: // Add a token, based on either #token or form_id, to any form displayed to webmaster@1: // authenticated users. This ensures that any submitted form was actually webmaster@1: // requested previously by the user and protects against cross site request webmaster@1: // forgeries. webmaster@1: if (isset($form['#token'])) { webmaster@1: if ($form['#token'] === FALSE || $user->uid == 0 || $form['#programmed']) { webmaster@1: unset($form['#token']); webmaster@1: } webmaster@1: else { webmaster@1: $form['form_token'] = array('#type' => 'token', '#default_value' => drupal_get_token($form['#token'])); webmaster@1: } webmaster@1: } webmaster@1: else if (isset($user->uid) && $user->uid && !$form['#programmed']) { webmaster@1: $form['#token'] = $form_id; webmaster@1: $form['form_token'] = array( webmaster@1: '#id' => form_clean_id('edit-'. $form_id .'-form-token'), webmaster@1: '#type' => 'token', webmaster@1: '#default_value' => drupal_get_token($form['#token']), webmaster@1: ); webmaster@1: } webmaster@1: webmaster@1: if (isset($form_id)) { webmaster@1: $form['form_id'] = array( webmaster@1: '#type' => 'hidden', webmaster@1: '#value' => $form_id, webmaster@1: '#id' => form_clean_id("edit-$form_id"), webmaster@1: ); webmaster@1: } webmaster@1: if (!isset($form['#id'])) { webmaster@1: $form['#id'] = form_clean_id($form_id); webmaster@1: } webmaster@1: webmaster@1: $form += _element_info('form'); webmaster@1: webmaster@1: if (!isset($form['#validate'])) { webmaster@1: if (function_exists($form_id .'_validate')) { webmaster@1: $form['#validate'] = array($form_id .'_validate'); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: if (!isset($form['#submit'])) { webmaster@1: if (function_exists($form_id .'_submit')) { webmaster@1: // We set submit here so that it can be altered. webmaster@1: $form['#submit'] = array($form_id .'_submit'); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Normally, we would call drupal_alter($form_id, $form, $form_state). webmaster@1: // However, drupal_alter() normally supports just one byref parameter. Using webmaster@1: // the __drupal_alter_by_ref key, we can store any additional parameters webmaster@1: // that need to be altered, and they'll be split out into additional params webmaster@1: // for the hook_form_alter() implementations. webmaster@1: // @todo: Remove this in Drupal 7. webmaster@1: $data = &$form; webmaster@1: $data['__drupal_alter_by_ref'] = array(&$form_state); webmaster@1: drupal_alter('form_'. $form_id, $data); webmaster@1: webmaster@1: // __drupal_alter_by_ref is unset in the drupal_alter() function, we need webmaster@1: // to repopulate it to ensure both calls get the data. webmaster@1: $data['__drupal_alter_by_ref'] = array(&$form_state); webmaster@1: drupal_alter('form', $data, $form_id); webmaster@1: } webmaster@1: webmaster@1: webmaster@1: /** webmaster@1: * Validates user-submitted form data from the $form_state using webmaster@1: * the validate functions defined in a structured form array. webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * A unique string identifying the form for validation, submission, webmaster@1: * theming, and hook_form_alter functions. webmaster@1: * @param $form webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. The current webmaster@1: * user-submitted data is stored in $form_state['values'], though webmaster@1: * form validation functions are passed an explicit copy of the webmaster@1: * values for the sake of simplicity. Validation handlers can also webmaster@1: * $form_state to pass information on to submit handlers. For example: webmaster@1: * $form_state['data_for_submision'] = $data; webmaster@1: * This technique is useful when validation requires file parsing, webmaster@1: * web service requests, or other expensive requests that should webmaster@1: * not be repeated in the submission step. webmaster@1: */ webmaster@1: function drupal_validate_form($form_id, $form, &$form_state) { webmaster@1: static $validated_forms = array(); webmaster@1: webmaster@1: if (isset($validated_forms[$form_id])) { webmaster@1: return; webmaster@1: } webmaster@1: webmaster@1: // If the session token was set by drupal_prepare_form(), ensure that it webmaster@1: // matches the current user's session. webmaster@1: if (isset($form['#token'])) { webmaster@1: if (!drupal_valid_token($form_state['values']['form_token'], $form['#token'])) { webmaster@1: // Setting this error will cause the form to fail validation. webmaster@1: form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.')); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: _form_validate($form, $form_state, $form_id); webmaster@1: $validated_forms[$form_id] = TRUE; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Renders a structured form array into themed HTML. webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * A unique string identifying the form for validation, submission, webmaster@1: * theming, and hook_form_alter functions. webmaster@1: * @param $form webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @return webmaster@1: * A string containing the path of the page to display when processing webmaster@1: * is complete. webmaster@1: */ webmaster@1: function drupal_render_form($form_id, &$form) { webmaster@1: // Don't override #theme if someone already set it. webmaster@1: if (!isset($form['#theme'])) { webmaster@1: init_theme(); webmaster@1: $registry = theme_get_registry(); webmaster@1: if (isset($registry[$form_id])) { webmaster@1: $form['#theme'] = $form_id; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: $output = drupal_render($form); webmaster@1: return $output; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Redirect the user to a URL after a form has been processed. webmaster@1: * webmaster@1: * @param $form webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @param $redirect webmaster@1: * An optional value containing the destination path to redirect webmaster@1: * to if none is specified by the form. webmaster@1: */ webmaster@1: function drupal_redirect_form($form, $redirect = NULL) { webmaster@1: $goto = NULL; webmaster@1: if (isset($redirect)) { webmaster@1: $goto = $redirect; webmaster@1: } webmaster@1: if ($goto !== FALSE && isset($form['#redirect'])) { webmaster@1: $goto = $form['#redirect']; webmaster@1: } webmaster@1: if (!isset($goto) || ($goto !== FALSE)) { webmaster@1: if (isset($goto)) { webmaster@1: if (is_array($goto)) { webmaster@1: call_user_func_array('drupal_goto', $goto); webmaster@1: } webmaster@1: else { webmaster@1: drupal_goto($goto); webmaster@1: } webmaster@1: } webmaster@1: drupal_goto($_GET['q']); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Performs validation on form elements. First ensures required fields are webmaster@1: * completed, #maxlength is not exceeded, and selected options were in the webmaster@1: * list of options given to the user. Then calls user-defined validators. webmaster@1: * webmaster@1: * @param $elements webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. The current webmaster@1: * user-submitted data is stored in $form_state['values'], though webmaster@1: * form validation functions are passed an explicit copy of the webmaster@1: * values for the sake of simplicity. Validation handlers can also webmaster@1: * $form_state to pass information on to submit handlers. For example: webmaster@1: * $form_state['data_for_submision'] = $data; webmaster@1: * This technique is useful when validation requires file parsing, webmaster@1: * web service requests, or other expensive requests that should webmaster@1: * not be repeated in the submission step. webmaster@1: * @param $form_id webmaster@1: * A unique string identifying the form for validation, submission, webmaster@1: * theming, and hook_form_alter functions. webmaster@1: */ webmaster@1: function _form_validate($elements, &$form_state, $form_id = NULL) { webmaster@1: static $complete_form; webmaster@1: webmaster@1: // Also used in the installer, pre-database setup. webmaster@1: $t = get_t(); webmaster@1: webmaster@1: // Recurse through all children. webmaster@1: foreach (element_children($elements) as $key) { webmaster@1: if (isset($elements[$key]) && $elements[$key]) { webmaster@1: _form_validate($elements[$key], $form_state); webmaster@1: } webmaster@1: } webmaster@1: // Validate the current input. webmaster@1: if (!isset($elements['#validated']) || !$elements['#validated']) { webmaster@1: if (isset($elements['#needs_validation'])) { webmaster@1: // Make sure a value is passed when the field is required. webmaster@1: // A simple call to empty() will not cut it here as some fields, like webmaster@1: // checkboxes, can return a valid value of '0'. Instead, check the webmaster@1: // length if it's a string, and the item count if it's an array. webmaster@1: if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0))) { webmaster@1: form_error($elements, $t('!name field is required.', array('!name' => $elements['#title']))); webmaster@1: } webmaster@1: webmaster@1: // Verify that the value is not longer than #maxlength. webmaster@1: if (isset($elements['#maxlength']) && drupal_strlen($elements['#value']) > $elements['#maxlength']) { webmaster@1: form_error($elements, $t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => drupal_strlen($elements['#value'])))); webmaster@1: } webmaster@1: webmaster@1: if (isset($elements['#options']) && isset($elements['#value'])) { webmaster@1: if ($elements['#type'] == 'select') { webmaster@1: $options = form_options_flatten($elements['#options']); webmaster@1: } webmaster@1: else { webmaster@1: $options = $elements['#options']; webmaster@1: } webmaster@1: if (is_array($elements['#value'])) { webmaster@1: $value = $elements['#type'] == 'checkboxes' ? array_keys(array_filter($elements['#value'])) : $elements['#value']; webmaster@1: foreach ($value as $v) { webmaster@1: if (!isset($options[$v])) { webmaster@1: form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.')); webmaster@1: watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: elseif (!isset($options[$elements['#value']])) { webmaster@1: form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.')); webmaster@1: watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Call user-defined form level validators and store a copy of the full webmaster@1: // form so that element-specific validators can examine the entire structure webmaster@1: // if necessary. webmaster@1: if (isset($form_id)) { webmaster@1: form_execute_handlers('validate', $elements, $form_state); webmaster@1: $complete_form = $elements; webmaster@1: } webmaster@1: // Call any element-specific validators. These must act on the element webmaster@1: // #value data. webmaster@1: elseif (isset($elements['#element_validate'])) { webmaster@1: foreach ($elements['#element_validate'] as $function) { webmaster@1: if (function_exists($function)) { webmaster@1: $function($elements, $form_state, $complete_form); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: $elements['#validated'] = TRUE; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * A helper function used to execute custom validation and submission webmaster@1: * handlers for a given form. Button-specific handlers are checked webmaster@1: * first. If none exist, the function falls back to form-level handlers. webmaster@1: * webmaster@1: * @param $type webmaster@1: * The type of handler to execute. 'validate' or 'submit' are the webmaster@1: * defaults used by Form API. webmaster@1: * @param $form webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. If the user webmaster@1: * submitted the form by clicking a button with custom handler functions webmaster@1: * defined, those handlers will be stored here. webmaster@1: */ webmaster@1: function form_execute_handlers($type, &$form, &$form_state) { webmaster@1: $return = FALSE; webmaster@1: if (isset($form_state[$type .'_handlers'])) { webmaster@1: $handlers = $form_state[$type .'_handlers']; webmaster@1: } webmaster@1: elseif (isset($form['#'. $type])) { webmaster@1: $handlers = $form['#'. $type]; webmaster@1: } webmaster@1: else { webmaster@1: $handlers = array(); webmaster@1: } webmaster@1: webmaster@1: foreach ($handlers as $function) { webmaster@1: if (function_exists($function)) { webmaster@1: if ($type == 'submit' && ($batch =& batch_get())) { webmaster@1: // Some previous _submit handler has set a batch. We store the call webmaster@1: // in a special 'control' batch set, for execution at the correct webmaster@1: // time during the batch processing workflow. webmaster@1: $batch['sets'][] = array('form_submit' => $function); webmaster@1: } webmaster@1: else { webmaster@1: $function($form, $form_state); webmaster@1: } webmaster@1: $return = TRUE; webmaster@1: } webmaster@1: } webmaster@1: return $return; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * File an error against a form element. webmaster@1: * webmaster@1: * @param $name webmaster@1: * The name of the form element. If the #parents property of your form webmaster@1: * element is array('foo', 'bar', 'baz') then you may set an error on 'foo' webmaster@1: * or 'foo][bar][baz'. Setting an error on 'foo' sets an error for every webmaster@1: * element where the #parents array starts with 'foo'. webmaster@1: * @param $message webmaster@1: * The error message to present to the user. webmaster@1: * @return webmaster@1: * Never use the return value of this function, use form_get_errors and webmaster@1: * form_get_error instead. webmaster@1: */ webmaster@1: function form_set_error($name = NULL, $message = '') { webmaster@1: static $form = array(); webmaster@1: if (isset($name) && !isset($form[$name])) { webmaster@1: $form[$name] = $message; webmaster@1: if ($message) { webmaster@1: drupal_set_message($message, 'error'); webmaster@1: } webmaster@1: } webmaster@1: return $form; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return an associative array of all errors. webmaster@1: */ webmaster@1: function form_get_errors() { webmaster@1: $form = form_set_error(); webmaster@1: if (!empty($form)) { webmaster@1: return $form; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return the error message filed against the form with the specified name. webmaster@1: */ webmaster@1: function form_get_error($element) { webmaster@1: $form = form_set_error(); webmaster@1: $key = $element['#parents'][0]; webmaster@1: if (isset($form[$key])) { webmaster@1: return $form[$key]; webmaster@1: } webmaster@1: $key = implode('][', $element['#parents']); webmaster@1: if (isset($form[$key])) { webmaster@1: return $form[$key]; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Flag an element as having an error. webmaster@1: */ webmaster@1: function form_error(&$element, $message = '') { webmaster@1: form_set_error(implode('][', $element['#parents']), $message); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Walk through the structured form array, adding any required webmaster@1: * properties to each element and mapping the incoming $_POST webmaster@1: * data to the proper elements. webmaster@1: * webmaster@1: * @param $form_id webmaster@1: * A unique string identifying the form for validation, submission, webmaster@1: * theming, and hook_form_alter functions. webmaster@1: * @param $form webmaster@1: * An associative array containing the structure of the form. webmaster@1: * @param $form_state webmaster@1: * A keyed array containing the current state of the form. In this webmaster@1: * context, it is used to accumulate information about which button webmaster@1: * was clicked when the form was submitted, as well as the sanitized webmaster@1: * $_POST data. webmaster@1: */ webmaster@1: function form_builder($form_id, $form, &$form_state) { webmaster@1: static $complete_form, $cache; webmaster@1: webmaster@1: // Initialize as unprocessed. webmaster@1: $form['#processed'] = FALSE; webmaster@1: webmaster@1: // Use element defaults. webmaster@1: if ((!empty($form['#type'])) && ($info = _element_info($form['#type']))) { webmaster@1: // Overlay $info onto $form, retaining preexisting keys in $form. webmaster@1: $form += $info; webmaster@1: } webmaster@1: webmaster@1: if (isset($form['#type']) && $form['#type'] == 'form') { webmaster@1: $cache = NULL; webmaster@1: $complete_form = $form; webmaster@1: if (!empty($form['#programmed'])) { webmaster@1: $form_state['submitted'] = TRUE; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: if (isset($form['#input']) && $form['#input']) { webmaster@1: _form_builder_handle_input_element($form_id, $form, $form_state, $complete_form); webmaster@1: } webmaster@1: $form['#defaults_loaded'] = TRUE; webmaster@1: webmaster@1: // We start off assuming all form elements are in the correct order. webmaster@1: $form['#sorted'] = TRUE; webmaster@1: webmaster@1: // Recurse through all child elements. webmaster@1: $count = 0; webmaster@1: foreach (element_children($form) as $key) { webmaster@1: $form[$key]['#post'] = $form['#post']; webmaster@1: $form[$key]['#programmed'] = $form['#programmed']; webmaster@1: // Don't squash an existing tree value. webmaster@1: if (!isset($form[$key]['#tree'])) { webmaster@1: $form[$key]['#tree'] = $form['#tree']; webmaster@1: } webmaster@1: webmaster@1: // Deny access to child elements if parent is denied. webmaster@1: if (isset($form['#access']) && !$form['#access']) { webmaster@1: $form[$key]['#access'] = FALSE; webmaster@1: } webmaster@1: webmaster@1: // Don't squash existing parents value. webmaster@1: if (!isset($form[$key]['#parents'])) { webmaster@1: // Check to see if a tree of child elements is present. If so, webmaster@1: // continue down the tree if required. webmaster@1: $form[$key]['#parents'] = $form[$key]['#tree'] && $form['#tree'] ? array_merge($form['#parents'], array($key)) : array($key); webmaster@1: $array_parents = isset($form['#array_parents']) ? $form['#array_parents'] : array(); webmaster@1: $array_parents[] = $key; webmaster@1: $form[$key]['#array_parents'] = $array_parents; webmaster@1: } webmaster@1: webmaster@1: // Assign a decimal placeholder weight to preserve original array order. webmaster@1: if (!isset($form[$key]['#weight'])) { webmaster@1: $form[$key]['#weight'] = $count/1000; webmaster@1: } webmaster@1: else { webmaster@1: // If one of the child elements has a weight then we will need to sort webmaster@1: // later. webmaster@1: unset($form['#sorted']); webmaster@1: } webmaster@1: $form[$key] = form_builder($form_id, $form[$key], $form_state); webmaster@1: $count++; webmaster@1: } webmaster@1: webmaster@1: // The #after_build flag allows any piece of a form to be altered webmaster@1: // after normal input parsing has been completed. webmaster@1: if (isset($form['#after_build']) && !isset($form['#after_build_done'])) { webmaster@1: foreach ($form['#after_build'] as $function) { webmaster@1: $form = $function($form, $form_state); webmaster@1: $form['#after_build_done'] = TRUE; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Now that we've processed everything, we can go back to handle the funky webmaster@1: // Internet Explorer button-click scenario. webmaster@1: _form_builder_ie_cleanup($form, $form_state); webmaster@1: webmaster@1: // We shoud keep the buttons array until the IE clean up function webmaster@1: // has recognized the submit button so the form has been marked webmaster@1: // as submitted. If we already know which button was submitted, webmaster@1: // we don't need the array. webmaster@1: if (!empty($form_state['submitted'])) { webmaster@1: unset($form_state['buttons']); webmaster@1: } webmaster@1: webmaster@1: // If some callback set #cache, we need to flip a static flag so later it webmaster@1: // can be found. webmaster@1: if (isset($form['#cache'])) { webmaster@1: $cache = $form['#cache']; webmaster@1: } webmaster@1: // We are on the top form, we can copy back #cache if it's set. webmaster@1: if (isset($form['#type']) && $form['#type'] == 'form' && isset($cache)) { webmaster@1: $form['#cache'] = TRUE; webmaster@1: } webmaster@1: return $form; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Populate the #value and #name properties of input elements so they webmaster@1: * can be processed and rendered. Also, execute any #process handlers webmaster@1: * attached to a specific element. webmaster@1: */ webmaster@1: function _form_builder_handle_input_element($form_id, &$form, &$form_state, $complete_form) { webmaster@1: if (!isset($form['#name'])) { webmaster@1: $name = array_shift($form['#parents']); webmaster@1: $form['#name'] = $name; webmaster@1: if ($form['#type'] == 'file') { webmaster@1: // To make it easier to handle $_FILES in file.inc, we place all webmaster@1: // file fields in the 'files' array. Also, we do not support webmaster@1: // nested file names. webmaster@1: $form['#name'] = 'files['. $form['#name'] .']'; webmaster@1: } webmaster@1: elseif (count($form['#parents'])) { webmaster@1: $form['#name'] .= '['. implode('][', $form['#parents']) .']'; webmaster@1: } webmaster@1: array_unshift($form['#parents'], $name); webmaster@1: } webmaster@1: if (!isset($form['#id'])) { webmaster@1: $form['#id'] = form_clean_id('edit-'. implode('-', $form['#parents'])); webmaster@1: } webmaster@1: webmaster@1: unset($edit); webmaster@1: if (!empty($form['#disabled'])) { webmaster@1: $form['#attributes']['disabled'] = 'disabled'; webmaster@1: } webmaster@1: webmaster@1: if (!isset($form['#value']) && !array_key_exists('#value', $form)) { webmaster@1: $function = !empty($form['#value_callback']) ? $form['#value_callback'] : 'form_type_'. $form['#type'] .'_value'; webmaster@1: if (($form['#programmed']) || ((!isset($form['#access']) || $form['#access']) && isset($form['#post']) && (isset($form['#post']['form_id']) && $form['#post']['form_id'] == $form_id))) { webmaster@1: $edit = $form['#post']; webmaster@1: foreach ($form['#parents'] as $parent) { webmaster@1: $edit = isset($edit[$parent]) ? $edit[$parent] : NULL; webmaster@1: } webmaster@1: if (!$form['#programmed'] || isset($edit)) { webmaster@1: // Call #type_value to set the form value; webmaster@1: if (function_exists($function)) { webmaster@1: $form['#value'] = $function($form, $edit); webmaster@1: } webmaster@1: if (!isset($form['#value']) && isset($edit)) { webmaster@1: $form['#value'] = $edit; webmaster@1: } webmaster@1: } webmaster@1: // Mark all posted values for validation. webmaster@1: if (isset($form['#value']) || (isset($form['#required']) && $form['#required'])) { webmaster@1: $form['#needs_validation'] = TRUE; webmaster@1: } webmaster@1: } webmaster@1: // Load defaults. webmaster@1: if (!isset($form['#value'])) { webmaster@1: // Call #type_value without a second argument to request default_value handling. webmaster@1: if (function_exists($function)) { webmaster@1: $form['#value'] = $function($form); webmaster@1: } webmaster@1: // Final catch. If we haven't set a value yet, use the explicit default value. webmaster@1: // Avoid image buttons (which come with garbage value), so we only get value webmaster@1: // for the button actually clicked. webmaster@1: if (!isset($form['#value']) && empty($form['#has_garbage_value'])) { webmaster@1: $form['#value'] = isset($form['#default_value']) ? $form['#default_value'] : ''; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Determine which button (if any) was clicked to submit the form. webmaster@1: // We compare the incoming values with the buttons defined in the form, webmaster@1: // and flag the one that matches. We have to do some funky tricks to webmaster@1: // deal with Internet Explorer's handling of single-button forms, though. webmaster@1: if (!empty($form['#post']) && isset($form['#executes_submit_callback'])) { webmaster@1: // First, accumulate a collection of buttons, divided into two bins: webmaster@1: // those that execute full submit callbacks and those that only validate. webmaster@1: $button_type = $form['#executes_submit_callback'] ? 'submit' : 'button'; webmaster@1: $form_state['buttons'][$button_type][] = $form; webmaster@1: webmaster@1: if (_form_button_was_clicked($form)) { webmaster@1: $form_state['submitted'] = $form_state['submitted'] || $form['#executes_submit_callback']; webmaster@1: webmaster@1: // In most cases, we want to use form_set_value() to manipulate webmaster@1: // the global variables. In this special case, we want to make sure that webmaster@1: // the value of this element is listed in $form_variables under 'op'. webmaster@1: $form_state['values'][$form['#name']] = $form['#value']; webmaster@1: $form_state['clicked_button'] = $form; webmaster@1: webmaster@1: if (isset($form['#validate'])) { webmaster@1: $form_state['validate_handlers'] = $form['#validate']; webmaster@1: } webmaster@1: if (isset($form['#submit'])) { webmaster@1: $form_state['submit_handlers'] = $form['#submit']; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: // Allow for elements to expand to multiple elements, e.g., radios, webmaster@1: // checkboxes and files. webmaster@1: if (isset($form['#process']) && !$form['#processed']) { webmaster@1: foreach ($form['#process'] as $process) { webmaster@1: if (function_exists($process)) { webmaster@1: $form = $process($form, isset($edit) ? $edit : NULL, $form_state, $complete_form); webmaster@1: } webmaster@1: } webmaster@1: $form['#processed'] = TRUE; webmaster@1: } webmaster@1: form_set_value($form, $form['#value'], $form_state); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to handle the sometimes-convoluted logic of button webmaster@1: * click detection. webmaster@1: * webmaster@1: * In Internet Explorer, if ONLY one submit button is present, AND the webmaster@1: * enter key is used to submit the form, no form value is sent for it webmaster@1: * and we'll never detect a match. That special case is handled by webmaster@1: * _form_builder_ie_cleanup(). webmaster@1: */ webmaster@1: function _form_button_was_clicked($form) { webmaster@1: // First detect normal 'vanilla' button clicks. Traditionally, all webmaster@1: // standard buttons on a form share the same name (usually 'op'), webmaster@1: // and the specific return value is used to determine which was webmaster@1: // clicked. This ONLY works as long as $form['#name'] puts the webmaster@1: // value at the top level of the tree of $_POST data. webmaster@1: if (isset($form['#post'][$form['#name']]) && $form['#post'][$form['#name']] == $form['#value']) { webmaster@1: return TRUE; webmaster@1: } webmaster@1: // When image buttons are clicked, browsers do NOT pass the form element webmaster@1: // value in $_POST. Instead they pass an integer representing the webmaster@1: // coordinates of the click on the button image. This means that image webmaster@1: // buttons MUST have unique $form['#name'] values, but the details of webmaster@1: // their $_POST data should be ignored. webmaster@1: elseif (!empty($form['#has_garbage_value']) && isset($form['#value']) && $form['#value'] !== '') { webmaster@1: return TRUE; webmaster@1: } webmaster@1: return FALSE; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * In IE, if only one submit button is present, AND the enter key is webmaster@1: * used to submit the form, no form value is sent for it and our normal webmaster@1: * button detection code will never detect a match. We call this webmaster@1: * function after all other button-detection is complete to check webmaster@1: * for the proper conditions, and treat the single button on the form webmaster@1: * as 'clicked' if they are met. webmaster@1: */ webmaster@1: function _form_builder_ie_cleanup($form, &$form_state) { webmaster@1: // Quick check to make sure we're always looking at the full form webmaster@1: // and not a sub-element. webmaster@1: if (!empty($form['#type']) && $form['#type'] == 'form') { webmaster@1: // If we haven't recognized a submission yet, and there's a single webmaster@1: // submit button, we know that we've hit the right conditions. Grab webmaster@1: // the first one and treat it as the clicked button. webmaster@1: if (empty($form_state['submitted']) && !empty($form_state['buttons']['submit']) && empty($form_state['buttons']['button'])) { webmaster@1: $button = $form_state['buttons']['submit'][0]; webmaster@1: webmaster@1: // Set up all the $form_state information that would have been webmaster@1: // populated had the button been recognized earlier. webmaster@1: $form_state['submitted'] = TRUE; webmaster@1: $form_state['submit_handlers'] = empty($button['#submit']) ? NULL : $button['#submit']; webmaster@1: $form_state['validate_handlers'] = empty($button['#validate']) ? NULL : $button['#validate']; webmaster@1: $form_state['values'][$button['#name']] = $button['#value']; webmaster@1: $form_state['clicked_button'] = $button; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to determine the value for an image button form element. webmaster@1: * webmaster@1: * @param $form webmaster@1: * The form element whose value is being populated. webmaster@1: * @param $edit webmaster@1: * The incoming POST data to populate the form element. If this is FALSE, webmaster@1: * the element's default value should be returned. webmaster@1: * @return webmaster@1: * The data that will appear in the $form_state['values'] collection webmaster@1: * for this element. Return nothing to use the default. webmaster@1: */ webmaster@1: function form_type_image_button_value($form, $edit = FALSE) { webmaster@1: if ($edit !== FALSE) { webmaster@1: if (!empty($edit)) { webmaster@1: // If we're dealing with Mozilla or Opera, we're lucky. It will webmaster@1: // return a proper value, and we can get on with things. webmaster@1: return $form['#return_value']; webmaster@1: } webmaster@1: else { webmaster@1: // Unfortunately, in IE we never get back a proper value for THIS webmaster@1: // form element. Instead, we get back two split values: one for the webmaster@1: // X and one for the Y coordinates on which the user clicked the webmaster@1: // button. We'll find this element in the #post data, and search webmaster@1: // in the same spot for its name, with '_x'. webmaster@1: $post = $form['#post']; webmaster@1: foreach (split('\[', $form['#name']) as $element_name) { webmaster@1: // chop off the ] that may exist. webmaster@1: if (substr($element_name, -1) == ']') { webmaster@1: $element_name = substr($element_name, 0, -1); webmaster@1: } webmaster@1: webmaster@1: if (!isset($post[$element_name])) { webmaster@1: if (isset($post[$element_name .'_x'])) { webmaster@1: return $form['#return_value']; webmaster@1: } webmaster@1: return NULL; webmaster@1: } webmaster@1: $post = $post[$element_name]; webmaster@1: } webmaster@1: return $form['#return_value']; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to determine the value for a checkbox form element. webmaster@1: * webmaster@1: * @param $form webmaster@1: * The form element whose value is being populated. webmaster@1: * @param $edit webmaster@1: * The incoming POST data to populate the form element. If this is FALSE, webmaster@1: * the element's default value should be returned. webmaster@1: * @return webmaster@1: * The data that will appear in the $form_state['values'] collection webmaster@1: * for this element. Return nothing to use the default. webmaster@1: */ webmaster@1: function form_type_checkbox_value($form, $edit = FALSE) { webmaster@1: if ($edit !== FALSE) { webmaster@1: return !empty($edit) ? $form['#return_value'] : 0; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to determine the value for a checkboxes form element. webmaster@1: * webmaster@1: * @param $form webmaster@1: * The form element whose value is being populated. webmaster@1: * @param $edit webmaster@1: * The incoming POST data to populate the form element. If this is FALSE, webmaster@1: * the element's default value should be returned. webmaster@1: * @return webmaster@1: * The data that will appear in the $form_state['values'] collection webmaster@1: * for this element. Return nothing to use the default. webmaster@1: */ webmaster@1: function form_type_checkboxes_value($form, $edit = FALSE) { webmaster@1: if ($edit === FALSE) { webmaster@1: $value = array(); webmaster@1: $form += array('#default_value' => array()); webmaster@1: foreach ($form['#default_value'] as $key) { webmaster@1: $value[$key] = 1; webmaster@1: } webmaster@1: return $value; webmaster@1: } webmaster@1: elseif (!isset($edit)) { webmaster@1: return array(); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to determine the value for a password_confirm form webmaster@1: * element. webmaster@1: * webmaster@1: * @param $form webmaster@1: * The form element whose value is being populated. webmaster@1: * @param $edit webmaster@1: * The incoming POST data to populate the form element. If this is FALSE, webmaster@1: * the element's default value should be returned. webmaster@1: * @return webmaster@1: * The data that will appear in the $form_state['values'] collection webmaster@1: * for this element. Return nothing to use the default. webmaster@1: */ webmaster@1: function form_type_password_confirm_value($form, $edit = FALSE) { webmaster@1: if ($edit === FALSE) { webmaster@1: $form += array('#default_value' => array()); webmaster@1: return $form['#default_value'] + array('pass1' => '', 'pass2' => ''); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to determine the value for a select form element. webmaster@1: * webmaster@1: * @param $form webmaster@1: * The form element whose value is being populated. webmaster@1: * @param $edit webmaster@1: * The incoming POST data to populate the form element. If this is FALSE, webmaster@1: * the element's default value should be returned. webmaster@1: * @return webmaster@1: * The data that will appear in the $form_state['values'] collection webmaster@1: * for this element. Return nothing to use the default. webmaster@1: */ webmaster@1: function form_type_select_value($form, $edit = FALSE) { webmaster@1: if ($edit !== FALSE) { webmaster@1: if (isset($form['#multiple']) && $form['#multiple']) { webmaster@1: return (is_array($edit)) ? drupal_map_assoc($edit) : array(); webmaster@1: } webmaster@1: else { webmaster@1: return $edit; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to determine the value for a textfield form element. webmaster@1: * webmaster@1: * @param $form webmaster@1: * The form element whose value is being populated. webmaster@1: * @param $edit webmaster@1: * The incoming POST data to populate the form element. If this is FALSE, webmaster@1: * the element's default value should be returned. webmaster@1: * @return webmaster@1: * The data that will appear in the $form_state['values'] collection webmaster@1: * for this element. Return nothing to use the default. webmaster@1: */ webmaster@1: function form_type_textfield_value($form, $edit = FALSE) { webmaster@1: if ($edit !== FALSE) { webmaster@1: // Equate $edit to the form value to ensure it's marked for webmaster@1: // validation. webmaster@1: return str_replace(array("\r", "\n"), '', $edit); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function to determine the value for form's token value. webmaster@1: * webmaster@1: * @param $form webmaster@1: * The form element whose value is being populated. webmaster@1: * @param $edit webmaster@1: * The incoming POST data to populate the form element. If this is FALSE, webmaster@1: * the element's default value should be returned. webmaster@1: * @return webmaster@1: * The data that will appear in the $form_state['values'] collection webmaster@1: * for this element. Return nothing to use the default. webmaster@1: */ webmaster@1: function form_type_token_value($form, $edit = FALSE) { webmaster@1: if ($edit !== FALSE) { webmaster@1: return (string)$edit; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@7: * Change submitted form values during the form processing cycle. webmaster@1: * webmaster@7: * Use this function to change the submitted value of a form item in the webmaster@7: * validation phase so that it persists in $form_state through to the webmaster@7: * submission handlers in the submission phase. webmaster@1: * webmaster@7: * Since $form_state['values'] can either be a flat array of values, or a tree webmaster@7: * of nested values, some care must be taken when using this function. webmaster@7: * Specifically, $form_item['#parents'] is an array that describes the branch of webmaster@7: * the tree whose value should be updated. For example, if we wanted to update webmaster@7: * $form_state['values']['one']['two'] to 'new value', we'd pass in webmaster@7: * $form_item['#parents'] = array('one', 'two') and $value = 'new value'. webmaster@7: * webmaster@7: * @param $form_item webmaster@7: * The form item that should have its value updated. Keys used: #parents, webmaster@7: * #value. In most cases you can just pass in the right element from the $form webmaster@7: * array. webmaster@1: * @param $value webmaster@7: * The new value for the form item. webmaster@7: * @param $form_state webmaster@7: * The array where the value change should be recorded. webmaster@1: */ webmaster@7: function form_set_value($form_item, $value, &$form_state) { webmaster@7: _form_set_value($form_state['values'], $form_item, $form_item['#parents'], $value); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Helper function for form_set_value(). webmaster@1: * webmaster@1: * We iterate over $parents and create nested arrays for them webmaster@1: * in $form_state['values'] if needed. Then we insert the value into webmaster@1: * the right array. webmaster@1: */ webmaster@7: function _form_set_value(&$form_values, $form_item, $parents, $value) { webmaster@1: $parent = array_shift($parents); webmaster@1: if (empty($parents)) { webmaster@1: $form_values[$parent] = $value; webmaster@1: } webmaster@1: else { webmaster@1: if (!isset($form_values[$parent])) { webmaster@1: $form_values[$parent] = array(); webmaster@1: } webmaster@7: _form_set_value($form_values[$parent], $form_item, $parents, $value); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Retrieve the default properties for the defined element type. webmaster@1: */ webmaster@1: function _element_info($type, $refresh = NULL) { webmaster@1: static $cache; webmaster@1: webmaster@1: $basic_defaults = array( webmaster@1: '#description' => NULL, webmaster@1: '#attributes' => array(), webmaster@1: '#required' => FALSE, webmaster@1: '#tree' => FALSE, webmaster@1: '#parents' => array() webmaster@1: ); webmaster@1: if (!isset($cache) || $refresh) { webmaster@1: $cache = array(); webmaster@1: foreach (module_implements('elements') as $module) { webmaster@1: $elements = module_invoke($module, 'elements'); webmaster@1: if (isset($elements) && is_array($elements)) { webmaster@1: $cache = array_merge_recursive($cache, $elements); webmaster@1: } webmaster@1: } webmaster@1: if (sizeof($cache)) { webmaster@1: foreach ($cache as $element_type => $info) { webmaster@1: $cache[$element_type] = array_merge_recursive($basic_defaults, $info); webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $cache[$type]; webmaster@1: } webmaster@1: webmaster@1: function form_options_flatten($array, $reset = TRUE) { webmaster@1: static $return; webmaster@1: webmaster@1: if ($reset) { webmaster@1: $return = array(); webmaster@1: } webmaster@1: webmaster@1: foreach ($array as $key => $value) { webmaster@1: if (is_object($value)) { webmaster@1: form_options_flatten($value->option, FALSE); webmaster@1: } webmaster@1: else if (is_array($value)) { webmaster@1: form_options_flatten($value, FALSE); webmaster@1: } webmaster@1: else { webmaster@1: $return[$key] = 1; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return $return; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Format a dropdown menu or scrolling selection box. webmaster@1: * webmaster@1: * @param $element webmaster@1: * An associative array containing the properties of the element. webmaster@1: * Properties used: title, value, options, description, extra, multiple, required webmaster@1: * @return webmaster@1: * A themed HTML string representing the form element. webmaster@1: * webmaster@1: * @ingroup themeable webmaster@1: * webmaster@1: * It is possible to group options together; to do this, change the format of webmaster@1: * $options to an associative array in which the keys are group labels, and the webmaster@1: * values are associative arrays in the normal $options format. webmaster@1: */ webmaster@1: function theme_select($element) { webmaster@1: $select = ''; webmaster@1: $size = $element['#size'] ? ' size="'. $element['#size'] .'"' : ''; webmaster@1: _form_set_class($element, array('form-select')); webmaster@1: $multiple = $element['#multiple']; webmaster@1: return theme('form_element', $element, ''); webmaster@1: } webmaster@1: webmaster@1: function form_select_options($element, $choices = NULL) { webmaster@1: if (!isset($choices)) { webmaster@1: $choices = $element['#options']; webmaster@1: } webmaster@1: // array_key_exists() accommodates the rare event where $element['#value'] is NULL. webmaster@1: // isset() fails in this situation. webmaster@1: $value_valid = isset($element['#value']) || array_key_exists('#value', $element); webmaster@1: $value_is_array = is_array($element['#value']); webmaster@1: $options = ''; webmaster@1: foreach ($choices as $key => $choice) { webmaster@1: if (is_array($choice)) { webmaster@1: $options .= ''; webmaster@1: } webmaster@1: elseif (is_object($choice)) { webmaster@1: $options .= form_select_options($element, $choice->option); webmaster@1: } webmaster@1: else { webmaster@1: $key = (string)$key; webmaster@1: if ($value_valid && (!$value_is_array && (string)$element['#value'] === $key || ($value_is_array && in_array($key, $element['#value'])))) { webmaster@1: $selected = ' selected="selected"'; webmaster@1: } webmaster@1: else { webmaster@1: $selected = ''; webmaster@1: } webmaster@1: $options .= ''; webmaster@1: } webmaster@1: } webmaster@1: return $options; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Traverses a select element's #option array looking for any values webmaster@1: * that hold the given key. Returns an array of indexes that match. webmaster@1: * webmaster@1: * This function is useful if you need to modify the options that are webmaster@1: * already in a form element; for example, to remove choices which are webmaster@1: * not valid because of additional filters imposed by another module. webmaster@1: * One example might be altering the choices in a taxonomy selector. webmaster@1: * To correctly handle the case of a multiple hierarchy taxonomy, webmaster@1: * #options arrays can now hold an array of objects, instead of a webmaster@1: * direct mapping of keys to labels, so that multiple choices in the webmaster@1: * selector can have the same key (and label). This makes it difficult webmaster@1: * to manipulate directly, which is why this helper function exists. webmaster@1: * webmaster@1: * This function does not support optgroups (when the elements of the webmaster@1: * #options array are themselves arrays), and will return FALSE if webmaster@1: * arrays are found. The caller must either flatten/restore or webmaster@1: * manually do their manipulations in this case, since returning the webmaster@1: * index is not sufficient, and supporting this would make the webmaster@1: * "helper" too complicated and cumbersome to be of any help. webmaster@1: * webmaster@1: * As usual with functions that can return array() or FALSE, do not webmaster@1: * forget to use === and !== if needed. webmaster@1: * webmaster@1: * @param $element webmaster@1: * The select element to search. webmaster@1: * @param $key webmaster@1: * The key to look for. webmaster@1: * @return webmaster@1: * An array of indexes that match the given $key. Array will be webmaster@1: * empty if no elements were found. FALSE if optgroups were found. webmaster@1: */ webmaster@1: function form_get_options($element, $key) { webmaster@1: $keys = array(); webmaster@1: foreach ($element['#options'] as $index => $choice) { webmaster@1: if (is_array($choice)) { webmaster@1: return FALSE; webmaster@1: } webmaster@1: else if (is_object($choice)) { webmaster@1: if (isset($choice->option[$key])) { webmaster@1: $keys[] = $index; webmaster@1: } webmaster@1: } webmaster@1: else if ($index == $key) { webmaster@1: $keys[] = $index; webmaster@1: } webmaster@1: } webmaster@1: return $keys; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Format a group of form items. webmaster@1: * webmaster@1: * @param $element webmaster@1: * An associative array containing the properties of the element. webmaster@1: * Properties used: attributes, title, value, description, children, collapsible, collapsed webmaster@1: * @return webmaster@1: * A themed HTML string representing the form item group. webmaster@1: * webmaster@1: * @ingroup themeable webmaster@1: */ webmaster@1: function theme_fieldset($element) { webmaster@1: if ($element['#collapsible']) { webmaster@1: drupal_add_js('misc/collapse.js'); webmaster@1: webmaster@1: if (!isset($element['#attributes']['class'])) { webmaster@1: $element['#attributes']['class'] = ''; webmaster@1: } webmaster@1: webmaster@1: $element['#attributes']['class'] .= ' collapsible'; webmaster@1: if ($element['#collapsed']) { webmaster@1: $element['#attributes']['class'] .= ' collapsed'; webmaster@1: } webmaster@1: } webmaster@1: webmaster@7: return '
\n"; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Format a radio button. webmaster@1: * webmaster@1: * @param $element webmaster@1: * An associative array containing the properties of the element. webmaster@1: * Properties used: required, return_value, value, attributes, title, description webmaster@1: * @return webmaster@1: * A themed HTML string representing the form item group. webmaster@1: * webmaster@1: * @ingroup themeable webmaster@1: */ webmaster@1: function theme_radio($element) { webmaster@1: _form_set_class($element, array('form-radio')); webmaster@1: $output = ''; webmaster@1: if (!is_null($element['#title'])) { webmaster@1: $output = ''; webmaster@1: } webmaster@1: webmaster@1: unset($element['#title']); webmaster@1: return theme('form_element', $element, $output); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Format a set of radio buttons. webmaster@1: * webmaster@1: * @param $element webmaster@1: * An associative array containing the properties of the element. webmaster@1: * Properties used: title, value, options, description, required and attributes. webmaster@1: * @return webmaster@1: * A themed HTML string representing the radio button set. webmaster@1: * webmaster@1: * @ingroup themeable webmaster@1: */ webmaster@1: function theme_radios($element) { webmaster@1: $class = 'form-radios'; webmaster@1: if (isset($element['#attributes']['class'])) { webmaster@1: $class .= ' '. $element['#attributes']['class']; webmaster@1: } webmaster@1: $element['#children'] = '