annotate includes/form.inc @ 13:134d4b2e75f6

updated quicktabs and google analytics modules
author danieleb <danielebarchiesi@me.com>
date Tue, 29 Oct 2013 13:48:59 +0000
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2 /**
danielebarchiesi@0 3 * @file
danielebarchiesi@0 4 * Functions for form and batch generation and processing.
danielebarchiesi@0 5 */
danielebarchiesi@0 6
danielebarchiesi@0 7 /**
danielebarchiesi@0 8 * @defgroup forms Form builder functions
danielebarchiesi@0 9 * @{
danielebarchiesi@0 10 * Functions that build an abstract representation of a HTML form.
danielebarchiesi@0 11 *
danielebarchiesi@0 12 * All modules should declare their form builder functions to be in this
danielebarchiesi@0 13 * group and each builder function should reference its validate and submit
danielebarchiesi@0 14 * functions using \@see. Conversely, validate and submit functions should
danielebarchiesi@0 15 * reference the form builder function using \@see. For examples, of this see
danielebarchiesi@0 16 * system_modules_uninstall() or user_pass(), the latter of which has the
danielebarchiesi@0 17 * following in its doxygen documentation:
danielebarchiesi@0 18 *
danielebarchiesi@0 19 * \@ingroup forms
danielebarchiesi@0 20 * \@see user_pass_validate().
danielebarchiesi@0 21 * \@see user_pass_submit().
danielebarchiesi@0 22 *
danielebarchiesi@0 23 * @}
danielebarchiesi@0 24 */
danielebarchiesi@0 25
danielebarchiesi@0 26 /**
danielebarchiesi@0 27 * @defgroup form_api Form generation
danielebarchiesi@0 28 * @{
danielebarchiesi@0 29 * Functions to enable the processing and display of HTML forms.
danielebarchiesi@0 30 *
danielebarchiesi@0 31 * Drupal uses these functions to achieve consistency in its form processing and
danielebarchiesi@0 32 * presentation, while simplifying code and reducing the amount of HTML that
danielebarchiesi@0 33 * must be explicitly generated by modules.
danielebarchiesi@0 34 *
danielebarchiesi@0 35 * The primary function used with forms is drupal_get_form(), which is
danielebarchiesi@0 36 * used for forms presented interactively to a user. Forms can also be built and
danielebarchiesi@0 37 * submitted programmatically without any user input using the
danielebarchiesi@0 38 * drupal_form_submit() function.
danielebarchiesi@0 39 *
danielebarchiesi@0 40 * drupal_get_form() handles retrieving, processing, and displaying a rendered
danielebarchiesi@0 41 * HTML form for modules automatically.
danielebarchiesi@0 42 *
danielebarchiesi@0 43 * Here is an example of how to use drupal_get_form() and a form builder
danielebarchiesi@0 44 * function:
danielebarchiesi@0 45 * @code
danielebarchiesi@0 46 * $form = drupal_get_form('my_module_example_form');
danielebarchiesi@0 47 * ...
danielebarchiesi@0 48 * function my_module_example_form($form, &$form_state) {
danielebarchiesi@0 49 * $form['submit'] = array(
danielebarchiesi@0 50 * '#type' => 'submit',
danielebarchiesi@0 51 * '#value' => t('Submit'),
danielebarchiesi@0 52 * );
danielebarchiesi@0 53 * return $form;
danielebarchiesi@0 54 * }
danielebarchiesi@0 55 * function my_module_example_form_validate($form, &$form_state) {
danielebarchiesi@0 56 * // Validation logic.
danielebarchiesi@0 57 * }
danielebarchiesi@0 58 * function my_module_example_form_submit($form, &$form_state) {
danielebarchiesi@0 59 * // Submission logic.
danielebarchiesi@0 60 * }
danielebarchiesi@0 61 * @endcode
danielebarchiesi@0 62 *
danielebarchiesi@0 63 * Or with any number of additional arguments:
danielebarchiesi@0 64 * @code
danielebarchiesi@0 65 * $extra = "extra";
danielebarchiesi@0 66 * $form = drupal_get_form('my_module_example_form', $extra);
danielebarchiesi@0 67 * ...
danielebarchiesi@0 68 * function my_module_example_form($form, &$form_state, $extra) {
danielebarchiesi@0 69 * $form['submit'] = array(
danielebarchiesi@0 70 * '#type' => 'submit',
danielebarchiesi@0 71 * '#value' => $extra,
danielebarchiesi@0 72 * );
danielebarchiesi@0 73 * return $form;
danielebarchiesi@0 74 * }
danielebarchiesi@0 75 * @endcode
danielebarchiesi@0 76 *
danielebarchiesi@0 77 * The $form argument to form-related functions is a structured array containing
danielebarchiesi@0 78 * the elements and properties of the form. For information on the array
danielebarchiesi@0 79 * components and format, and more detailed explanations of the Form API
danielebarchiesi@0 80 * workflow, see the
danielebarchiesi@0 81 * @link forms_api_reference.html Form API reference @endlink
danielebarchiesi@0 82 * and the
danielebarchiesi@0 83 * @link http://drupal.org/node/37775 Form API documentation section. @endlink
danielebarchiesi@0 84 * In addition, there is a set of Form API tutorials in
danielebarchiesi@0 85 * @link form_example_tutorial.inc the Form Example Tutorial @endlink which
danielebarchiesi@0 86 * provide basics all the way up through multistep forms.
danielebarchiesi@0 87 *
danielebarchiesi@0 88 * In the form builder, validation, submission, and other form functions,
danielebarchiesi@0 89 * $form_state is the primary influence on the processing of the form and is
danielebarchiesi@0 90 * passed by reference to most functions, so they use it to communicate with
danielebarchiesi@0 91 * the form system and each other.
danielebarchiesi@0 92 *
danielebarchiesi@0 93 * See drupal_build_form() for documentation of $form_state keys.
danielebarchiesi@0 94 */
danielebarchiesi@0 95
danielebarchiesi@0 96 /**
danielebarchiesi@0 97 * Returns a renderable form array for a given form ID.
danielebarchiesi@0 98 *
danielebarchiesi@0 99 * This function should be used instead of drupal_build_form() when $form_state
danielebarchiesi@0 100 * is not needed (i.e., when initially rendering the form) and is often
danielebarchiesi@0 101 * used as a menu callback.
danielebarchiesi@0 102 *
danielebarchiesi@0 103 * @param $form_id
danielebarchiesi@0 104 * The unique string identifying the desired form. If a function with that
danielebarchiesi@0 105 * name exists, it is called to build the form array. Modules that need to
danielebarchiesi@0 106 * generate the same form (or very similar forms) using different $form_ids
danielebarchiesi@0 107 * can implement hook_forms(), which maps different $form_id values to the
danielebarchiesi@0 108 * proper form constructor function. Examples may be found in node_forms(),
danielebarchiesi@0 109 * and search_forms().
danielebarchiesi@0 110 * @param ...
danielebarchiesi@0 111 * Any additional arguments are passed on to the functions called by
danielebarchiesi@0 112 * drupal_get_form(), including the unique form constructor function. For
danielebarchiesi@0 113 * example, the node_edit form requires that a node object is passed in here
danielebarchiesi@0 114 * when it is called. These are available to implementations of
danielebarchiesi@0 115 * hook_form_alter() and hook_form_FORM_ID_alter() as the array
danielebarchiesi@0 116 * $form_state['build_info']['args'].
danielebarchiesi@0 117 *
danielebarchiesi@0 118 * @return
danielebarchiesi@0 119 * The form array.
danielebarchiesi@0 120 *
danielebarchiesi@0 121 * @see drupal_build_form()
danielebarchiesi@0 122 */
danielebarchiesi@0 123 function drupal_get_form($form_id) {
danielebarchiesi@0 124 $form_state = array();
danielebarchiesi@0 125
danielebarchiesi@0 126 $args = func_get_args();
danielebarchiesi@0 127 // Remove $form_id from the arguments.
danielebarchiesi@0 128 array_shift($args);
danielebarchiesi@0 129 $form_state['build_info']['args'] = $args;
danielebarchiesi@0 130
danielebarchiesi@0 131 return drupal_build_form($form_id, $form_state);
danielebarchiesi@0 132 }
danielebarchiesi@0 133
danielebarchiesi@0 134 /**
danielebarchiesi@0 135 * Builds and process a form based on a form id.
danielebarchiesi@0 136 *
danielebarchiesi@0 137 * The form may also be retrieved from the cache if the form was built in a
danielebarchiesi@0 138 * previous page-load. The form is then passed on for processing, validation
danielebarchiesi@0 139 * and submission if there is proper input.
danielebarchiesi@0 140 *
danielebarchiesi@0 141 * @param $form_id
danielebarchiesi@0 142 * The unique string identifying the desired form. If a function with that
danielebarchiesi@0 143 * name exists, it is called to build the form array. Modules that need to
danielebarchiesi@0 144 * generate the same form (or very similar forms) using different $form_ids
danielebarchiesi@0 145 * can implement hook_forms(), which maps different $form_id values to the
danielebarchiesi@0 146 * proper form constructor function. Examples may be found in node_forms(),
danielebarchiesi@0 147 * and search_forms().
danielebarchiesi@0 148 * @param $form_state
danielebarchiesi@0 149 * An array which stores information about the form. This is passed as a
danielebarchiesi@0 150 * reference so that the caller can use it to examine what in the form changed
danielebarchiesi@0 151 * when the form submission process is complete. Furthermore, it may be used
danielebarchiesi@0 152 * to store information related to the processed data in the form, which will
danielebarchiesi@0 153 * persist across page requests when the 'cache' or 'rebuild' flag is set.
danielebarchiesi@0 154 * The following parameters may be set in $form_state to affect how the form
danielebarchiesi@0 155 * is rendered:
danielebarchiesi@0 156 * - build_info: Internal. An associative array of information stored by Form
danielebarchiesi@0 157 * API that is necessary to build and rebuild the form from cache when the
danielebarchiesi@0 158 * original context may no longer be available:
danielebarchiesi@0 159 * - args: A list of arguments to pass to the form constructor.
danielebarchiesi@0 160 * - files: An optional array defining include files that need to be loaded
danielebarchiesi@0 161 * for building the form. Each array entry may be the path to a file or
danielebarchiesi@0 162 * another array containing values for the parameters 'type', 'module' and
danielebarchiesi@0 163 * 'name' as needed by module_load_include(). The files listed here are
danielebarchiesi@0 164 * automatically loaded by form_get_cache(). By default the current menu
danielebarchiesi@0 165 * router item's 'file' definition is added, if any. Use
danielebarchiesi@0 166 * form_load_include() to add include files from a form constructor.
danielebarchiesi@0 167 * - form_id: Identification of the primary form being constructed and
danielebarchiesi@0 168 * processed.
danielebarchiesi@0 169 * - base_form_id: Identification for a base form, as declared in a
danielebarchiesi@0 170 * hook_forms() implementation.
danielebarchiesi@0 171 * - rebuild_info: Internal. Similar to 'build_info', but pertaining to
danielebarchiesi@0 172 * drupal_rebuild_form().
danielebarchiesi@0 173 * - rebuild: Normally, after the entire form processing is completed and
danielebarchiesi@0 174 * submit handlers have run, a form is considered to be done and
danielebarchiesi@0 175 * drupal_redirect_form() will redirect the user to a new page using a GET
danielebarchiesi@0 176 * request (so a browser refresh does not re-submit the form). However, if
danielebarchiesi@0 177 * 'rebuild' has been set to TRUE, then a new copy of the form is
danielebarchiesi@0 178 * immediately built and sent to the browser, instead of a redirect. This is
danielebarchiesi@0 179 * used for multi-step forms, such as wizards and confirmation forms.
danielebarchiesi@0 180 * Normally, $form_state['rebuild'] is set by a submit handler, since it is
danielebarchiesi@0 181 * usually logic within a submit handler that determines whether a form is
danielebarchiesi@0 182 * done or requires another step. However, a validation handler may already
danielebarchiesi@0 183 * set $form_state['rebuild'] to cause the form processing to bypass submit
danielebarchiesi@0 184 * handlers and rebuild the form instead, even if there are no validation
danielebarchiesi@0 185 * errors.
danielebarchiesi@0 186 * - redirect: Used to redirect the form on submission. It may either be a
danielebarchiesi@0 187 * string containing the destination URL, or an array of arguments
danielebarchiesi@0 188 * compatible with drupal_goto(). See drupal_redirect_form() for complete
danielebarchiesi@0 189 * information.
danielebarchiesi@0 190 * - no_redirect: If set to TRUE the form will NOT perform a drupal_goto(),
danielebarchiesi@0 191 * even if 'redirect' is set.
danielebarchiesi@0 192 * - method: The HTTP form method to use for finding the input for this form.
danielebarchiesi@0 193 * May be 'post' or 'get'. Defaults to 'post'. Note that 'get' method
danielebarchiesi@0 194 * forms do not use form ids so are always considered to be submitted, which
danielebarchiesi@0 195 * can have unexpected effects. The 'get' method should only be used on
danielebarchiesi@0 196 * forms that do not change data, as that is exclusively the domain of
danielebarchiesi@0 197 * 'post.'
danielebarchiesi@0 198 * - cache: If set to TRUE the original, unprocessed form structure will be
danielebarchiesi@0 199 * cached, which allows the entire form to be rebuilt from cache. A typical
danielebarchiesi@0 200 * form workflow involves two page requests; first, a form is built and
danielebarchiesi@0 201 * rendered for the user to fill in. Then, the user fills the form in and
danielebarchiesi@0 202 * submits it, triggering a second page request in which the form must be
danielebarchiesi@0 203 * built and processed. By default, $form and $form_state are built from
danielebarchiesi@0 204 * scratch during each of these page requests. Often, it is necessary or
danielebarchiesi@0 205 * desired to persist the $form and $form_state variables from the initial
danielebarchiesi@0 206 * page request to the one that processes the submission. 'cache' can be set
danielebarchiesi@0 207 * to TRUE to do this. A prominent example is an Ajax-enabled form, in which
danielebarchiesi@0 208 * ajax_process_form() enables form caching for all forms that include an
danielebarchiesi@0 209 * element with the #ajax property. (The Ajax handler has no way to build
danielebarchiesi@0 210 * the form itself, so must rely on the cached version.) Note that the
danielebarchiesi@0 211 * persistence of $form and $form_state happens automatically for
danielebarchiesi@0 212 * (multi-step) forms having the 'rebuild' flag set, regardless of the value
danielebarchiesi@0 213 * for 'cache'.
danielebarchiesi@0 214 * - no_cache: If set to TRUE the form will NOT be cached, even if 'cache' is
danielebarchiesi@0 215 * set.
danielebarchiesi@0 216 * - values: An associative array of values submitted to the form. The
danielebarchiesi@0 217 * validation functions and submit functions use this array for nearly all
danielebarchiesi@0 218 * their decision making. (Note that #tree determines whether the values are
danielebarchiesi@0 219 * a flat array or an array whose structure parallels the $form array. See
danielebarchiesi@0 220 * @link forms_api_reference.html Form API reference @endlink for more
danielebarchiesi@0 221 * information.) These are raw and unvalidated, so should not be used
danielebarchiesi@0 222 * without a thorough understanding of security implications. In almost all
danielebarchiesi@0 223 * cases, code should use the data in the 'values' array exclusively. The
danielebarchiesi@0 224 * most common use of this key is for multi-step forms that need to clear
danielebarchiesi@0 225 * some of the user input when setting 'rebuild'. The values correspond to
danielebarchiesi@0 226 * $_POST or $_GET, depending on the 'method' chosen.
danielebarchiesi@0 227 * - always_process: If TRUE and the method is GET, a form_id is not
danielebarchiesi@0 228 * necessary. This should only be used on RESTful GET forms that do NOT
danielebarchiesi@0 229 * write data, as this could lead to security issues. It is useful so that
danielebarchiesi@0 230 * searches do not need to have a form_id in their query arguments to
danielebarchiesi@0 231 * trigger the search.
danielebarchiesi@0 232 * - must_validate: Ordinarily, a form is only validated once, but there are
danielebarchiesi@0 233 * times when a form is resubmitted internally and should be validated
danielebarchiesi@0 234 * again. Setting this to TRUE will force that to happen. This is most
danielebarchiesi@0 235 * likely to occur during Ajax operations.
danielebarchiesi@0 236 * - programmed: If TRUE, the form was submitted programmatically, usually
danielebarchiesi@0 237 * invoked via drupal_form_submit(). Defaults to FALSE.
danielebarchiesi@0 238 * - process_input: Boolean flag. TRUE signifies correct form submission.
danielebarchiesi@0 239 * This is always TRUE for programmed forms coming from drupal_form_submit()
danielebarchiesi@0 240 * (see 'programmed' key), or if the form_id coming from the $_POST data is
danielebarchiesi@0 241 * set and matches the current form_id.
danielebarchiesi@0 242 * - submitted: If TRUE, the form has been submitted. Defaults to FALSE.
danielebarchiesi@0 243 * - executed: If TRUE, the form was submitted and has been processed and
danielebarchiesi@0 244 * executed. Defaults to FALSE.
danielebarchiesi@0 245 * - triggering_element: (read-only) The form element that triggered
danielebarchiesi@0 246 * submission. This is the same as the deprecated
danielebarchiesi@0 247 * $form_state['clicked_button']. It is the element that caused submission,
danielebarchiesi@0 248 * which may or may not be a button (in the case of Ajax forms). This key is
danielebarchiesi@0 249 * often used to distinguish between various buttons in a submit handler,
danielebarchiesi@0 250 * and is also used in Ajax handlers.
danielebarchiesi@0 251 * - clicked_button: Deprecated. Use triggering_element instead.
danielebarchiesi@0 252 * - has_file_element: Internal. If TRUE, there is a file element and Form API
danielebarchiesi@0 253 * will set the appropriate 'enctype' HTML attribute on the form.
danielebarchiesi@0 254 * - groups: Internal. An array containing references to fieldsets to render
danielebarchiesi@0 255 * them within vertical tabs.
danielebarchiesi@0 256 * - storage: $form_state['storage'] is not a special key, and no specific
danielebarchiesi@0 257 * support is provided for it in the Form API. By tradition it was
danielebarchiesi@0 258 * the location where application-specific data was stored for communication
danielebarchiesi@0 259 * between the submit, validation, and form builder functions, especially
danielebarchiesi@0 260 * in a multi-step-style form. Form implementations may use any key(s)
danielebarchiesi@0 261 * within $form_state (other than the keys listed here and other reserved
danielebarchiesi@0 262 * ones used by Form API internals) for this kind of storage. The
danielebarchiesi@0 263 * recommended way to ensure that the chosen key doesn't conflict with ones
danielebarchiesi@0 264 * used by the Form API or other modules is to use the module name as the
danielebarchiesi@0 265 * key name or a prefix for the key name. For example, the Node module uses
danielebarchiesi@0 266 * $form_state['node'] in node editing forms to store information about the
danielebarchiesi@0 267 * node being edited, and this information stays available across successive
danielebarchiesi@0 268 * clicks of the "Preview" button as well as when the "Save" button is
danielebarchiesi@0 269 * finally clicked.
danielebarchiesi@0 270 * - buttons: A list containing copies of all submit and button elements in
danielebarchiesi@0 271 * the form.
danielebarchiesi@0 272 * - complete form: A reference to the $form variable containing the complete
danielebarchiesi@0 273 * form structure. #process, #after_build, #element_validate, and other
danielebarchiesi@0 274 * handlers being invoked on a form element may use this reference to access
danielebarchiesi@0 275 * other information in the form the element is contained in.
danielebarchiesi@0 276 * - temporary: An array holding temporary data accessible during the current
danielebarchiesi@0 277 * page request only. All $form_state properties that are not reserved keys
danielebarchiesi@0 278 * (see form_state_keys_no_cache()) persist throughout a multistep form
danielebarchiesi@0 279 * sequence. Form API provides this key for modules to communicate
danielebarchiesi@0 280 * information across form-related functions during a single page request.
danielebarchiesi@0 281 * It may be used to temporarily save data that does not need to or should
danielebarchiesi@0 282 * not be cached during the whole form workflow; e.g., data that needs to be
danielebarchiesi@0 283 * accessed during the current form build process only. There is no use-case
danielebarchiesi@0 284 * for this functionality in Drupal core.
danielebarchiesi@0 285 * - wrapper_callback: Modules that wish to pre-populate certain forms with
danielebarchiesi@0 286 * common elements, such as back/next/save buttons in multi-step form
danielebarchiesi@0 287 * wizards, may define a form builder function name that returns a form
danielebarchiesi@0 288 * structure, which is passed on to the actual form builder function.
danielebarchiesi@0 289 * Such implementations may either define the 'wrapper_callback' via
danielebarchiesi@0 290 * hook_forms() or have to invoke drupal_build_form() (instead of
danielebarchiesi@0 291 * drupal_get_form()) on their own in a custom menu callback to prepare
danielebarchiesi@0 292 * $form_state accordingly.
danielebarchiesi@0 293 * Information on how certain $form_state properties control redirection
danielebarchiesi@0 294 * behavior after form submission may be found in drupal_redirect_form().
danielebarchiesi@0 295 *
danielebarchiesi@0 296 * @return
danielebarchiesi@0 297 * The rendered form. This function may also perform a redirect and hence may
danielebarchiesi@0 298 * not return at all, depending upon the $form_state flags that were set.
danielebarchiesi@0 299 *
danielebarchiesi@0 300 * @see drupal_redirect_form()
danielebarchiesi@0 301 */
danielebarchiesi@0 302 function drupal_build_form($form_id, &$form_state) {
danielebarchiesi@0 303 // Ensure some defaults; if already set they will not be overridden.
danielebarchiesi@0 304 $form_state += form_state_defaults();
danielebarchiesi@0 305
danielebarchiesi@0 306 if (!isset($form_state['input'])) {
danielebarchiesi@0 307 $form_state['input'] = $form_state['method'] == 'get' ? $_GET : $_POST;
danielebarchiesi@0 308 }
danielebarchiesi@0 309
danielebarchiesi@0 310 if (isset($_SESSION['batch_form_state'])) {
danielebarchiesi@0 311 // We've been redirected here after a batch processing. The form has
danielebarchiesi@0 312 // already been processed, but needs to be rebuilt. See _batch_finished().
danielebarchiesi@0 313 $form_state = $_SESSION['batch_form_state'];
danielebarchiesi@0 314 unset($_SESSION['batch_form_state']);
danielebarchiesi@0 315 return drupal_rebuild_form($form_id, $form_state);
danielebarchiesi@0 316 }
danielebarchiesi@0 317
danielebarchiesi@0 318 // If the incoming input contains a form_build_id, we'll check the cache for a
danielebarchiesi@0 319 // copy of the form in question. If it's there, we don't have to rebuild the
danielebarchiesi@0 320 // form to proceed. In addition, if there is stored form_state data from a
danielebarchiesi@0 321 // previous step, we'll retrieve it so it can be passed on to the form
danielebarchiesi@0 322 // processing code.
danielebarchiesi@0 323 $check_cache = isset($form_state['input']['form_id']) && $form_state['input']['form_id'] == $form_id && !empty($form_state['input']['form_build_id']);
danielebarchiesi@0 324 if ($check_cache) {
danielebarchiesi@0 325 $form = form_get_cache($form_state['input']['form_build_id'], $form_state);
danielebarchiesi@0 326 }
danielebarchiesi@0 327
danielebarchiesi@0 328 // If the previous bit of code didn't result in a populated $form object, we
danielebarchiesi@0 329 // are hitting the form for the first time and we need to build it from
danielebarchiesi@0 330 // scratch.
danielebarchiesi@0 331 if (!isset($form)) {
danielebarchiesi@0 332 // If we attempted to serve the form from cache, uncacheable $form_state
danielebarchiesi@0 333 // keys need to be removed after retrieving and preparing the form, except
danielebarchiesi@0 334 // any that were already set prior to retrieving the form.
danielebarchiesi@0 335 if ($check_cache) {
danielebarchiesi@0 336 $form_state_before_retrieval = $form_state;
danielebarchiesi@0 337 }
danielebarchiesi@0 338
danielebarchiesi@0 339 $form = drupal_retrieve_form($form_id, $form_state);
danielebarchiesi@0 340 drupal_prepare_form($form_id, $form, $form_state);
danielebarchiesi@0 341
danielebarchiesi@0 342 // form_set_cache() removes uncacheable $form_state keys defined in
danielebarchiesi@0 343 // form_state_keys_no_cache() in order for multi-step forms to work
danielebarchiesi@0 344 // properly. This means that form processing logic for single-step forms
danielebarchiesi@0 345 // using $form_state['cache'] may depend on data stored in those keys
danielebarchiesi@0 346 // during drupal_retrieve_form()/drupal_prepare_form(), but form
danielebarchiesi@0 347 // processing should not depend on whether the form is cached or not, so
danielebarchiesi@0 348 // $form_state is adjusted to match what it would be after a
danielebarchiesi@0 349 // form_set_cache()/form_get_cache() sequence. These exceptions are
danielebarchiesi@0 350 // allowed to survive here:
danielebarchiesi@0 351 // - always_process: Does not make sense in conjunction with form caching
danielebarchiesi@0 352 // in the first place, since passing form_build_id as a GET parameter is
danielebarchiesi@0 353 // not desired.
danielebarchiesi@0 354 // - temporary: Any assigned data is expected to survives within the same
danielebarchiesi@0 355 // page request.
danielebarchiesi@0 356 if ($check_cache) {
danielebarchiesi@0 357 $uncacheable_keys = array_flip(array_diff(form_state_keys_no_cache(), array('always_process', 'temporary')));
danielebarchiesi@0 358 $form_state = array_diff_key($form_state, $uncacheable_keys);
danielebarchiesi@0 359 $form_state += $form_state_before_retrieval;
danielebarchiesi@0 360 }
danielebarchiesi@0 361 }
danielebarchiesi@0 362
danielebarchiesi@0 363 // Now that we have a constructed form, process it. This is where:
danielebarchiesi@0 364 // - Element #process functions get called to further refine $form.
danielebarchiesi@0 365 // - User input, if any, gets incorporated in the #value property of the
danielebarchiesi@0 366 // corresponding elements and into $form_state['values'].
danielebarchiesi@0 367 // - Validation and submission handlers are called.
danielebarchiesi@0 368 // - If this submission is part of a multistep workflow, the form is rebuilt
danielebarchiesi@0 369 // to contain the information of the next step.
danielebarchiesi@0 370 // - If necessary, the form and form state are cached or re-cached, so that
danielebarchiesi@0 371 // appropriate information persists to the next page request.
danielebarchiesi@0 372 // All of the handlers in the pipeline receive $form_state by reference and
danielebarchiesi@0 373 // can use it to know or update information about the state of the form.
danielebarchiesi@0 374 drupal_process_form($form_id, $form, $form_state);
danielebarchiesi@0 375
danielebarchiesi@0 376 // If this was a successful submission of a single-step form or the last step
danielebarchiesi@0 377 // of a multi-step form, then drupal_process_form() issued a redirect to
danielebarchiesi@0 378 // another page, or back to this page, but as a new request. Therefore, if
danielebarchiesi@0 379 // we're here, it means that this is either a form being viewed initially
danielebarchiesi@0 380 // before any user input, or there was a validation error requiring the form
danielebarchiesi@0 381 // to be re-displayed, or we're in a multi-step workflow and need to display
danielebarchiesi@0 382 // the form's next step. In any case, we have what we need in $form, and can
danielebarchiesi@0 383 // return it for rendering.
danielebarchiesi@0 384 return $form;
danielebarchiesi@0 385 }
danielebarchiesi@0 386
danielebarchiesi@0 387 /**
danielebarchiesi@0 388 * Retrieves default values for the $form_state array.
danielebarchiesi@0 389 */
danielebarchiesi@0 390 function form_state_defaults() {
danielebarchiesi@0 391 return array(
danielebarchiesi@0 392 'rebuild' => FALSE,
danielebarchiesi@0 393 'rebuild_info' => array(),
danielebarchiesi@0 394 'redirect' => NULL,
danielebarchiesi@0 395 // @todo 'args' is usually set, so no other default 'build_info' keys are
danielebarchiesi@0 396 // appended via += form_state_defaults().
danielebarchiesi@0 397 'build_info' => array(
danielebarchiesi@0 398 'args' => array(),
danielebarchiesi@0 399 'files' => array(),
danielebarchiesi@0 400 ),
danielebarchiesi@0 401 'temporary' => array(),
danielebarchiesi@0 402 'submitted' => FALSE,
danielebarchiesi@0 403 'executed' => FALSE,
danielebarchiesi@0 404 'programmed' => FALSE,
danielebarchiesi@0 405 'cache'=> FALSE,
danielebarchiesi@0 406 'method' => 'post',
danielebarchiesi@0 407 'groups' => array(),
danielebarchiesi@0 408 'buttons' => array(),
danielebarchiesi@0 409 );
danielebarchiesi@0 410 }
danielebarchiesi@0 411
danielebarchiesi@0 412 /**
danielebarchiesi@0 413 * Constructs a new $form from the information in $form_state.
danielebarchiesi@0 414 *
danielebarchiesi@0 415 * This is the key function for making multi-step forms advance from step to
danielebarchiesi@0 416 * step. It is called by drupal_process_form() when all user input processing,
danielebarchiesi@0 417 * including calling validation and submission handlers, for the request is
danielebarchiesi@0 418 * finished. If a validate or submit handler set $form_state['rebuild'] to TRUE,
danielebarchiesi@0 419 * and if other conditions don't preempt a rebuild from happening, then this
danielebarchiesi@0 420 * function is called to generate a new $form, the next step in the form
danielebarchiesi@0 421 * workflow, to be returned for rendering.
danielebarchiesi@0 422 *
danielebarchiesi@0 423 * Ajax form submissions are almost always multi-step workflows, so that is one
danielebarchiesi@0 424 * common use-case during which form rebuilding occurs. See ajax_form_callback()
danielebarchiesi@0 425 * for more information about creating Ajax-enabled forms.
danielebarchiesi@0 426 *
danielebarchiesi@0 427 * @param $form_id
danielebarchiesi@0 428 * The unique string identifying the desired form. If a function
danielebarchiesi@0 429 * with that name exists, it is called to build the form array.
danielebarchiesi@0 430 * Modules that need to generate the same form (or very similar forms)
danielebarchiesi@0 431 * using different $form_ids can implement hook_forms(), which maps
danielebarchiesi@0 432 * different $form_id values to the proper form constructor function. Examples
danielebarchiesi@0 433 * may be found in node_forms() and search_forms().
danielebarchiesi@0 434 * @param $form_state
danielebarchiesi@0 435 * A keyed array containing the current state of the form.
danielebarchiesi@0 436 * @param $old_form
danielebarchiesi@0 437 * (optional) A previously built $form. Used to retain the #build_id and
danielebarchiesi@0 438 * #action properties in Ajax callbacks and similar partial form rebuilds. The
danielebarchiesi@0 439 * only properties copied from $old_form are the ones which both exist in
danielebarchiesi@0 440 * $old_form and for which $form_state['rebuild_info']['copy'][PROPERTY] is
danielebarchiesi@0 441 * TRUE. If $old_form is not passed, the entire $form is rebuilt freshly.
danielebarchiesi@0 442 * 'rebuild_info' needs to be a separate top-level property next to
danielebarchiesi@0 443 * 'build_info', since the contained data must not be cached.
danielebarchiesi@0 444 *
danielebarchiesi@0 445 * @return
danielebarchiesi@0 446 * The newly built form.
danielebarchiesi@0 447 *
danielebarchiesi@0 448 * @see drupal_process_form()
danielebarchiesi@0 449 * @see ajax_form_callback()
danielebarchiesi@0 450 */
danielebarchiesi@0 451 function drupal_rebuild_form($form_id, &$form_state, $old_form = NULL) {
danielebarchiesi@0 452 $form = drupal_retrieve_form($form_id, $form_state);
danielebarchiesi@0 453
danielebarchiesi@0 454 // If only parts of the form will be returned to the browser (e.g., Ajax or
danielebarchiesi@0 455 // RIA clients), re-use the old #build_id to not require client-side code to
danielebarchiesi@0 456 // manually update the hidden 'build_id' input element.
danielebarchiesi@0 457 // Otherwise, a new #build_id is generated, to not clobber the previous
danielebarchiesi@0 458 // build's data in the form cache; also allowing the user to go back to an
danielebarchiesi@0 459 // earlier build, make changes, and re-submit.
danielebarchiesi@0 460 // @see drupal_prepare_form()
danielebarchiesi@0 461 if (isset($old_form['#build_id']) && !empty($form_state['rebuild_info']['copy']['#build_id'])) {
danielebarchiesi@0 462 $form['#build_id'] = $old_form['#build_id'];
danielebarchiesi@0 463 }
danielebarchiesi@0 464 else {
danielebarchiesi@0 465 $form['#build_id'] = 'form-' . drupal_hash_base64(uniqid(mt_rand(), TRUE) . mt_rand());
danielebarchiesi@0 466 }
danielebarchiesi@0 467
danielebarchiesi@0 468 // #action defaults to request_uri(), but in case of Ajax and other partial
danielebarchiesi@0 469 // rebuilds, the form is submitted to an alternate URL, and the original
danielebarchiesi@0 470 // #action needs to be retained.
danielebarchiesi@0 471 if (isset($old_form['#action']) && !empty($form_state['rebuild_info']['copy']['#action'])) {
danielebarchiesi@0 472 $form['#action'] = $old_form['#action'];
danielebarchiesi@0 473 }
danielebarchiesi@0 474
danielebarchiesi@0 475 drupal_prepare_form($form_id, $form, $form_state);
danielebarchiesi@0 476
danielebarchiesi@0 477 // Caching is normally done in drupal_process_form(), but what needs to be
danielebarchiesi@0 478 // cached is the $form structure before it passes through form_builder(),
danielebarchiesi@0 479 // so we need to do it here.
danielebarchiesi@0 480 // @todo For Drupal 8, find a way to avoid this code duplication.
danielebarchiesi@0 481 if (empty($form_state['no_cache'])) {
danielebarchiesi@0 482 form_set_cache($form['#build_id'], $form, $form_state);
danielebarchiesi@0 483 }
danielebarchiesi@0 484
danielebarchiesi@0 485 // Clear out all group associations as these might be different when
danielebarchiesi@0 486 // re-rendering the form.
danielebarchiesi@0 487 $form_state['groups'] = array();
danielebarchiesi@0 488
danielebarchiesi@0 489 // Return a fully built form that is ready for rendering.
danielebarchiesi@0 490 return form_builder($form_id, $form, $form_state);
danielebarchiesi@0 491 }
danielebarchiesi@0 492
danielebarchiesi@0 493 /**
danielebarchiesi@0 494 * Fetches a form from cache.
danielebarchiesi@0 495 */
danielebarchiesi@0 496 function form_get_cache($form_build_id, &$form_state) {
danielebarchiesi@0 497 if ($cached = cache_get('form_' . $form_build_id, 'cache_form')) {
danielebarchiesi@0 498 $form = $cached->data;
danielebarchiesi@0 499
danielebarchiesi@0 500 global $user;
danielebarchiesi@0 501 if ((isset($form['#cache_token']) && drupal_valid_token($form['#cache_token'])) || (!isset($form['#cache_token']) && !$user->uid)) {
danielebarchiesi@0 502 if ($cached = cache_get('form_state_' . $form_build_id, 'cache_form')) {
danielebarchiesi@0 503 // Re-populate $form_state for subsequent rebuilds.
danielebarchiesi@0 504 $form_state = $cached->data + $form_state;
danielebarchiesi@0 505
danielebarchiesi@0 506 // If the original form is contained in include files, load the files.
danielebarchiesi@0 507 // @see form_load_include()
danielebarchiesi@0 508 $form_state['build_info'] += array('files' => array());
danielebarchiesi@0 509 foreach ($form_state['build_info']['files'] as $file) {
danielebarchiesi@0 510 if (is_array($file)) {
danielebarchiesi@0 511 $file += array('type' => 'inc', 'name' => $file['module']);
danielebarchiesi@0 512 module_load_include($file['type'], $file['module'], $file['name']);
danielebarchiesi@0 513 }
danielebarchiesi@0 514 elseif (file_exists($file)) {
danielebarchiesi@0 515 require_once DRUPAL_ROOT . '/' . $file;
danielebarchiesi@0 516 }
danielebarchiesi@0 517 }
danielebarchiesi@0 518 }
danielebarchiesi@0 519 return $form;
danielebarchiesi@0 520 }
danielebarchiesi@0 521 }
danielebarchiesi@0 522 }
danielebarchiesi@0 523
danielebarchiesi@0 524 /**
danielebarchiesi@0 525 * Stores a form in the cache.
danielebarchiesi@0 526 */
danielebarchiesi@0 527 function form_set_cache($form_build_id, $form, $form_state) {
danielebarchiesi@0 528 // 6 hours cache life time for forms should be plenty.
danielebarchiesi@0 529 $expire = 21600;
danielebarchiesi@0 530
danielebarchiesi@0 531 // Cache form structure.
danielebarchiesi@0 532 if (isset($form)) {
danielebarchiesi@0 533 if ($GLOBALS['user']->uid) {
danielebarchiesi@0 534 $form['#cache_token'] = drupal_get_token();
danielebarchiesi@0 535 }
danielebarchiesi@0 536 cache_set('form_' . $form_build_id, $form, 'cache_form', REQUEST_TIME + $expire);
danielebarchiesi@0 537 }
danielebarchiesi@0 538
danielebarchiesi@0 539 // Cache form state.
danielebarchiesi@0 540 if ($data = array_diff_key($form_state, array_flip(form_state_keys_no_cache()))) {
danielebarchiesi@0 541 cache_set('form_state_' . $form_build_id, $data, 'cache_form', REQUEST_TIME + $expire);
danielebarchiesi@0 542 }
danielebarchiesi@0 543 }
danielebarchiesi@0 544
danielebarchiesi@0 545 /**
danielebarchiesi@0 546 * Returns an array of $form_state keys that shouldn't be cached.
danielebarchiesi@0 547 */
danielebarchiesi@0 548 function form_state_keys_no_cache() {
danielebarchiesi@0 549 return array(
danielebarchiesi@0 550 // Public properties defined by form constructors and form handlers.
danielebarchiesi@0 551 'always_process',
danielebarchiesi@0 552 'must_validate',
danielebarchiesi@0 553 'rebuild',
danielebarchiesi@0 554 'rebuild_info',
danielebarchiesi@0 555 'redirect',
danielebarchiesi@0 556 'no_redirect',
danielebarchiesi@0 557 'temporary',
danielebarchiesi@0 558 // Internal properties defined by form processing.
danielebarchiesi@0 559 'buttons',
danielebarchiesi@0 560 'triggering_element',
danielebarchiesi@0 561 'clicked_button',
danielebarchiesi@0 562 'complete form',
danielebarchiesi@0 563 'groups',
danielebarchiesi@0 564 'input',
danielebarchiesi@0 565 'method',
danielebarchiesi@0 566 'submit_handlers',
danielebarchiesi@0 567 'submitted',
danielebarchiesi@0 568 'executed',
danielebarchiesi@0 569 'validate_handlers',
danielebarchiesi@0 570 'values',
danielebarchiesi@0 571 );
danielebarchiesi@0 572 }
danielebarchiesi@0 573
danielebarchiesi@0 574 /**
danielebarchiesi@0 575 * Ensures an include file is loaded whenever the form is processed.
danielebarchiesi@0 576 *
danielebarchiesi@0 577 * Example:
danielebarchiesi@0 578 * @code
danielebarchiesi@0 579 * // Load node.admin.inc from Node module.
danielebarchiesi@0 580 * form_load_include($form_state, 'inc', 'node', 'node.admin');
danielebarchiesi@0 581 * @endcode
danielebarchiesi@0 582 *
danielebarchiesi@0 583 * Use this function instead of module_load_include() from inside a form
danielebarchiesi@0 584 * constructor or any form processing logic as it ensures that the include file
danielebarchiesi@0 585 * is loaded whenever the form is processed. In contrast to using
danielebarchiesi@0 586 * module_load_include() directly, form_load_include() makes sure the include
danielebarchiesi@0 587 * file is correctly loaded also if the form is cached.
danielebarchiesi@0 588 *
danielebarchiesi@0 589 * @param $form_state
danielebarchiesi@0 590 * The current state of the form.
danielebarchiesi@0 591 * @param $type
danielebarchiesi@0 592 * The include file's type (file extension).
danielebarchiesi@0 593 * @param $module
danielebarchiesi@0 594 * The module to which the include file belongs.
danielebarchiesi@0 595 * @param $name
danielebarchiesi@0 596 * (optional) The base file name (without the $type extension). If omitted,
danielebarchiesi@0 597 * $module is used; i.e., resulting in "$module.$type" by default.
danielebarchiesi@0 598 *
danielebarchiesi@0 599 * @return
danielebarchiesi@0 600 * The filepath of the loaded include file, or FALSE if the include file was
danielebarchiesi@0 601 * not found or has been loaded already.
danielebarchiesi@0 602 *
danielebarchiesi@0 603 * @see module_load_include()
danielebarchiesi@0 604 */
danielebarchiesi@0 605 function form_load_include(&$form_state, $type, $module, $name = NULL) {
danielebarchiesi@0 606 if (!isset($name)) {
danielebarchiesi@0 607 $name = $module;
danielebarchiesi@0 608 }
danielebarchiesi@0 609 if (!isset($form_state['build_info']['files']["$module:$name.$type"])) {
danielebarchiesi@0 610 // Only add successfully included files to the form state.
danielebarchiesi@0 611 if ($result = module_load_include($type, $module, $name)) {
danielebarchiesi@0 612 $form_state['build_info']['files']["$module:$name.$type"] = array(
danielebarchiesi@0 613 'type' => $type,
danielebarchiesi@0 614 'module' => $module,
danielebarchiesi@0 615 'name' => $name,
danielebarchiesi@0 616 );
danielebarchiesi@0 617 return $result;
danielebarchiesi@0 618 }
danielebarchiesi@0 619 }
danielebarchiesi@0 620 return FALSE;
danielebarchiesi@0 621 }
danielebarchiesi@0 622
danielebarchiesi@0 623 /**
danielebarchiesi@0 624 * Retrieves, populates, and processes a form.
danielebarchiesi@0 625 *
danielebarchiesi@0 626 * This function allows you to supply values for form elements and submit a
danielebarchiesi@0 627 * form for processing. Compare to drupal_get_form(), which also builds and
danielebarchiesi@0 628 * processes a form, but does not allow you to supply values.
danielebarchiesi@0 629 *
danielebarchiesi@0 630 * There is no return value, but you can check to see if there are errors
danielebarchiesi@0 631 * by calling form_get_errors().
danielebarchiesi@0 632 *
danielebarchiesi@0 633 * @param $form_id
danielebarchiesi@0 634 * The unique string identifying the desired form. If a function
danielebarchiesi@0 635 * with that name exists, it is called to build the form array.
danielebarchiesi@0 636 * Modules that need to generate the same form (or very similar forms)
danielebarchiesi@0 637 * using different $form_ids can implement hook_forms(), which maps
danielebarchiesi@0 638 * different $form_id values to the proper form constructor function. Examples
danielebarchiesi@0 639 * may be found in node_forms() and search_forms().
danielebarchiesi@0 640 * @param $form_state
danielebarchiesi@0 641 * A keyed array containing the current state of the form. Most important is
danielebarchiesi@0 642 * the $form_state['values'] collection, a tree of data used to simulate the
danielebarchiesi@0 643 * incoming $_POST information from a user's form submission. If a key is not
danielebarchiesi@0 644 * filled in $form_state['values'], then the default value of the respective
danielebarchiesi@0 645 * element is used. To submit an unchecked checkbox or other control that
danielebarchiesi@0 646 * browsers submit by not having a $_POST entry, include the key, but set the
danielebarchiesi@0 647 * value to NULL.
danielebarchiesi@0 648 * @param ...
danielebarchiesi@0 649 * Any additional arguments are passed on to the functions called by
danielebarchiesi@0 650 * drupal_form_submit(), including the unique form constructor function.
danielebarchiesi@0 651 * For example, the node_edit form requires that a node object be passed
danielebarchiesi@0 652 * in here when it is called. Arguments that need to be passed by reference
danielebarchiesi@0 653 * should not be included here, but rather placed directly in the $form_state
danielebarchiesi@0 654 * build info array so that the reference can be preserved. For example, a
danielebarchiesi@0 655 * form builder function with the following signature:
danielebarchiesi@0 656 * @code
danielebarchiesi@0 657 * function mymodule_form($form, &$form_state, &$object) {
danielebarchiesi@0 658 * }
danielebarchiesi@0 659 * @endcode
danielebarchiesi@0 660 * would be called via drupal_form_submit() as follows:
danielebarchiesi@0 661 * @code
danielebarchiesi@0 662 * $form_state['values'] = $my_form_values;
danielebarchiesi@0 663 * $form_state['build_info']['args'] = array(&$object);
danielebarchiesi@0 664 * drupal_form_submit('mymodule_form', $form_state);
danielebarchiesi@0 665 * @endcode
danielebarchiesi@0 666 * For example:
danielebarchiesi@0 667 * @code
danielebarchiesi@0 668 * // register a new user
danielebarchiesi@0 669 * $form_state = array();
danielebarchiesi@0 670 * $form_state['values']['name'] = 'robo-user';
danielebarchiesi@0 671 * $form_state['values']['mail'] = 'robouser@example.com';
danielebarchiesi@0 672 * $form_state['values']['pass']['pass1'] = 'password';
danielebarchiesi@0 673 * $form_state['values']['pass']['pass2'] = 'password';
danielebarchiesi@0 674 * $form_state['values']['op'] = t('Create new account');
danielebarchiesi@0 675 * drupal_form_submit('user_register_form', $form_state);
danielebarchiesi@0 676 * @endcode
danielebarchiesi@0 677 */
danielebarchiesi@0 678 function drupal_form_submit($form_id, &$form_state) {
danielebarchiesi@0 679 if (!isset($form_state['build_info']['args'])) {
danielebarchiesi@0 680 $args = func_get_args();
danielebarchiesi@0 681 array_shift($args);
danielebarchiesi@0 682 array_shift($args);
danielebarchiesi@0 683 $form_state['build_info']['args'] = $args;
danielebarchiesi@0 684 }
danielebarchiesi@0 685 // Merge in default values.
danielebarchiesi@0 686 $form_state += form_state_defaults();
danielebarchiesi@0 687
danielebarchiesi@0 688 // Populate $form_state['input'] with the submitted values before retrieving
danielebarchiesi@0 689 // the form, to be consistent with what drupal_build_form() does for
danielebarchiesi@0 690 // non-programmatic submissions (form builder functions may expect it to be
danielebarchiesi@0 691 // there).
danielebarchiesi@0 692 $form_state['input'] = $form_state['values'];
danielebarchiesi@0 693
danielebarchiesi@0 694 $form_state['programmed'] = TRUE;
danielebarchiesi@0 695 $form = drupal_retrieve_form($form_id, $form_state);
danielebarchiesi@0 696 // Programmed forms are always submitted.
danielebarchiesi@0 697 $form_state['submitted'] = TRUE;
danielebarchiesi@0 698
danielebarchiesi@0 699 // Reset form validation.
danielebarchiesi@0 700 $form_state['must_validate'] = TRUE;
danielebarchiesi@0 701 form_clear_error();
danielebarchiesi@0 702
danielebarchiesi@0 703 drupal_prepare_form($form_id, $form, $form_state);
danielebarchiesi@0 704 drupal_process_form($form_id, $form, $form_state);
danielebarchiesi@0 705 }
danielebarchiesi@0 706
danielebarchiesi@0 707 /**
danielebarchiesi@0 708 * Retrieves the structured array that defines a given form.
danielebarchiesi@0 709 *
danielebarchiesi@0 710 * @param $form_id
danielebarchiesi@0 711 * The unique string identifying the desired form. If a function
danielebarchiesi@0 712 * with that name exists, it is called to build the form array.
danielebarchiesi@0 713 * Modules that need to generate the same form (or very similar forms)
danielebarchiesi@0 714 * using different $form_ids can implement hook_forms(), which maps
danielebarchiesi@0 715 * different $form_id values to the proper form constructor function.
danielebarchiesi@0 716 * @param $form_state
danielebarchiesi@0 717 * A keyed array containing the current state of the form, including the
danielebarchiesi@0 718 * additional arguments to drupal_get_form() or drupal_form_submit() in the
danielebarchiesi@0 719 * 'args' component of the array.
danielebarchiesi@0 720 */
danielebarchiesi@0 721 function drupal_retrieve_form($form_id, &$form_state) {
danielebarchiesi@0 722 $forms = &drupal_static(__FUNCTION__);
danielebarchiesi@0 723
danielebarchiesi@0 724 // Record the $form_id.
danielebarchiesi@0 725 $form_state['build_info']['form_id'] = $form_id;
danielebarchiesi@0 726
danielebarchiesi@0 727 // Record the filepath of the include file containing the original form, so
danielebarchiesi@0 728 // the form builder callbacks can be loaded when the form is being rebuilt
danielebarchiesi@0 729 // from cache on a different path (such as 'system/ajax'). See
danielebarchiesi@0 730 // form_get_cache(). Don't do this in maintenance mode as Drupal may not be
danielebarchiesi@0 731 // fully bootstrapped (i.e. during installation) in which case
danielebarchiesi@0 732 // menu_get_item() is not available.
danielebarchiesi@0 733 if (!isset($form_state['build_info']['files']['menu']) && !defined('MAINTENANCE_MODE')) {
danielebarchiesi@0 734 $item = menu_get_item();
danielebarchiesi@0 735 if (!empty($item['include_file'])) {
danielebarchiesi@0 736 // Do not use form_load_include() here, as the file is already loaded.
danielebarchiesi@0 737 // Anyway, form_get_cache() is able to handle filepaths too.
danielebarchiesi@0 738 $form_state['build_info']['files']['menu'] = $item['include_file'];
danielebarchiesi@0 739 }
danielebarchiesi@0 740 }
danielebarchiesi@0 741
danielebarchiesi@0 742 // We save two copies of the incoming arguments: one for modules to use
danielebarchiesi@0 743 // when mapping form ids to constructor functions, and another to pass to
danielebarchiesi@0 744 // the constructor function itself.
danielebarchiesi@0 745 $args = $form_state['build_info']['args'];
danielebarchiesi@0 746
danielebarchiesi@0 747 // We first check to see if there's a function named after the $form_id.
danielebarchiesi@0 748 // If there is, we simply pass the arguments on to it to get the form.
danielebarchiesi@0 749 if (!function_exists($form_id)) {
danielebarchiesi@0 750 // In cases where many form_ids need to share a central constructor function,
danielebarchiesi@0 751 // such as the node editing form, modules can implement hook_forms(). It
danielebarchiesi@0 752 // maps one or more form_ids to the correct constructor functions.
danielebarchiesi@0 753 //
danielebarchiesi@0 754 // We cache the results of that hook to save time, but that only works
danielebarchiesi@0 755 // for modules that know all their form_ids in advance. (A module that
danielebarchiesi@0 756 // adds a small 'rate this comment' form to each comment in a list
danielebarchiesi@0 757 // would need a unique form_id for each one, for example.)
danielebarchiesi@0 758 //
danielebarchiesi@0 759 // So, we call the hook if $forms isn't yet populated, OR if it doesn't
danielebarchiesi@0 760 // yet have an entry for the requested form_id.
danielebarchiesi@0 761 if (!isset($forms) || !isset($forms[$form_id])) {
danielebarchiesi@0 762 $forms = module_invoke_all('forms', $form_id, $args);
danielebarchiesi@0 763 }
danielebarchiesi@0 764 $form_definition = $forms[$form_id];
danielebarchiesi@0 765 if (isset($form_definition['callback arguments'])) {
danielebarchiesi@0 766 $args = array_merge($form_definition['callback arguments'], $args);
danielebarchiesi@0 767 }
danielebarchiesi@0 768 if (isset($form_definition['callback'])) {
danielebarchiesi@0 769 $callback = $form_definition['callback'];
danielebarchiesi@0 770 $form_state['build_info']['base_form_id'] = $callback;
danielebarchiesi@0 771 }
danielebarchiesi@0 772 // In case $form_state['wrapper_callback'] is not defined already, we also
danielebarchiesi@0 773 // allow hook_forms() to define one.
danielebarchiesi@0 774 if (!isset($form_state['wrapper_callback']) && isset($form_definition['wrapper_callback'])) {
danielebarchiesi@0 775 $form_state['wrapper_callback'] = $form_definition['wrapper_callback'];
danielebarchiesi@0 776 }
danielebarchiesi@0 777 }
danielebarchiesi@0 778
danielebarchiesi@0 779 $form = array();
danielebarchiesi@0 780 // We need to pass $form_state by reference in order for forms to modify it,
danielebarchiesi@0 781 // since call_user_func_array() requires that referenced variables are passed
danielebarchiesi@0 782 // explicitly.
danielebarchiesi@0 783 $args = array_merge(array($form, &$form_state), $args);
danielebarchiesi@0 784
danielebarchiesi@0 785 // When the passed $form_state (not using drupal_get_form()) defines a
danielebarchiesi@0 786 // 'wrapper_callback', then it requests to invoke a separate (wrapping) form
danielebarchiesi@0 787 // builder function to pre-populate the $form array with form elements, which
danielebarchiesi@0 788 // the actual form builder function ($callback) expects. This allows for
danielebarchiesi@0 789 // pre-populating a form with common elements for certain forms, such as
danielebarchiesi@0 790 // back/next/save buttons in multi-step form wizards. See drupal_build_form().
danielebarchiesi@0 791 if (isset($form_state['wrapper_callback']) && function_exists($form_state['wrapper_callback'])) {
danielebarchiesi@0 792 $form = call_user_func_array($form_state['wrapper_callback'], $args);
danielebarchiesi@0 793 // Put the prepopulated $form into $args.
danielebarchiesi@0 794 $args[0] = $form;
danielebarchiesi@0 795 }
danielebarchiesi@0 796
danielebarchiesi@0 797 // If $callback was returned by a hook_forms() implementation, call it.
danielebarchiesi@0 798 // Otherwise, call the function named after the form id.
danielebarchiesi@0 799 $form = call_user_func_array(isset($callback) ? $callback : $form_id, $args);
danielebarchiesi@0 800 $form['#form_id'] = $form_id;
danielebarchiesi@0 801
danielebarchiesi@0 802 return $form;
danielebarchiesi@0 803 }
danielebarchiesi@0 804
danielebarchiesi@0 805 /**
danielebarchiesi@0 806 * Processes a form submission.
danielebarchiesi@0 807 *
danielebarchiesi@0 808 * This function is the heart of form API. The form gets built, validated and in
danielebarchiesi@0 809 * appropriate cases, submitted and rebuilt.
danielebarchiesi@0 810 *
danielebarchiesi@0 811 * @param $form_id
danielebarchiesi@0 812 * The unique string identifying the current form.
danielebarchiesi@0 813 * @param $form
danielebarchiesi@0 814 * An associative array containing the structure of the form.
danielebarchiesi@0 815 * @param $form_state
danielebarchiesi@0 816 * A keyed array containing the current state of the form. This
danielebarchiesi@0 817 * includes the current persistent storage data for the form, and
danielebarchiesi@0 818 * any data passed along by earlier steps when displaying a
danielebarchiesi@0 819 * multi-step form. Additional information, like the sanitized $_POST
danielebarchiesi@0 820 * data, is also accumulated here.
danielebarchiesi@0 821 */
danielebarchiesi@0 822 function drupal_process_form($form_id, &$form, &$form_state) {
danielebarchiesi@0 823 $form_state['values'] = array();
danielebarchiesi@0 824
danielebarchiesi@0 825 // With $_GET, these forms are always submitted if requested.
danielebarchiesi@0 826 if ($form_state['method'] == 'get' && !empty($form_state['always_process'])) {
danielebarchiesi@0 827 if (!isset($form_state['input']['form_build_id'])) {
danielebarchiesi@0 828 $form_state['input']['form_build_id'] = $form['#build_id'];
danielebarchiesi@0 829 }
danielebarchiesi@0 830 if (!isset($form_state['input']['form_id'])) {
danielebarchiesi@0 831 $form_state['input']['form_id'] = $form_id;
danielebarchiesi@0 832 }
danielebarchiesi@0 833 if (!isset($form_state['input']['form_token']) && isset($form['#token'])) {
danielebarchiesi@0 834 $form_state['input']['form_token'] = drupal_get_token($form['#token']);
danielebarchiesi@0 835 }
danielebarchiesi@0 836 }
danielebarchiesi@0 837
danielebarchiesi@0 838 // form_builder() finishes building the form by calling element #process
danielebarchiesi@0 839 // functions and mapping user input, if any, to #value properties, and also
danielebarchiesi@0 840 // storing the values in $form_state['values']. We need to retain the
danielebarchiesi@0 841 // unprocessed $form in case it needs to be cached.
danielebarchiesi@0 842 $unprocessed_form = $form;
danielebarchiesi@0 843 $form = form_builder($form_id, $form, $form_state);
danielebarchiesi@0 844
danielebarchiesi@0 845 // Only process the input if we have a correct form submission.
danielebarchiesi@0 846 if ($form_state['process_input']) {
danielebarchiesi@0 847 drupal_validate_form($form_id, $form, $form_state);
danielebarchiesi@0 848
danielebarchiesi@0 849 // drupal_html_id() maintains a cache of element IDs it has seen,
danielebarchiesi@0 850 // so it can prevent duplicates. We want to be sure we reset that
danielebarchiesi@0 851 // cache when a form is processed, so scenarios that result in
danielebarchiesi@0 852 // the form being built behind the scenes and again for the
danielebarchiesi@0 853 // browser don't increment all the element IDs needlessly.
danielebarchiesi@0 854 if (!form_get_errors()) {
danielebarchiesi@0 855 // In case of errors, do not break HTML IDs of other forms.
danielebarchiesi@0 856 drupal_static_reset('drupal_html_id');
danielebarchiesi@0 857 }
danielebarchiesi@0 858
danielebarchiesi@0 859 if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) {
danielebarchiesi@0 860 // Execute form submit handlers.
danielebarchiesi@0 861 form_execute_handlers('submit', $form, $form_state);
danielebarchiesi@0 862
danielebarchiesi@0 863 // We'll clear out the cached copies of the form and its stored data
danielebarchiesi@0 864 // here, as we've finished with them. The in-memory copies are still
danielebarchiesi@0 865 // here, though.
danielebarchiesi@0 866 if (!variable_get('cache', 0) && !empty($form_state['values']['form_build_id'])) {
danielebarchiesi@0 867 cache_clear_all('form_' . $form_state['values']['form_build_id'], 'cache_form');
danielebarchiesi@0 868 cache_clear_all('form_state_' . $form_state['values']['form_build_id'], 'cache_form');
danielebarchiesi@0 869 }
danielebarchiesi@0 870
danielebarchiesi@0 871 // If batches were set in the submit handlers, we process them now,
danielebarchiesi@0 872 // possibly ending execution. We make sure we do not react to the batch
danielebarchiesi@0 873 // that is already being processed (if a batch operation performs a
danielebarchiesi@0 874 // drupal_form_submit).
danielebarchiesi@0 875 if ($batch =& batch_get() && !isset($batch['current_set'])) {
danielebarchiesi@0 876 // Store $form_state information in the batch definition.
danielebarchiesi@0 877 // We need the full $form_state when either:
danielebarchiesi@0 878 // - Some submit handlers were saved to be called during batch
danielebarchiesi@0 879 // processing. See form_execute_handlers().
danielebarchiesi@0 880 // - The form is multistep.
danielebarchiesi@0 881 // In other cases, we only need the information expected by
danielebarchiesi@0 882 // drupal_redirect_form().
danielebarchiesi@0 883 if ($batch['has_form_submits'] || !empty($form_state['rebuild'])) {
danielebarchiesi@0 884 $batch['form_state'] = $form_state;
danielebarchiesi@0 885 }
danielebarchiesi@0 886 else {
danielebarchiesi@0 887 $batch['form_state'] = array_intersect_key($form_state, array_flip(array('programmed', 'rebuild', 'storage', 'no_redirect', 'redirect')));
danielebarchiesi@0 888 }
danielebarchiesi@0 889
danielebarchiesi@0 890 $batch['progressive'] = !$form_state['programmed'];
danielebarchiesi@0 891 batch_process();
danielebarchiesi@0 892
danielebarchiesi@0 893 // Execution continues only for programmatic forms.
danielebarchiesi@0 894 // For 'regular' forms, we get redirected to the batch processing
danielebarchiesi@0 895 // page. Form redirection will be handled in _batch_finished(),
danielebarchiesi@0 896 // after the batch is processed.
danielebarchiesi@0 897 }
danielebarchiesi@0 898
danielebarchiesi@0 899 // Set a flag to indicate the the form has been processed and executed.
danielebarchiesi@0 900 $form_state['executed'] = TRUE;
danielebarchiesi@0 901
danielebarchiesi@0 902 // Redirect the form based on values in $form_state.
danielebarchiesi@0 903 drupal_redirect_form($form_state);
danielebarchiesi@0 904 }
danielebarchiesi@0 905
danielebarchiesi@0 906 // Don't rebuild or cache form submissions invoked via drupal_form_submit().
danielebarchiesi@0 907 if (!empty($form_state['programmed'])) {
danielebarchiesi@0 908 return;
danielebarchiesi@0 909 }
danielebarchiesi@0 910
danielebarchiesi@0 911 // If $form_state['rebuild'] has been set and input has been processed
danielebarchiesi@0 912 // without validation errors, we are in a multi-step workflow that is not
danielebarchiesi@0 913 // yet complete. A new $form needs to be constructed based on the changes
danielebarchiesi@0 914 // made to $form_state during this request. Normally, a submit handler sets
danielebarchiesi@0 915 // $form_state['rebuild'] if a fully executed form requires another step.
danielebarchiesi@0 916 // However, for forms that have not been fully executed (e.g., Ajax
danielebarchiesi@0 917 // submissions triggered by non-buttons), there is no submit handler to set
danielebarchiesi@0 918 // $form_state['rebuild']. It would not make sense to redisplay the
danielebarchiesi@0 919 // identical form without an error for the user to correct, so we also
danielebarchiesi@0 920 // rebuild error-free non-executed forms, regardless of
danielebarchiesi@0 921 // $form_state['rebuild'].
danielebarchiesi@0 922 // @todo D8: Simplify this logic; considering Ajax and non-HTML front-ends,
danielebarchiesi@0 923 // along with element-level #submit properties, it makes no sense to have
danielebarchiesi@0 924 // divergent form execution based on whether the triggering element has
danielebarchiesi@0 925 // #executes_submit_callback set to TRUE.
danielebarchiesi@0 926 if (($form_state['rebuild'] || !$form_state['executed']) && !form_get_errors()) {
danielebarchiesi@0 927 // Form building functions (e.g., _form_builder_handle_input_element())
danielebarchiesi@0 928 // may use $form_state['rebuild'] to determine if they are running in the
danielebarchiesi@0 929 // context of a rebuild, so ensure it is set.
danielebarchiesi@0 930 $form_state['rebuild'] = TRUE;
danielebarchiesi@0 931 $form = drupal_rebuild_form($form_id, $form_state, $form);
danielebarchiesi@0 932 }
danielebarchiesi@0 933 }
danielebarchiesi@0 934
danielebarchiesi@0 935 // After processing the form, the form builder or a #process callback may
danielebarchiesi@0 936 // have set $form_state['cache'] to indicate that the form and form state
danielebarchiesi@0 937 // shall be cached. But the form may only be cached if the 'no_cache' property
danielebarchiesi@0 938 // is not set to TRUE. Only cache $form as it was prior to form_builder(),
danielebarchiesi@0 939 // because form_builder() must run for each request to accommodate new user
danielebarchiesi@0 940 // input. Rebuilt forms are not cached here, because drupal_rebuild_form()
danielebarchiesi@0 941 // already takes care of that.
danielebarchiesi@0 942 if (!$form_state['rebuild'] && $form_state['cache'] && empty($form_state['no_cache'])) {
danielebarchiesi@0 943 form_set_cache($form['#build_id'], $unprocessed_form, $form_state);
danielebarchiesi@0 944 }
danielebarchiesi@0 945 }
danielebarchiesi@0 946
danielebarchiesi@0 947 /**
danielebarchiesi@0 948 * Prepares a structured form array.
danielebarchiesi@0 949 *
danielebarchiesi@0 950 * Adds required elements, executes any hook_form_alter functions, and
danielebarchiesi@0 951 * optionally inserts a validation token to prevent tampering.
danielebarchiesi@0 952 *
danielebarchiesi@0 953 * @param $form_id
danielebarchiesi@0 954 * A unique string identifying the form for validation, submission,
danielebarchiesi@0 955 * theming, and hook_form_alter functions.
danielebarchiesi@0 956 * @param $form
danielebarchiesi@0 957 * An associative array containing the structure of the form.
danielebarchiesi@0 958 * @param $form_state
danielebarchiesi@0 959 * A keyed array containing the current state of the form. Passed
danielebarchiesi@0 960 * in here so that hook_form_alter() calls can use it, as well.
danielebarchiesi@0 961 */
danielebarchiesi@0 962 function drupal_prepare_form($form_id, &$form, &$form_state) {
danielebarchiesi@0 963 global $user;
danielebarchiesi@0 964
danielebarchiesi@0 965 $form['#type'] = 'form';
danielebarchiesi@0 966 $form_state['programmed'] = isset($form_state['programmed']) ? $form_state['programmed'] : FALSE;
danielebarchiesi@0 967
danielebarchiesi@0 968 // Fix the form method, if it is 'get' in $form_state, but not in $form.
danielebarchiesi@0 969 if ($form_state['method'] == 'get' && !isset($form['#method'])) {
danielebarchiesi@0 970 $form['#method'] = 'get';
danielebarchiesi@0 971 }
danielebarchiesi@0 972
danielebarchiesi@0 973 // Generate a new #build_id for this form, if none has been set already. The
danielebarchiesi@0 974 // form_build_id is used as key to cache a particular build of the form. For
danielebarchiesi@0 975 // multi-step forms, this allows the user to go back to an earlier build, make
danielebarchiesi@0 976 // changes, and re-submit.
danielebarchiesi@0 977 // @see drupal_build_form()
danielebarchiesi@0 978 // @see drupal_rebuild_form()
danielebarchiesi@0 979 if (!isset($form['#build_id'])) {
danielebarchiesi@0 980 $form['#build_id'] = 'form-' . drupal_hash_base64(uniqid(mt_rand(), TRUE) . mt_rand());
danielebarchiesi@0 981 }
danielebarchiesi@0 982 $form['form_build_id'] = array(
danielebarchiesi@0 983 '#type' => 'hidden',
danielebarchiesi@0 984 '#value' => $form['#build_id'],
danielebarchiesi@0 985 '#id' => $form['#build_id'],
danielebarchiesi@0 986 '#name' => 'form_build_id',
danielebarchiesi@0 987 // Form processing and validation requires this value, so ensure the
danielebarchiesi@0 988 // submitted form value appears literally, regardless of custom #tree
danielebarchiesi@0 989 // and #parents being set elsewhere.
danielebarchiesi@0 990 '#parents' => array('form_build_id'),
danielebarchiesi@0 991 );
danielebarchiesi@0 992
danielebarchiesi@0 993 // Add a token, based on either #token or form_id, to any form displayed to
danielebarchiesi@0 994 // authenticated users. This ensures that any submitted form was actually
danielebarchiesi@0 995 // requested previously by the user and protects against cross site request
danielebarchiesi@0 996 // forgeries.
danielebarchiesi@0 997 // This does not apply to programmatically submitted forms. Furthermore, since
danielebarchiesi@0 998 // tokens are session-bound and forms displayed to anonymous users are very
danielebarchiesi@0 999 // likely cached, we cannot assign a token for them.
danielebarchiesi@0 1000 // During installation, there is no $user yet.
danielebarchiesi@0 1001 if (!empty($user->uid) && !$form_state['programmed']) {
danielebarchiesi@0 1002 // Form constructors may explicitly set #token to FALSE when cross site
danielebarchiesi@0 1003 // request forgery is irrelevant to the form, such as search forms.
danielebarchiesi@0 1004 if (isset($form['#token']) && $form['#token'] === FALSE) {
danielebarchiesi@0 1005 unset($form['#token']);
danielebarchiesi@0 1006 }
danielebarchiesi@0 1007 // Otherwise, generate a public token based on the form id.
danielebarchiesi@0 1008 else {
danielebarchiesi@0 1009 $form['#token'] = $form_id;
danielebarchiesi@0 1010 $form['form_token'] = array(
danielebarchiesi@0 1011 '#id' => drupal_html_id('edit-' . $form_id . '-form-token'),
danielebarchiesi@0 1012 '#type' => 'token',
danielebarchiesi@0 1013 '#default_value' => drupal_get_token($form['#token']),
danielebarchiesi@0 1014 // Form processing and validation requires this value, so ensure the
danielebarchiesi@0 1015 // submitted form value appears literally, regardless of custom #tree
danielebarchiesi@0 1016 // and #parents being set elsewhere.
danielebarchiesi@0 1017 '#parents' => array('form_token'),
danielebarchiesi@0 1018 );
danielebarchiesi@0 1019 }
danielebarchiesi@0 1020 }
danielebarchiesi@0 1021
danielebarchiesi@0 1022 if (isset($form_id)) {
danielebarchiesi@0 1023 $form['form_id'] = array(
danielebarchiesi@0 1024 '#type' => 'hidden',
danielebarchiesi@0 1025 '#value' => $form_id,
danielebarchiesi@0 1026 '#id' => drupal_html_id("edit-$form_id"),
danielebarchiesi@0 1027 // Form processing and validation requires this value, so ensure the
danielebarchiesi@0 1028 // submitted form value appears literally, regardless of custom #tree
danielebarchiesi@0 1029 // and #parents being set elsewhere.
danielebarchiesi@0 1030 '#parents' => array('form_id'),
danielebarchiesi@0 1031 );
danielebarchiesi@0 1032 }
danielebarchiesi@0 1033 if (!isset($form['#id'])) {
danielebarchiesi@0 1034 $form['#id'] = drupal_html_id($form_id);
danielebarchiesi@0 1035 }
danielebarchiesi@0 1036
danielebarchiesi@0 1037 $form += element_info('form');
danielebarchiesi@0 1038 $form += array('#tree' => FALSE, '#parents' => array());
danielebarchiesi@0 1039
danielebarchiesi@0 1040 if (!isset($form['#validate'])) {
danielebarchiesi@0 1041 // Ensure that modules can rely on #validate being set.
danielebarchiesi@0 1042 $form['#validate'] = array();
danielebarchiesi@0 1043 // Check for a handler specific to $form_id.
danielebarchiesi@0 1044 if (function_exists($form_id . '_validate')) {
danielebarchiesi@0 1045 $form['#validate'][] = $form_id . '_validate';
danielebarchiesi@0 1046 }
danielebarchiesi@0 1047 // Otherwise check whether this is a shared form and whether there is a
danielebarchiesi@0 1048 // handler for the shared $form_id.
danielebarchiesi@0 1049 elseif (isset($form_state['build_info']['base_form_id']) && function_exists($form_state['build_info']['base_form_id'] . '_validate')) {
danielebarchiesi@0 1050 $form['#validate'][] = $form_state['build_info']['base_form_id'] . '_validate';
danielebarchiesi@0 1051 }
danielebarchiesi@0 1052 }
danielebarchiesi@0 1053
danielebarchiesi@0 1054 if (!isset($form['#submit'])) {
danielebarchiesi@0 1055 // Ensure that modules can rely on #submit being set.
danielebarchiesi@0 1056 $form['#submit'] = array();
danielebarchiesi@0 1057 // Check for a handler specific to $form_id.
danielebarchiesi@0 1058 if (function_exists($form_id . '_submit')) {
danielebarchiesi@0 1059 $form['#submit'][] = $form_id . '_submit';
danielebarchiesi@0 1060 }
danielebarchiesi@0 1061 // Otherwise check whether this is a shared form and whether there is a
danielebarchiesi@0 1062 // handler for the shared $form_id.
danielebarchiesi@0 1063 elseif (isset($form_state['build_info']['base_form_id']) && function_exists($form_state['build_info']['base_form_id'] . '_submit')) {
danielebarchiesi@0 1064 $form['#submit'][] = $form_state['build_info']['base_form_id'] . '_submit';
danielebarchiesi@0 1065 }
danielebarchiesi@0 1066 }
danielebarchiesi@0 1067
danielebarchiesi@0 1068 // If no #theme has been set, automatically apply theme suggestions.
danielebarchiesi@0 1069 // theme_form() itself is in #theme_wrappers and not #theme. Therefore, the
danielebarchiesi@0 1070 // #theme function only has to care for rendering the inner form elements,
danielebarchiesi@0 1071 // not the form itself.
danielebarchiesi@0 1072 if (!isset($form['#theme'])) {
danielebarchiesi@0 1073 $form['#theme'] = array($form_id);
danielebarchiesi@0 1074 if (isset($form_state['build_info']['base_form_id'])) {
danielebarchiesi@0 1075 $form['#theme'][] = $form_state['build_info']['base_form_id'];
danielebarchiesi@0 1076 }
danielebarchiesi@0 1077 }
danielebarchiesi@0 1078
danielebarchiesi@0 1079 // Invoke hook_form_alter(), hook_form_BASE_FORM_ID_alter(), and
danielebarchiesi@0 1080 // hook_form_FORM_ID_alter() implementations.
danielebarchiesi@0 1081 $hooks = array('form');
danielebarchiesi@0 1082 if (isset($form_state['build_info']['base_form_id'])) {
danielebarchiesi@0 1083 $hooks[] = 'form_' . $form_state['build_info']['base_form_id'];
danielebarchiesi@0 1084 }
danielebarchiesi@0 1085 $hooks[] = 'form_' . $form_id;
danielebarchiesi@0 1086 drupal_alter($hooks, $form, $form_state, $form_id);
danielebarchiesi@0 1087 }
danielebarchiesi@0 1088
danielebarchiesi@0 1089
danielebarchiesi@0 1090 /**
danielebarchiesi@0 1091 * Validates user-submitted form data in the $form_state array.
danielebarchiesi@0 1092 *
danielebarchiesi@0 1093 * @param $form_id
danielebarchiesi@0 1094 * A unique string identifying the form for validation, submission,
danielebarchiesi@0 1095 * theming, and hook_form_alter functions.
danielebarchiesi@0 1096 * @param $form
danielebarchiesi@0 1097 * An associative array containing the structure of the form, which is passed
danielebarchiesi@0 1098 * by reference. Form validation handlers are able to alter the form structure
danielebarchiesi@0 1099 * (like #process and #after_build callbacks during form building) in case of
danielebarchiesi@0 1100 * a validation error. If a validation handler alters the form structure, it
danielebarchiesi@0 1101 * is responsible for validating the values of changed form elements in
danielebarchiesi@0 1102 * $form_state['values'] to prevent form submit handlers from receiving
danielebarchiesi@0 1103 * unvalidated values.
danielebarchiesi@0 1104 * @param $form_state
danielebarchiesi@0 1105 * A keyed array containing the current state of the form. The current
danielebarchiesi@0 1106 * user-submitted data is stored in $form_state['values'], though
danielebarchiesi@0 1107 * form validation functions are passed an explicit copy of the
danielebarchiesi@0 1108 * values for the sake of simplicity. Validation handlers can also use
danielebarchiesi@0 1109 * $form_state to pass information on to submit handlers. For example:
danielebarchiesi@0 1110 * $form_state['data_for_submission'] = $data;
danielebarchiesi@0 1111 * This technique is useful when validation requires file parsing,
danielebarchiesi@0 1112 * web service requests, or other expensive requests that should
danielebarchiesi@0 1113 * not be repeated in the submission step.
danielebarchiesi@0 1114 */
danielebarchiesi@0 1115 function drupal_validate_form($form_id, &$form, &$form_state) {
danielebarchiesi@0 1116 $validated_forms = &drupal_static(__FUNCTION__, array());
danielebarchiesi@0 1117
danielebarchiesi@0 1118 if (isset($validated_forms[$form_id]) && empty($form_state['must_validate'])) {
danielebarchiesi@0 1119 return;
danielebarchiesi@0 1120 }
danielebarchiesi@0 1121
danielebarchiesi@0 1122 // If the session token was set by drupal_prepare_form(), ensure that it
danielebarchiesi@0 1123 // matches the current user's session.
danielebarchiesi@0 1124 if (isset($form['#token'])) {
danielebarchiesi@0 1125 if (!drupal_valid_token($form_state['values']['form_token'], $form['#token'])) {
danielebarchiesi@0 1126 $path = current_path();
danielebarchiesi@0 1127 $query = drupal_get_query_parameters();
danielebarchiesi@0 1128 $url = url($path, array('query' => $query));
danielebarchiesi@0 1129
danielebarchiesi@0 1130 // Setting this error will cause the form to fail validation.
danielebarchiesi@0 1131 form_set_error('form_token', t('The form has become outdated. Copy any unsaved work in the form below and then <a href="@link">reload this page</a>.', array('@link' => $url)));
danielebarchiesi@0 1132 }
danielebarchiesi@0 1133 }
danielebarchiesi@0 1134
danielebarchiesi@0 1135 _form_validate($form, $form_state, $form_id);
danielebarchiesi@0 1136 $validated_forms[$form_id] = TRUE;
danielebarchiesi@0 1137
danielebarchiesi@0 1138 // If validation errors are limited then remove any non validated form values,
danielebarchiesi@0 1139 // so that only values that passed validation are left for submit callbacks.
danielebarchiesi@0 1140 if (isset($form_state['triggering_element']['#limit_validation_errors']) && $form_state['triggering_element']['#limit_validation_errors'] !== FALSE) {
danielebarchiesi@0 1141 $values = array();
danielebarchiesi@0 1142 foreach ($form_state['triggering_element']['#limit_validation_errors'] as $section) {
danielebarchiesi@0 1143 // If the section exists within $form_state['values'], even if the value
danielebarchiesi@0 1144 // is NULL, copy it to $values.
danielebarchiesi@0 1145 $section_exists = NULL;
danielebarchiesi@0 1146 $value = drupal_array_get_nested_value($form_state['values'], $section, $section_exists);
danielebarchiesi@0 1147 if ($section_exists) {
danielebarchiesi@0 1148 drupal_array_set_nested_value($values, $section, $value);
danielebarchiesi@0 1149 }
danielebarchiesi@0 1150 }
danielebarchiesi@0 1151 // A button's #value does not require validation, so for convenience we
danielebarchiesi@0 1152 // allow the value of the clicked button to be retained in its normal
danielebarchiesi@0 1153 // $form_state['values'] locations, even if these locations are not included
danielebarchiesi@0 1154 // in #limit_validation_errors.
danielebarchiesi@0 1155 if (isset($form_state['triggering_element']['#button_type'])) {
danielebarchiesi@0 1156 $button_value = $form_state['triggering_element']['#value'];
danielebarchiesi@0 1157
danielebarchiesi@0 1158 // Like all input controls, the button value may be in the location
danielebarchiesi@0 1159 // dictated by #parents. If it is, copy it to $values, but do not override
danielebarchiesi@0 1160 // what may already be in $values.
danielebarchiesi@0 1161 $parents = $form_state['triggering_element']['#parents'];
danielebarchiesi@0 1162 if (!drupal_array_nested_key_exists($values, $parents) && drupal_array_get_nested_value($form_state['values'], $parents) === $button_value) {
danielebarchiesi@0 1163 drupal_array_set_nested_value($values, $parents, $button_value);
danielebarchiesi@0 1164 }
danielebarchiesi@0 1165
danielebarchiesi@0 1166 // Additionally, form_builder() places the button value in
danielebarchiesi@0 1167 // $form_state['values'][BUTTON_NAME]. If it's still there, after
danielebarchiesi@0 1168 // validation handlers have run, copy it to $values, but do not override
danielebarchiesi@0 1169 // what may already be in $values.
danielebarchiesi@0 1170 $name = $form_state['triggering_element']['#name'];
danielebarchiesi@0 1171 if (!isset($values[$name]) && isset($form_state['values'][$name]) && $form_state['values'][$name] === $button_value) {
danielebarchiesi@0 1172 $values[$name] = $button_value;
danielebarchiesi@0 1173 }
danielebarchiesi@0 1174 }
danielebarchiesi@0 1175 $form_state['values'] = $values;
danielebarchiesi@0 1176 }
danielebarchiesi@0 1177 }
danielebarchiesi@0 1178
danielebarchiesi@0 1179 /**
danielebarchiesi@0 1180 * Redirects the user to a URL after a form has been processed.
danielebarchiesi@0 1181 *
danielebarchiesi@0 1182 * After a form is submitted and processed, normally the user should be
danielebarchiesi@0 1183 * redirected to a new destination page. This function figures out what that
danielebarchiesi@0 1184 * destination should be, based on the $form_state array and the 'destination'
danielebarchiesi@0 1185 * query string in the request URL, and redirects the user there.
danielebarchiesi@0 1186 *
danielebarchiesi@0 1187 * Usually (for exceptions, see below) $form_state['redirect'] determines where
danielebarchiesi@0 1188 * to redirect the user. This can be set either to a string (the path to
danielebarchiesi@0 1189 * redirect to), or an array of arguments for drupal_goto(). If
danielebarchiesi@0 1190 * $form_state['redirect'] is missing, the user is usually (again, see below for
danielebarchiesi@0 1191 * exceptions) redirected back to the page they came from, where they should see
danielebarchiesi@0 1192 * a fresh, unpopulated copy of the form.
danielebarchiesi@0 1193 *
danielebarchiesi@0 1194 * Here is an example of how to set up a form to redirect to the path 'node':
danielebarchiesi@0 1195 * @code
danielebarchiesi@0 1196 * $form_state['redirect'] = 'node';
danielebarchiesi@0 1197 * @endcode
danielebarchiesi@0 1198 * And here is an example of how to redirect to 'node/123?foo=bar#baz':
danielebarchiesi@0 1199 * @code
danielebarchiesi@0 1200 * $form_state['redirect'] = array(
danielebarchiesi@0 1201 * 'node/123',
danielebarchiesi@0 1202 * array(
danielebarchiesi@0 1203 * 'query' => array(
danielebarchiesi@0 1204 * 'foo' => 'bar',
danielebarchiesi@0 1205 * ),
danielebarchiesi@0 1206 * 'fragment' => 'baz',
danielebarchiesi@0 1207 * ),
danielebarchiesi@0 1208 * );
danielebarchiesi@0 1209 * @endcode
danielebarchiesi@0 1210 *
danielebarchiesi@0 1211 * There are several exceptions to the "usual" behavior described above:
danielebarchiesi@0 1212 * - If $form_state['programmed'] is TRUE, the form submission was usually
danielebarchiesi@0 1213 * invoked via drupal_form_submit(), so any redirection would break the script
danielebarchiesi@0 1214 * that invoked drupal_form_submit() and no redirection is done.
danielebarchiesi@0 1215 * - If $form_state['rebuild'] is TRUE, the form is being rebuilt, and no
danielebarchiesi@0 1216 * redirection is done.
danielebarchiesi@0 1217 * - If $form_state['no_redirect'] is TRUE, redirection is disabled. This is
danielebarchiesi@0 1218 * set, for instance, by ajax_get_form() to prevent redirection in Ajax
danielebarchiesi@0 1219 * callbacks. $form_state['no_redirect'] should never be set or altered by
danielebarchiesi@0 1220 * form builder functions or form validation/submit handlers.
danielebarchiesi@0 1221 * - If $form_state['redirect'] is set to FALSE, redirection is disabled.
danielebarchiesi@0 1222 * - If none of the above conditions has prevented redirection, then the
danielebarchiesi@0 1223 * redirect is accomplished by calling drupal_goto(), passing in the value of
danielebarchiesi@0 1224 * $form_state['redirect'] if it is set, or the current path if it is
danielebarchiesi@0 1225 * not. drupal_goto() preferentially uses the value of $_GET['destination']
danielebarchiesi@0 1226 * (the 'destination' URL query string) if it is present, so this will
danielebarchiesi@0 1227 * override any values set by $form_state['redirect']. Note that during
danielebarchiesi@0 1228 * installation, install_goto() is called in place of drupal_goto().
danielebarchiesi@0 1229 *
danielebarchiesi@0 1230 * @param $form_state
danielebarchiesi@0 1231 * An associative array containing the current state of the form.
danielebarchiesi@0 1232 *
danielebarchiesi@0 1233 * @see drupal_process_form()
danielebarchiesi@0 1234 * @see drupal_build_form()
danielebarchiesi@0 1235 */
danielebarchiesi@0 1236 function drupal_redirect_form($form_state) {
danielebarchiesi@0 1237 // Skip redirection for form submissions invoked via drupal_form_submit().
danielebarchiesi@0 1238 if (!empty($form_state['programmed'])) {
danielebarchiesi@0 1239 return;
danielebarchiesi@0 1240 }
danielebarchiesi@0 1241 // Skip redirection if rebuild is activated.
danielebarchiesi@0 1242 if (!empty($form_state['rebuild'])) {
danielebarchiesi@0 1243 return;
danielebarchiesi@0 1244 }
danielebarchiesi@0 1245 // Skip redirection if it was explicitly disallowed.
danielebarchiesi@0 1246 if (!empty($form_state['no_redirect'])) {
danielebarchiesi@0 1247 return;
danielebarchiesi@0 1248 }
danielebarchiesi@0 1249 // Only invoke drupal_goto() if redirect value was not set to FALSE.
danielebarchiesi@0 1250 if (!isset($form_state['redirect']) || $form_state['redirect'] !== FALSE) {
danielebarchiesi@0 1251 if (isset($form_state['redirect'])) {
danielebarchiesi@0 1252 if (is_array($form_state['redirect'])) {
danielebarchiesi@0 1253 call_user_func_array('drupal_goto', $form_state['redirect']);
danielebarchiesi@0 1254 }
danielebarchiesi@0 1255 else {
danielebarchiesi@0 1256 // This function can be called from the installer, which guarantees
danielebarchiesi@0 1257 // that $redirect will always be a string, so catch that case here
danielebarchiesi@0 1258 // and use the appropriate redirect function.
danielebarchiesi@0 1259 $function = drupal_installation_attempted() ? 'install_goto' : 'drupal_goto';
danielebarchiesi@0 1260 $function($form_state['redirect']);
danielebarchiesi@0 1261 }
danielebarchiesi@0 1262 }
danielebarchiesi@0 1263 drupal_goto(current_path(), array('query' => drupal_get_query_parameters()));
danielebarchiesi@0 1264 }
danielebarchiesi@0 1265 }
danielebarchiesi@0 1266
danielebarchiesi@0 1267 /**
danielebarchiesi@0 1268 * Performs validation on form elements.
danielebarchiesi@0 1269 *
danielebarchiesi@0 1270 * First ensures required fields are completed, #maxlength is not exceeded, and
danielebarchiesi@0 1271 * selected options were in the list of options given to the user. Then calls
danielebarchiesi@0 1272 * user-defined validators.
danielebarchiesi@0 1273 *
danielebarchiesi@0 1274 * @param $elements
danielebarchiesi@0 1275 * An associative array containing the structure of the form.
danielebarchiesi@0 1276 * @param $form_state
danielebarchiesi@0 1277 * A keyed array containing the current state of the form. The current
danielebarchiesi@0 1278 * user-submitted data is stored in $form_state['values'], though
danielebarchiesi@0 1279 * form validation functions are passed an explicit copy of the
danielebarchiesi@0 1280 * values for the sake of simplicity. Validation handlers can also
danielebarchiesi@0 1281 * $form_state to pass information on to submit handlers. For example:
danielebarchiesi@0 1282 * $form_state['data_for_submission'] = $data;
danielebarchiesi@0 1283 * This technique is useful when validation requires file parsing,
danielebarchiesi@0 1284 * web service requests, or other expensive requests that should
danielebarchiesi@0 1285 * not be repeated in the submission step.
danielebarchiesi@0 1286 * @param $form_id
danielebarchiesi@0 1287 * A unique string identifying the form for validation, submission,
danielebarchiesi@0 1288 * theming, and hook_form_alter functions.
danielebarchiesi@0 1289 */
danielebarchiesi@0 1290 function _form_validate(&$elements, &$form_state, $form_id = NULL) {
danielebarchiesi@0 1291 // Also used in the installer, pre-database setup.
danielebarchiesi@0 1292 $t = get_t();
danielebarchiesi@0 1293
danielebarchiesi@0 1294 // Recurse through all children.
danielebarchiesi@0 1295 foreach (element_children($elements) as $key) {
danielebarchiesi@0 1296 if (isset($elements[$key]) && $elements[$key]) {
danielebarchiesi@0 1297 _form_validate($elements[$key], $form_state);
danielebarchiesi@0 1298 }
danielebarchiesi@0 1299 }
danielebarchiesi@0 1300
danielebarchiesi@0 1301 // Validate the current input.
danielebarchiesi@0 1302 if (!isset($elements['#validated']) || !$elements['#validated']) {
danielebarchiesi@0 1303 // The following errors are always shown.
danielebarchiesi@0 1304 if (isset($elements['#needs_validation'])) {
danielebarchiesi@0 1305 // Verify that the value is not longer than #maxlength.
danielebarchiesi@0 1306 if (isset($elements['#maxlength']) && drupal_strlen($elements['#value']) > $elements['#maxlength']) {
danielebarchiesi@0 1307 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']))));
danielebarchiesi@0 1308 }
danielebarchiesi@0 1309
danielebarchiesi@0 1310 if (isset($elements['#options']) && isset($elements['#value'])) {
danielebarchiesi@0 1311 if ($elements['#type'] == 'select') {
danielebarchiesi@0 1312 $options = form_options_flatten($elements['#options']);
danielebarchiesi@0 1313 }
danielebarchiesi@0 1314 else {
danielebarchiesi@0 1315 $options = $elements['#options'];
danielebarchiesi@0 1316 }
danielebarchiesi@0 1317 if (is_array($elements['#value'])) {
danielebarchiesi@0 1318 $value = in_array($elements['#type'], array('checkboxes', 'tableselect')) ? array_keys($elements['#value']) : $elements['#value'];
danielebarchiesi@0 1319 foreach ($value as $v) {
danielebarchiesi@0 1320 if (!isset($options[$v])) {
danielebarchiesi@0 1321 form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
danielebarchiesi@0 1322 watchdog('form', 'Illegal choice %choice in !name element.', array('%choice' => $v, '!name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
danielebarchiesi@0 1323 }
danielebarchiesi@0 1324 }
danielebarchiesi@0 1325 }
danielebarchiesi@0 1326 // Non-multiple select fields always have a value in HTML. If the user
danielebarchiesi@0 1327 // does not change the form, it will be the value of the first option.
danielebarchiesi@0 1328 // Because of this, form validation for the field will almost always
danielebarchiesi@0 1329 // pass, even if the user did not select anything. To work around this
danielebarchiesi@0 1330 // browser behavior, required select fields without a #default_value get
danielebarchiesi@0 1331 // an additional, first empty option. In case the submitted value is
danielebarchiesi@0 1332 // identical to the empty option's value, we reset the element's value
danielebarchiesi@0 1333 // to NULL to trigger the regular #required handling below.
danielebarchiesi@0 1334 // @see form_process_select()
danielebarchiesi@0 1335 elseif ($elements['#type'] == 'select' && !$elements['#multiple'] && $elements['#required'] && !isset($elements['#default_value']) && $elements['#value'] === $elements['#empty_value']) {
danielebarchiesi@0 1336 $elements['#value'] = NULL;
danielebarchiesi@0 1337 form_set_value($elements, NULL, $form_state);
danielebarchiesi@0 1338 }
danielebarchiesi@0 1339 elseif (!isset($options[$elements['#value']])) {
danielebarchiesi@0 1340 form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
danielebarchiesi@0 1341 watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
danielebarchiesi@0 1342 }
danielebarchiesi@0 1343 }
danielebarchiesi@0 1344 }
danielebarchiesi@0 1345
danielebarchiesi@0 1346 // While this element is being validated, it may be desired that some calls
danielebarchiesi@0 1347 // to form_set_error() be suppressed and not result in a form error, so
danielebarchiesi@0 1348 // that a button that implements low-risk functionality (such as "Previous"
danielebarchiesi@0 1349 // or "Add more") that doesn't require all user input to be valid can still
danielebarchiesi@0 1350 // have its submit handlers triggered. The triggering element's
danielebarchiesi@0 1351 // #limit_validation_errors property contains the information for which
danielebarchiesi@0 1352 // errors are needed, and all other errors are to be suppressed. The
danielebarchiesi@0 1353 // #limit_validation_errors property is ignored if submit handlers will run,
danielebarchiesi@0 1354 // but the element doesn't have a #submit property, because it's too large a
danielebarchiesi@0 1355 // security risk to have any invalid user input when executing form-level
danielebarchiesi@0 1356 // submit handlers.
danielebarchiesi@0 1357 if (isset($form_state['triggering_element']['#limit_validation_errors']) && ($form_state['triggering_element']['#limit_validation_errors'] !== FALSE) && !($form_state['submitted'] && !isset($form_state['triggering_element']['#submit']))) {
danielebarchiesi@0 1358 form_set_error(NULL, '', $form_state['triggering_element']['#limit_validation_errors']);
danielebarchiesi@0 1359 }
danielebarchiesi@0 1360 // If submit handlers won't run (due to the submission having been triggered
danielebarchiesi@0 1361 // by an element whose #executes_submit_callback property isn't TRUE), then
danielebarchiesi@0 1362 // it's safe to suppress all validation errors, and we do so by default,
danielebarchiesi@0 1363 // which is particularly useful during an Ajax submission triggered by a
danielebarchiesi@0 1364 // non-button. An element can override this default by setting the
danielebarchiesi@0 1365 // #limit_validation_errors property. For button element types,
danielebarchiesi@0 1366 // #limit_validation_errors defaults to FALSE (via system_element_info()),
danielebarchiesi@0 1367 // so that full validation is their default behavior.
danielebarchiesi@0 1368 elseif (isset($form_state['triggering_element']) && !isset($form_state['triggering_element']['#limit_validation_errors']) && !$form_state['submitted']) {
danielebarchiesi@0 1369 form_set_error(NULL, '', array());
danielebarchiesi@0 1370 }
danielebarchiesi@0 1371 // As an extra security measure, explicitly turn off error suppression if
danielebarchiesi@0 1372 // one of the above conditions wasn't met. Since this is also done at the
danielebarchiesi@0 1373 // end of this function, doing it here is only to handle the rare edge case
danielebarchiesi@0 1374 // where a validate handler invokes form processing of another form.
danielebarchiesi@0 1375 else {
danielebarchiesi@0 1376 drupal_static_reset('form_set_error:limit_validation_errors');
danielebarchiesi@0 1377 }
danielebarchiesi@0 1378
danielebarchiesi@0 1379 // Make sure a value is passed when the field is required.
danielebarchiesi@0 1380 if (isset($elements['#needs_validation']) && $elements['#required']) {
danielebarchiesi@0 1381 // A simple call to empty() will not cut it here as some fields, like
danielebarchiesi@0 1382 // checkboxes, can return a valid value of '0'. Instead, check the
danielebarchiesi@0 1383 // length if it's a string, and the item count if it's an array.
danielebarchiesi@0 1384 // An unchecked checkbox has a #value of integer 0, different than string
danielebarchiesi@0 1385 // '0', which could be a valid value.
danielebarchiesi@0 1386 $is_empty_multiple = (!count($elements['#value']));
danielebarchiesi@0 1387 $is_empty_string = (is_string($elements['#value']) && drupal_strlen(trim($elements['#value'])) == 0);
danielebarchiesi@0 1388 $is_empty_value = ($elements['#value'] === 0);
danielebarchiesi@0 1389 if ($is_empty_multiple || $is_empty_string || $is_empty_value) {
danielebarchiesi@0 1390 // Although discouraged, a #title is not mandatory for form elements. In
danielebarchiesi@0 1391 // case there is no #title, we cannot set a form error message.
danielebarchiesi@0 1392 // Instead of setting no #title, form constructors are encouraged to set
danielebarchiesi@0 1393 // #title_display to 'invisible' to improve accessibility.
danielebarchiesi@0 1394 if (isset($elements['#title'])) {
danielebarchiesi@0 1395 form_error($elements, $t('!name field is required.', array('!name' => $elements['#title'])));
danielebarchiesi@0 1396 }
danielebarchiesi@0 1397 else {
danielebarchiesi@0 1398 form_error($elements);
danielebarchiesi@0 1399 }
danielebarchiesi@0 1400 }
danielebarchiesi@0 1401 }
danielebarchiesi@0 1402
danielebarchiesi@0 1403 // Call user-defined form level validators.
danielebarchiesi@0 1404 if (isset($form_id)) {
danielebarchiesi@0 1405 form_execute_handlers('validate', $elements, $form_state);
danielebarchiesi@0 1406 }
danielebarchiesi@0 1407 // Call any element-specific validators. These must act on the element
danielebarchiesi@0 1408 // #value data.
danielebarchiesi@0 1409 elseif (isset($elements['#element_validate'])) {
danielebarchiesi@0 1410 foreach ($elements['#element_validate'] as $function) {
danielebarchiesi@0 1411 $function($elements, $form_state, $form_state['complete form']);
danielebarchiesi@0 1412 }
danielebarchiesi@0 1413 }
danielebarchiesi@0 1414 $elements['#validated'] = TRUE;
danielebarchiesi@0 1415 }
danielebarchiesi@0 1416
danielebarchiesi@0 1417 // Done validating this element, so turn off error suppression.
danielebarchiesi@0 1418 // _form_validate() turns it on again when starting on the next element, if
danielebarchiesi@0 1419 // it's still appropriate to do so.
danielebarchiesi@0 1420 drupal_static_reset('form_set_error:limit_validation_errors');
danielebarchiesi@0 1421 }
danielebarchiesi@0 1422
danielebarchiesi@0 1423 /**
danielebarchiesi@0 1424 * Executes custom validation and submission handlers for a given form.
danielebarchiesi@0 1425 *
danielebarchiesi@0 1426 * Button-specific handlers are checked first. If none exist, the function
danielebarchiesi@0 1427 * falls back to form-level handlers.
danielebarchiesi@0 1428 *
danielebarchiesi@0 1429 * @param $type
danielebarchiesi@0 1430 * The type of handler to execute. 'validate' or 'submit' are the
danielebarchiesi@0 1431 * defaults used by Form API.
danielebarchiesi@0 1432 * @param $form
danielebarchiesi@0 1433 * An associative array containing the structure of the form.
danielebarchiesi@0 1434 * @param $form_state
danielebarchiesi@0 1435 * A keyed array containing the current state of the form. If the user
danielebarchiesi@0 1436 * submitted the form by clicking a button with custom handler functions
danielebarchiesi@0 1437 * defined, those handlers will be stored here.
danielebarchiesi@0 1438 */
danielebarchiesi@0 1439 function form_execute_handlers($type, &$form, &$form_state) {
danielebarchiesi@0 1440 $return = FALSE;
danielebarchiesi@0 1441 // If there was a button pressed, use its handlers.
danielebarchiesi@0 1442 if (isset($form_state[$type . '_handlers'])) {
danielebarchiesi@0 1443 $handlers = $form_state[$type . '_handlers'];
danielebarchiesi@0 1444 }
danielebarchiesi@0 1445 // Otherwise, check for a form-level handler.
danielebarchiesi@0 1446 elseif (isset($form['#' . $type])) {
danielebarchiesi@0 1447 $handlers = $form['#' . $type];
danielebarchiesi@0 1448 }
danielebarchiesi@0 1449 else {
danielebarchiesi@0 1450 $handlers = array();
danielebarchiesi@0 1451 }
danielebarchiesi@0 1452
danielebarchiesi@0 1453 foreach ($handlers as $function) {
danielebarchiesi@0 1454 // Check if a previous _submit handler has set a batch, but make sure we
danielebarchiesi@0 1455 // do not react to a batch that is already being processed (for instance
danielebarchiesi@0 1456 // if a batch operation performs a drupal_form_submit()).
danielebarchiesi@0 1457 if ($type == 'submit' && ($batch =& batch_get()) && !isset($batch['id'])) {
danielebarchiesi@0 1458 // Some previous submit handler has set a batch. To ensure correct
danielebarchiesi@0 1459 // execution order, store the call in a special 'control' batch set.
danielebarchiesi@0 1460 // See _batch_next_set().
danielebarchiesi@0 1461 $batch['sets'][] = array('form_submit' => $function);
danielebarchiesi@0 1462 $batch['has_form_submits'] = TRUE;
danielebarchiesi@0 1463 }
danielebarchiesi@0 1464 else {
danielebarchiesi@0 1465 $function($form, $form_state);
danielebarchiesi@0 1466 }
danielebarchiesi@0 1467 $return = TRUE;
danielebarchiesi@0 1468 }
danielebarchiesi@0 1469 return $return;
danielebarchiesi@0 1470 }
danielebarchiesi@0 1471
danielebarchiesi@0 1472 /**
danielebarchiesi@0 1473 * Files an error against a form element.
danielebarchiesi@0 1474 *
danielebarchiesi@0 1475 * When a validation error is detected, the validator calls form_set_error() to
danielebarchiesi@0 1476 * indicate which element needs to be changed and provide an error message. This
danielebarchiesi@0 1477 * causes the Form API to not execute the form submit handlers, and instead to
danielebarchiesi@0 1478 * re-display the form to the user with the corresponding elements rendered with
danielebarchiesi@0 1479 * an 'error' CSS class (shown as red by default).
danielebarchiesi@0 1480 *
danielebarchiesi@0 1481 * The standard form_set_error() behavior can be changed if a button provides
danielebarchiesi@0 1482 * the #limit_validation_errors property. Multistep forms not wanting to
danielebarchiesi@0 1483 * validate the whole form can set #limit_validation_errors on buttons to
danielebarchiesi@0 1484 * limit validation errors to only certain elements. For example, pressing the
danielebarchiesi@0 1485 * "Previous" button in a multistep form should not fire validation errors just
danielebarchiesi@0 1486 * because the current step has invalid values. If #limit_validation_errors is
danielebarchiesi@0 1487 * set on a clicked button, the button must also define a #submit property
danielebarchiesi@0 1488 * (may be set to an empty array). Any #submit handlers will be executed even if
danielebarchiesi@0 1489 * there is invalid input, so extreme care should be taken with respect to any
danielebarchiesi@0 1490 * actions taken by them. This is typically not a problem with buttons like
danielebarchiesi@0 1491 * "Previous" or "Add more" that do not invoke persistent storage of the
danielebarchiesi@0 1492 * submitted form values. Do not use the #limit_validation_errors property on
danielebarchiesi@0 1493 * buttons that trigger saving of form values to the database.
danielebarchiesi@0 1494 *
danielebarchiesi@0 1495 * The #limit_validation_errors property is a list of "sections" within
danielebarchiesi@0 1496 * $form_state['values'] that must contain valid values. Each "section" is an
danielebarchiesi@0 1497 * array with the ordered set of keys needed to reach that part of
danielebarchiesi@0 1498 * $form_state['values'] (i.e., the #parents property of the element).
danielebarchiesi@0 1499 *
danielebarchiesi@0 1500 * Example 1: Allow the "Previous" button to function, regardless of whether any
danielebarchiesi@0 1501 * user input is valid.
danielebarchiesi@0 1502 *
danielebarchiesi@0 1503 * @code
danielebarchiesi@0 1504 * $form['actions']['previous'] = array(
danielebarchiesi@0 1505 * '#type' => 'submit',
danielebarchiesi@0 1506 * '#value' => t('Previous'),
danielebarchiesi@0 1507 * '#limit_validation_errors' => array(), // No validation.
danielebarchiesi@0 1508 * '#submit' => array('some_submit_function'), // #submit required.
danielebarchiesi@0 1509 * );
danielebarchiesi@0 1510 * @endcode
danielebarchiesi@0 1511 *
danielebarchiesi@0 1512 * Example 2: Require some, but not all, user input to be valid to process the
danielebarchiesi@0 1513 * submission of a "Previous" button.
danielebarchiesi@0 1514 *
danielebarchiesi@0 1515 * @code
danielebarchiesi@0 1516 * $form['actions']['previous'] = array(
danielebarchiesi@0 1517 * '#type' => 'submit',
danielebarchiesi@0 1518 * '#value' => t('Previous'),
danielebarchiesi@0 1519 * '#limit_validation_errors' => array(
danielebarchiesi@0 1520 * array('step1'), // Validate $form_state['values']['step1'].
danielebarchiesi@0 1521 * array('foo', 'bar'), // Validate $form_state['values']['foo']['bar'].
danielebarchiesi@0 1522 * ),
danielebarchiesi@0 1523 * '#submit' => array('some_submit_function'), // #submit required.
danielebarchiesi@0 1524 * );
danielebarchiesi@0 1525 * @endcode
danielebarchiesi@0 1526 *
danielebarchiesi@0 1527 * This will require $form_state['values']['step1'] and everything within it
danielebarchiesi@0 1528 * (for example, $form_state['values']['step1']['choice']) to be valid, so
danielebarchiesi@0 1529 * calls to form_set_error('step1', $message) or
danielebarchiesi@0 1530 * form_set_error('step1][choice', $message) will prevent the submit handlers
danielebarchiesi@0 1531 * from running, and result in the error message being displayed to the user.
danielebarchiesi@0 1532 * However, calls to form_set_error('step2', $message) and
danielebarchiesi@0 1533 * form_set_error('step2][groupX][choiceY', $message) will be suppressed,
danielebarchiesi@0 1534 * resulting in the message not being displayed to the user, and the submit
danielebarchiesi@0 1535 * handlers will run despite $form_state['values']['step2'] and
danielebarchiesi@0 1536 * $form_state['values']['step2']['groupX']['choiceY'] containing invalid
danielebarchiesi@0 1537 * values. Errors for an invalid $form_state['values']['foo'] will be
danielebarchiesi@0 1538 * suppressed, but errors flagging invalid values for
danielebarchiesi@0 1539 * $form_state['values']['foo']['bar'] and everything within it will be
danielebarchiesi@0 1540 * flagged and submission prevented.
danielebarchiesi@0 1541 *
danielebarchiesi@0 1542 * Partial form validation is implemented by suppressing errors rather than by
danielebarchiesi@0 1543 * skipping the input processing and validation steps entirely, because some
danielebarchiesi@0 1544 * forms have button-level submit handlers that call Drupal API functions that
danielebarchiesi@0 1545 * assume that certain data exists within $form_state['values'], and while not
danielebarchiesi@0 1546 * doing anything with that data that requires it to be valid, PHP errors
danielebarchiesi@0 1547 * would be triggered if the input processing and validation steps were fully
danielebarchiesi@0 1548 * skipped.
danielebarchiesi@0 1549 *
danielebarchiesi@0 1550 * @param $name
danielebarchiesi@0 1551 * The name of the form element. If the #parents property of your form
danielebarchiesi@0 1552 * element is array('foo', 'bar', 'baz') then you may set an error on 'foo'
danielebarchiesi@0 1553 * or 'foo][bar][baz'. Setting an error on 'foo' sets an error for every
danielebarchiesi@0 1554 * element where the #parents array starts with 'foo'.
danielebarchiesi@0 1555 * @param $message
danielebarchiesi@0 1556 * The error message to present to the user.
danielebarchiesi@0 1557 * @param $limit_validation_errors
danielebarchiesi@0 1558 * Internal use only. The #limit_validation_errors property of the clicked
danielebarchiesi@0 1559 * button, if it exists.
danielebarchiesi@0 1560 *
danielebarchiesi@0 1561 * @return
danielebarchiesi@0 1562 * Return value is for internal use only. To get a list of errors, use
danielebarchiesi@0 1563 * form_get_errors() or form_get_error().
danielebarchiesi@0 1564 *
danielebarchiesi@0 1565 * @see http://drupal.org/node/370537
danielebarchiesi@0 1566 * @see http://drupal.org/node/763376
danielebarchiesi@0 1567 */
danielebarchiesi@0 1568 function form_set_error($name = NULL, $message = '', $limit_validation_errors = NULL) {
danielebarchiesi@0 1569 $form = &drupal_static(__FUNCTION__, array());
danielebarchiesi@0 1570 $sections = &drupal_static(__FUNCTION__ . ':limit_validation_errors');
danielebarchiesi@0 1571 if (isset($limit_validation_errors)) {
danielebarchiesi@0 1572 $sections = $limit_validation_errors;
danielebarchiesi@0 1573 }
danielebarchiesi@0 1574
danielebarchiesi@0 1575 if (isset($name) && !isset($form[$name])) {
danielebarchiesi@0 1576 $record = TRUE;
danielebarchiesi@0 1577 if (isset($sections)) {
danielebarchiesi@0 1578 // #limit_validation_errors is an array of "sections" within which user
danielebarchiesi@0 1579 // input must be valid. If the element is within one of these sections,
danielebarchiesi@0 1580 // the error must be recorded. Otherwise, it can be suppressed.
danielebarchiesi@0 1581 // #limit_validation_errors can be an empty array, in which case all
danielebarchiesi@0 1582 // errors are suppressed. For example, a "Previous" button might want its
danielebarchiesi@0 1583 // submit action to be triggered even if none of the submitted values are
danielebarchiesi@0 1584 // valid.
danielebarchiesi@0 1585 $record = FALSE;
danielebarchiesi@0 1586 foreach ($sections as $section) {
danielebarchiesi@0 1587 // Exploding by '][' reconstructs the element's #parents. If the
danielebarchiesi@0 1588 // reconstructed #parents begin with the same keys as the specified
danielebarchiesi@0 1589 // section, then the element's values are within the part of
danielebarchiesi@0 1590 // $form_state['values'] that the clicked button requires to be valid,
danielebarchiesi@0 1591 // so errors for this element must be recorded. As the exploded array
danielebarchiesi@0 1592 // will all be strings, we need to cast every value of the section
danielebarchiesi@0 1593 // array to string.
danielebarchiesi@0 1594 if (array_slice(explode('][', $name), 0, count($section)) === array_map('strval', $section)) {
danielebarchiesi@0 1595 $record = TRUE;
danielebarchiesi@0 1596 break;
danielebarchiesi@0 1597 }
danielebarchiesi@0 1598 }
danielebarchiesi@0 1599 }
danielebarchiesi@0 1600 if ($record) {
danielebarchiesi@0 1601 $form[$name] = $message;
danielebarchiesi@0 1602 if ($message) {
danielebarchiesi@0 1603 drupal_set_message($message, 'error');
danielebarchiesi@0 1604 }
danielebarchiesi@0 1605 }
danielebarchiesi@0 1606 }
danielebarchiesi@0 1607
danielebarchiesi@0 1608 return $form;
danielebarchiesi@0 1609 }
danielebarchiesi@0 1610
danielebarchiesi@0 1611 /**
danielebarchiesi@0 1612 * Clears all errors against all form elements made by form_set_error().
danielebarchiesi@0 1613 */
danielebarchiesi@0 1614 function form_clear_error() {
danielebarchiesi@0 1615 drupal_static_reset('form_set_error');
danielebarchiesi@0 1616 }
danielebarchiesi@0 1617
danielebarchiesi@0 1618 /**
danielebarchiesi@0 1619 * Returns an associative array of all errors.
danielebarchiesi@0 1620 */
danielebarchiesi@0 1621 function form_get_errors() {
danielebarchiesi@0 1622 $form = form_set_error();
danielebarchiesi@0 1623 if (!empty($form)) {
danielebarchiesi@0 1624 return $form;
danielebarchiesi@0 1625 }
danielebarchiesi@0 1626 }
danielebarchiesi@0 1627
danielebarchiesi@0 1628 /**
danielebarchiesi@0 1629 * Returns the error message filed against the given form element.
danielebarchiesi@0 1630 *
danielebarchiesi@0 1631 * Form errors higher up in the form structure override deeper errors as well as
danielebarchiesi@0 1632 * errors on the element itself.
danielebarchiesi@0 1633 */
danielebarchiesi@0 1634 function form_get_error($element) {
danielebarchiesi@0 1635 $form = form_set_error();
danielebarchiesi@0 1636 $parents = array();
danielebarchiesi@0 1637 foreach ($element['#parents'] as $parent) {
danielebarchiesi@0 1638 $parents[] = $parent;
danielebarchiesi@0 1639 $key = implode('][', $parents);
danielebarchiesi@0 1640 if (isset($form[$key])) {
danielebarchiesi@0 1641 return $form[$key];
danielebarchiesi@0 1642 }
danielebarchiesi@0 1643 }
danielebarchiesi@0 1644 }
danielebarchiesi@0 1645
danielebarchiesi@0 1646 /**
danielebarchiesi@0 1647 * Flags an element as having an error.
danielebarchiesi@0 1648 */
danielebarchiesi@0 1649 function form_error(&$element, $message = '') {
danielebarchiesi@0 1650 form_set_error(implode('][', $element['#parents']), $message);
danielebarchiesi@0 1651 }
danielebarchiesi@0 1652
danielebarchiesi@0 1653 /**
danielebarchiesi@0 1654 * Builds and processes all elements in the structured form array.
danielebarchiesi@0 1655 *
danielebarchiesi@0 1656 * Adds any required properties to each element, maps the incoming input data
danielebarchiesi@0 1657 * to the proper elements, and executes any #process handlers attached to a
danielebarchiesi@0 1658 * specific element.
danielebarchiesi@0 1659 *
danielebarchiesi@0 1660 * This is one of the three primary functions that recursively iterates a form
danielebarchiesi@0 1661 * array. This one does it for completing the form building process. The other
danielebarchiesi@0 1662 * two are _form_validate() (invoked via drupal_validate_form() and used to
danielebarchiesi@0 1663 * invoke validation logic for each element) and drupal_render() (for rendering
danielebarchiesi@0 1664 * each element). Each of these three pipelines provides ample opportunity for
danielebarchiesi@0 1665 * modules to customize what happens. For example, during this function's life
danielebarchiesi@0 1666 * cycle, the following functions get called for each element:
danielebarchiesi@0 1667 * - $element['#value_callback']: A function that implements how user input is
danielebarchiesi@0 1668 * mapped to an element's #value property. This defaults to a function named
danielebarchiesi@0 1669 * 'form_type_TYPE_value' where TYPE is $element['#type'].
danielebarchiesi@0 1670 * - $element['#process']: An array of functions called after user input has
danielebarchiesi@0 1671 * been mapped to the element's #value property. These functions can be used
danielebarchiesi@0 1672 * to dynamically add child elements: for example, for the 'date' element
danielebarchiesi@0 1673 * type, one of the functions in this array is form_process_date(), which adds
danielebarchiesi@0 1674 * the individual 'year', 'month', 'day', etc. child elements. These functions
danielebarchiesi@0 1675 * can also be used to set additional properties or implement special logic
danielebarchiesi@0 1676 * other than adding child elements: for example, for the 'fieldset' element
danielebarchiesi@0 1677 * type, one of the functions in this array is form_process_fieldset(), which
danielebarchiesi@0 1678 * adds the attributes and JavaScript needed to make the fieldset collapsible
danielebarchiesi@0 1679 * if the #collapsible property is set. The #process functions are called in
danielebarchiesi@0 1680 * preorder traversal, meaning they are called for the parent element first,
danielebarchiesi@0 1681 * then for the child elements.
danielebarchiesi@0 1682 * - $element['#after_build']: An array of functions called after form_builder()
danielebarchiesi@0 1683 * is done with its processing of the element. These are called in postorder
danielebarchiesi@0 1684 * traversal, meaning they are called for the child elements first, then for
danielebarchiesi@0 1685 * the parent element.
danielebarchiesi@0 1686 * There are similar properties containing callback functions invoked by
danielebarchiesi@0 1687 * _form_validate() and drupal_render(), appropriate for those operations.
danielebarchiesi@0 1688 *
danielebarchiesi@0 1689 * Developers are strongly encouraged to integrate the functionality needed by
danielebarchiesi@0 1690 * their form or module within one of these three pipelines, using the
danielebarchiesi@0 1691 * appropriate callback property, rather than implementing their own recursive
danielebarchiesi@0 1692 * traversal of a form array. This facilitates proper integration between
danielebarchiesi@0 1693 * multiple modules. For example, module developers are familiar with the
danielebarchiesi@0 1694 * relative order in which hook_form_alter() implementations and #process
danielebarchiesi@0 1695 * functions run. A custom traversal function that affects the building of a
danielebarchiesi@0 1696 * form is likely to not integrate with hook_form_alter() and #process in the
danielebarchiesi@0 1697 * expected way. Also, deep recursion within PHP is both slow and memory
danielebarchiesi@0 1698 * intensive, so it is best to minimize how often it's done.
danielebarchiesi@0 1699 *
danielebarchiesi@0 1700 * As stated above, each element's #process functions are executed after its
danielebarchiesi@0 1701 * #value has been set. This enables those functions to execute conditional
danielebarchiesi@0 1702 * logic based on the current value. However, all of form_builder() runs before
danielebarchiesi@0 1703 * drupal_validate_form() is called, so during #process function execution, the
danielebarchiesi@0 1704 * element's #value has not yet been validated, so any code that requires
danielebarchiesi@0 1705 * validated values must reside within a submit handler.
danielebarchiesi@0 1706 *
danielebarchiesi@0 1707 * As a security measure, user input is used for an element's #value only if the
danielebarchiesi@0 1708 * element exists within $form, is not disabled (as per the #disabled property),
danielebarchiesi@0 1709 * and can be accessed (as per the #access property, except that forms submitted
danielebarchiesi@0 1710 * using drupal_form_submit() bypass #access restrictions). When user input is
danielebarchiesi@0 1711 * ignored due to #disabled and #access restrictions, the element's default
danielebarchiesi@0 1712 * value is used.
danielebarchiesi@0 1713 *
danielebarchiesi@0 1714 * Because of the preorder traversal, where #process functions of an element run
danielebarchiesi@0 1715 * before user input for its child elements is processed, and because of the
danielebarchiesi@0 1716 * Form API security of user input processing with respect to #access and
danielebarchiesi@0 1717 * #disabled described above, this generally means that #process functions
danielebarchiesi@0 1718 * should not use an element's (unvalidated) #value to affect the #disabled or
danielebarchiesi@0 1719 * #access of child elements. Use-cases where a developer may be tempted to
danielebarchiesi@0 1720 * implement such conditional logic usually fall into one of two categories:
danielebarchiesi@0 1721 * - Where user input from the current submission must affect the structure of a
danielebarchiesi@0 1722 * form, including properties like #access and #disabled that affect how the
danielebarchiesi@0 1723 * next submission needs to be processed, a multi-step workflow is needed.
danielebarchiesi@0 1724 * This is most commonly implemented with a submit handler setting persistent
danielebarchiesi@0 1725 * data within $form_state based on *validated* values in
danielebarchiesi@0 1726 * $form_state['values'] and setting $form_state['rebuild']. The form building
danielebarchiesi@0 1727 * functions must then be implemented to use the $form_state data to rebuild
danielebarchiesi@0 1728 * the form with the structure appropriate for the new state.
danielebarchiesi@0 1729 * - Where user input must affect the rendering of the form without affecting
danielebarchiesi@0 1730 * its structure, the necessary conditional rendering logic should reside
danielebarchiesi@0 1731 * within functions that run during the rendering phase (#pre_render, #theme,
danielebarchiesi@0 1732 * #theme_wrappers, and #post_render).
danielebarchiesi@0 1733 *
danielebarchiesi@0 1734 * @param $form_id
danielebarchiesi@0 1735 * A unique string identifying the form for validation, submission,
danielebarchiesi@0 1736 * theming, and hook_form_alter functions.
danielebarchiesi@0 1737 * @param $element
danielebarchiesi@0 1738 * An associative array containing the structure of the current element.
danielebarchiesi@0 1739 * @param $form_state
danielebarchiesi@0 1740 * A keyed array containing the current state of the form. In this
danielebarchiesi@0 1741 * context, it is used to accumulate information about which button
danielebarchiesi@0 1742 * was clicked when the form was submitted, as well as the sanitized
danielebarchiesi@0 1743 * $_POST data.
danielebarchiesi@0 1744 */
danielebarchiesi@0 1745 function form_builder($form_id, &$element, &$form_state) {
danielebarchiesi@0 1746 // Initialize as unprocessed.
danielebarchiesi@0 1747 $element['#processed'] = FALSE;
danielebarchiesi@0 1748
danielebarchiesi@0 1749 // Use element defaults.
danielebarchiesi@0 1750 if (isset($element['#type']) && empty($element['#defaults_loaded']) && ($info = element_info($element['#type']))) {
danielebarchiesi@0 1751 // Overlay $info onto $element, retaining preexisting keys in $element.
danielebarchiesi@0 1752 $element += $info;
danielebarchiesi@0 1753 $element['#defaults_loaded'] = TRUE;
danielebarchiesi@0 1754 }
danielebarchiesi@0 1755 // Assign basic defaults common for all form elements.
danielebarchiesi@0 1756 $element += array(
danielebarchiesi@0 1757 '#required' => FALSE,
danielebarchiesi@0 1758 '#attributes' => array(),
danielebarchiesi@0 1759 '#title_display' => 'before',
danielebarchiesi@0 1760 );
danielebarchiesi@0 1761
danielebarchiesi@0 1762 // Special handling if we're on the top level form element.
danielebarchiesi@0 1763 if (isset($element['#type']) && $element['#type'] == 'form') {
danielebarchiesi@0 1764 if (!empty($element['#https']) && variable_get('https', FALSE) &&
danielebarchiesi@0 1765 !url_is_external($element['#action'])) {
danielebarchiesi@0 1766 global $base_root;
danielebarchiesi@0 1767
danielebarchiesi@0 1768 // Not an external URL so ensure that it is secure.
danielebarchiesi@0 1769 $element['#action'] = str_replace('http://', 'https://', $base_root) . $element['#action'];
danielebarchiesi@0 1770 }
danielebarchiesi@0 1771
danielebarchiesi@0 1772 // Store a reference to the complete form in $form_state prior to building
danielebarchiesi@0 1773 // the form. This allows advanced #process and #after_build callbacks to
danielebarchiesi@0 1774 // perform changes elsewhere in the form.
danielebarchiesi@0 1775 $form_state['complete form'] = &$element;
danielebarchiesi@0 1776
danielebarchiesi@0 1777 // Set a flag if we have a correct form submission. This is always TRUE for
danielebarchiesi@0 1778 // programmed forms coming from drupal_form_submit(), or if the form_id coming
danielebarchiesi@0 1779 // from the POST data is set and matches the current form_id.
danielebarchiesi@0 1780 if ($form_state['programmed'] || (!empty($form_state['input']) && (isset($form_state['input']['form_id']) && ($form_state['input']['form_id'] == $form_id)))) {
danielebarchiesi@0 1781 $form_state['process_input'] = TRUE;
danielebarchiesi@0 1782 }
danielebarchiesi@0 1783 else {
danielebarchiesi@0 1784 $form_state['process_input'] = FALSE;
danielebarchiesi@0 1785 }
danielebarchiesi@0 1786
danielebarchiesi@0 1787 // All form elements should have an #array_parents property.
danielebarchiesi@0 1788 $element['#array_parents'] = array();
danielebarchiesi@0 1789 }
danielebarchiesi@0 1790
danielebarchiesi@0 1791 if (!isset($element['#id'])) {
danielebarchiesi@0 1792 $element['#id'] = drupal_html_id('edit-' . implode('-', $element['#parents']));
danielebarchiesi@0 1793 }
danielebarchiesi@0 1794 // Handle input elements.
danielebarchiesi@0 1795 if (!empty($element['#input'])) {
danielebarchiesi@0 1796 _form_builder_handle_input_element($form_id, $element, $form_state);
danielebarchiesi@0 1797 }
danielebarchiesi@0 1798 // Allow for elements to expand to multiple elements, e.g., radios,
danielebarchiesi@0 1799 // checkboxes and files.
danielebarchiesi@0 1800 if (isset($element['#process']) && !$element['#processed']) {
danielebarchiesi@0 1801 foreach ($element['#process'] as $process) {
danielebarchiesi@0 1802 $element = $process($element, $form_state, $form_state['complete form']);
danielebarchiesi@0 1803 }
danielebarchiesi@0 1804 $element['#processed'] = TRUE;
danielebarchiesi@0 1805 }
danielebarchiesi@0 1806
danielebarchiesi@0 1807 // We start off assuming all form elements are in the correct order.
danielebarchiesi@0 1808 $element['#sorted'] = TRUE;
danielebarchiesi@0 1809
danielebarchiesi@0 1810 // Recurse through all child elements.
danielebarchiesi@0 1811 $count = 0;
danielebarchiesi@0 1812 foreach (element_children($element) as $key) {
danielebarchiesi@0 1813 // Prior to checking properties of child elements, their default properties
danielebarchiesi@0 1814 // need to be loaded.
danielebarchiesi@0 1815 if (isset($element[$key]['#type']) && empty($element[$key]['#defaults_loaded']) && ($info = element_info($element[$key]['#type']))) {
danielebarchiesi@0 1816 $element[$key] += $info;
danielebarchiesi@0 1817 $element[$key]['#defaults_loaded'] = TRUE;
danielebarchiesi@0 1818 }
danielebarchiesi@0 1819
danielebarchiesi@0 1820 // Don't squash an existing tree value.
danielebarchiesi@0 1821 if (!isset($element[$key]['#tree'])) {
danielebarchiesi@0 1822 $element[$key]['#tree'] = $element['#tree'];
danielebarchiesi@0 1823 }
danielebarchiesi@0 1824
danielebarchiesi@0 1825 // Deny access to child elements if parent is denied.
danielebarchiesi@0 1826 if (isset($element['#access']) && !$element['#access']) {
danielebarchiesi@0 1827 $element[$key]['#access'] = FALSE;
danielebarchiesi@0 1828 }
danielebarchiesi@0 1829
danielebarchiesi@0 1830 // Make child elements inherit their parent's #disabled and #allow_focus
danielebarchiesi@0 1831 // values unless they specify their own.
danielebarchiesi@0 1832 foreach (array('#disabled', '#allow_focus') as $property) {
danielebarchiesi@0 1833 if (isset($element[$property]) && !isset($element[$key][$property])) {
danielebarchiesi@0 1834 $element[$key][$property] = $element[$property];
danielebarchiesi@0 1835 }
danielebarchiesi@0 1836 }
danielebarchiesi@0 1837
danielebarchiesi@0 1838 // Don't squash existing parents value.
danielebarchiesi@0 1839 if (!isset($element[$key]['#parents'])) {
danielebarchiesi@0 1840 // Check to see if a tree of child elements is present. If so,
danielebarchiesi@0 1841 // continue down the tree if required.
danielebarchiesi@0 1842 $element[$key]['#parents'] = $element[$key]['#tree'] && $element['#tree'] ? array_merge($element['#parents'], array($key)) : array($key);
danielebarchiesi@0 1843 }
danielebarchiesi@0 1844 // Ensure #array_parents follows the actual form structure.
danielebarchiesi@0 1845 $array_parents = $element['#array_parents'];
danielebarchiesi@0 1846 $array_parents[] = $key;
danielebarchiesi@0 1847 $element[$key]['#array_parents'] = $array_parents;
danielebarchiesi@0 1848
danielebarchiesi@0 1849 // Assign a decimal placeholder weight to preserve original array order.
danielebarchiesi@0 1850 if (!isset($element[$key]['#weight'])) {
danielebarchiesi@0 1851 $element[$key]['#weight'] = $count/1000;
danielebarchiesi@0 1852 }
danielebarchiesi@0 1853 else {
danielebarchiesi@0 1854 // If one of the child elements has a weight then we will need to sort
danielebarchiesi@0 1855 // later.
danielebarchiesi@0 1856 unset($element['#sorted']);
danielebarchiesi@0 1857 }
danielebarchiesi@0 1858 $element[$key] = form_builder($form_id, $element[$key], $form_state);
danielebarchiesi@0 1859 $count++;
danielebarchiesi@0 1860 }
danielebarchiesi@0 1861
danielebarchiesi@0 1862 // The #after_build flag allows any piece of a form to be altered
danielebarchiesi@0 1863 // after normal input parsing has been completed.
danielebarchiesi@0 1864 if (isset($element['#after_build']) && !isset($element['#after_build_done'])) {
danielebarchiesi@0 1865 foreach ($element['#after_build'] as $function) {
danielebarchiesi@0 1866 $element = $function($element, $form_state);
danielebarchiesi@0 1867 }
danielebarchiesi@0 1868 $element['#after_build_done'] = TRUE;
danielebarchiesi@0 1869 }
danielebarchiesi@0 1870
danielebarchiesi@0 1871 // If there is a file element, we need to flip a flag so later the
danielebarchiesi@0 1872 // form encoding can be set.
danielebarchiesi@0 1873 if (isset($element['#type']) && $element['#type'] == 'file') {
danielebarchiesi@0 1874 $form_state['has_file_element'] = TRUE;
danielebarchiesi@0 1875 }
danielebarchiesi@0 1876
danielebarchiesi@0 1877 // Final tasks for the form element after form_builder() has run for all other
danielebarchiesi@0 1878 // elements.
danielebarchiesi@0 1879 if (isset($element['#type']) && $element['#type'] == 'form') {
danielebarchiesi@0 1880 // If there is a file element, we set the form encoding.
danielebarchiesi@0 1881 if (isset($form_state['has_file_element'])) {
danielebarchiesi@0 1882 $element['#attributes']['enctype'] = 'multipart/form-data';
danielebarchiesi@0 1883 }
danielebarchiesi@0 1884
danielebarchiesi@0 1885 // If a form contains a single textfield, and the ENTER key is pressed
danielebarchiesi@0 1886 // within it, Internet Explorer submits the form with no POST data
danielebarchiesi@0 1887 // identifying any submit button. Other browsers submit POST data as though
danielebarchiesi@0 1888 // the user clicked the first button. Therefore, to be as consistent as we
danielebarchiesi@0 1889 // can be across browsers, if no 'triggering_element' has been identified
danielebarchiesi@0 1890 // yet, default it to the first button.
danielebarchiesi@0 1891 if (!$form_state['programmed'] && !isset($form_state['triggering_element']) && !empty($form_state['buttons'])) {
danielebarchiesi@0 1892 $form_state['triggering_element'] = $form_state['buttons'][0];
danielebarchiesi@0 1893 }
danielebarchiesi@0 1894
danielebarchiesi@0 1895 // If the triggering element specifies "button-level" validation and submit
danielebarchiesi@0 1896 // handlers to run instead of the default form-level ones, then add those to
danielebarchiesi@0 1897 // the form state.
danielebarchiesi@0 1898 foreach (array('validate', 'submit') as $type) {
danielebarchiesi@0 1899 if (isset($form_state['triggering_element']['#' . $type])) {
danielebarchiesi@0 1900 $form_state[$type . '_handlers'] = $form_state['triggering_element']['#' . $type];
danielebarchiesi@0 1901 }
danielebarchiesi@0 1902 }
danielebarchiesi@0 1903
danielebarchiesi@0 1904 // If the triggering element executes submit handlers, then set the form
danielebarchiesi@0 1905 // state key that's needed for those handlers to run.
danielebarchiesi@0 1906 if (!empty($form_state['triggering_element']['#executes_submit_callback'])) {
danielebarchiesi@0 1907 $form_state['submitted'] = TRUE;
danielebarchiesi@0 1908 }
danielebarchiesi@0 1909
danielebarchiesi@0 1910 // Special processing if the triggering element is a button.
danielebarchiesi@0 1911 if (isset($form_state['triggering_element']['#button_type'])) {
danielebarchiesi@0 1912 // Because there are several ways in which the triggering element could
danielebarchiesi@0 1913 // have been determined (including from input variables set by JavaScript
danielebarchiesi@0 1914 // or fallback behavior implemented for IE), and because buttons often
danielebarchiesi@0 1915 // have their #name property not derived from their #parents property, we
danielebarchiesi@0 1916 // can't assume that input processing that's happened up until here has
danielebarchiesi@0 1917 // resulted in $form_state['values'][BUTTON_NAME] being set. But it's
danielebarchiesi@0 1918 // common for forms to have several buttons named 'op' and switch on
danielebarchiesi@0 1919 // $form_state['values']['op'] during submit handler execution.
danielebarchiesi@0 1920 $form_state['values'][$form_state['triggering_element']['#name']] = $form_state['triggering_element']['#value'];
danielebarchiesi@0 1921
danielebarchiesi@0 1922 // @todo Legacy support. Remove in Drupal 8.
danielebarchiesi@0 1923 $form_state['clicked_button'] = $form_state['triggering_element'];
danielebarchiesi@0 1924 }
danielebarchiesi@0 1925 }
danielebarchiesi@0 1926 return $element;
danielebarchiesi@0 1927 }
danielebarchiesi@0 1928
danielebarchiesi@0 1929 /**
danielebarchiesi@0 1930 * Adds the #name and #value properties of an input element before rendering.
danielebarchiesi@0 1931 */
danielebarchiesi@0 1932 function _form_builder_handle_input_element($form_id, &$element, &$form_state) {
danielebarchiesi@0 1933 if (!isset($element['#name'])) {
danielebarchiesi@0 1934 $name = array_shift($element['#parents']);
danielebarchiesi@0 1935 $element['#name'] = $name;
danielebarchiesi@0 1936 if ($element['#type'] == 'file') {
danielebarchiesi@0 1937 // To make it easier to handle $_FILES in file.inc, we place all
danielebarchiesi@0 1938 // file fields in the 'files' array. Also, we do not support
danielebarchiesi@0 1939 // nested file names.
danielebarchiesi@0 1940 $element['#name'] = 'files[' . $element['#name'] . ']';
danielebarchiesi@0 1941 }
danielebarchiesi@0 1942 elseif (count($element['#parents'])) {
danielebarchiesi@0 1943 $element['#name'] .= '[' . implode('][', $element['#parents']) . ']';
danielebarchiesi@0 1944 }
danielebarchiesi@0 1945 array_unshift($element['#parents'], $name);
danielebarchiesi@0 1946 }
danielebarchiesi@0 1947
danielebarchiesi@0 1948 // Setting #disabled to TRUE results in user input being ignored, regardless
danielebarchiesi@0 1949 // of how the element is themed or whether JavaScript is used to change the
danielebarchiesi@0 1950 // control's attributes. However, it's good UI to let the user know that input
danielebarchiesi@0 1951 // is not wanted for the control. HTML supports two attributes for this:
danielebarchiesi@0 1952 // http://www.w3.org/TR/html401/interact/forms.html#h-17.12. If a form wants
danielebarchiesi@0 1953 // to start a control off with one of these attributes for UI purposes only,
danielebarchiesi@0 1954 // but still allow input to be processed if it's sumitted, it can set the
danielebarchiesi@0 1955 // desired attribute in #attributes directly rather than using #disabled.
danielebarchiesi@0 1956 // However, developers should think carefully about the accessibility
danielebarchiesi@0 1957 // implications of doing so: if the form expects input to be enterable under
danielebarchiesi@0 1958 // some condition triggered by JavaScript, how would someone who has
danielebarchiesi@0 1959 // JavaScript disabled trigger that condition? Instead, developers should
danielebarchiesi@0 1960 // consider whether a multi-step form would be more appropriate (#disabled can
danielebarchiesi@0 1961 // be changed from step to step). If one still decides to use JavaScript to
danielebarchiesi@0 1962 // affect when a control is enabled, then it is best for accessibility for the
danielebarchiesi@0 1963 // control to be enabled in the HTML, and disabled by JavaScript on document
danielebarchiesi@0 1964 // ready.
danielebarchiesi@0 1965 if (!empty($element['#disabled'])) {
danielebarchiesi@0 1966 if (!empty($element['#allow_focus'])) {
danielebarchiesi@0 1967 $element['#attributes']['readonly'] = 'readonly';
danielebarchiesi@0 1968 }
danielebarchiesi@0 1969 else {
danielebarchiesi@0 1970 $element['#attributes']['disabled'] = 'disabled';
danielebarchiesi@0 1971 }
danielebarchiesi@0 1972 }
danielebarchiesi@0 1973
danielebarchiesi@0 1974 // With JavaScript or other easy hacking, input can be submitted even for
danielebarchiesi@0 1975 // elements with #access=FALSE or #disabled=TRUE. For security, these must
danielebarchiesi@0 1976 // not be processed. Forms that set #disabled=TRUE on an element do not
danielebarchiesi@0 1977 // expect input for the element, and even forms submitted with
danielebarchiesi@0 1978 // drupal_form_submit() must not be able to get around this. Forms that set
danielebarchiesi@0 1979 // #access=FALSE on an element usually allow access for some users, so forms
danielebarchiesi@0 1980 // submitted with drupal_form_submit() may bypass access restriction and be
danielebarchiesi@0 1981 // treated as high-privilege users instead.
danielebarchiesi@0 1982 $process_input = empty($element['#disabled']) && ($form_state['programmed'] || ($form_state['process_input'] && (!isset($element['#access']) || $element['#access'])));
danielebarchiesi@0 1983
danielebarchiesi@0 1984 // Set the element's #value property.
danielebarchiesi@0 1985 if (!isset($element['#value']) && !array_key_exists('#value', $element)) {
danielebarchiesi@0 1986 $value_callback = !empty($element['#value_callback']) ? $element['#value_callback'] : 'form_type_' . $element['#type'] . '_value';
danielebarchiesi@0 1987 if ($process_input) {
danielebarchiesi@0 1988 // Get the input for the current element. NULL values in the input need to
danielebarchiesi@0 1989 // be explicitly distinguished from missing input. (see below)
danielebarchiesi@0 1990 $input_exists = NULL;
danielebarchiesi@0 1991 $input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists);
danielebarchiesi@0 1992 // For browser-submitted forms, the submitted values do not contain values
danielebarchiesi@0 1993 // for certain elements (empty multiple select, unchecked checkbox).
danielebarchiesi@0 1994 // During initial form processing, we add explicit NULL values for such
danielebarchiesi@0 1995 // elements in $form_state['input']. When rebuilding the form, we can
danielebarchiesi@0 1996 // distinguish elements having NULL input from elements that were not part
danielebarchiesi@0 1997 // of the initially submitted form and can therefore use default values
danielebarchiesi@0 1998 // for the latter, if required. Programmatically submitted forms can
danielebarchiesi@0 1999 // submit explicit NULL values when calling drupal_form_submit(), so we do
danielebarchiesi@0 2000 // not modify $form_state['input'] for them.
danielebarchiesi@0 2001 if (!$input_exists && !$form_state['rebuild'] && !$form_state['programmed']) {
danielebarchiesi@0 2002 // Add the necessary parent keys to $form_state['input'] and sets the
danielebarchiesi@0 2003 // element's input value to NULL.
danielebarchiesi@0 2004 drupal_array_set_nested_value($form_state['input'], $element['#parents'], NULL);
danielebarchiesi@0 2005 $input_exists = TRUE;
danielebarchiesi@0 2006 }
danielebarchiesi@0 2007 // If we have input for the current element, assign it to the #value
danielebarchiesi@0 2008 // property, optionally filtered through $value_callback.
danielebarchiesi@0 2009 if ($input_exists) {
danielebarchiesi@0 2010 if (function_exists($value_callback)) {
danielebarchiesi@0 2011 $element['#value'] = $value_callback($element, $input, $form_state);
danielebarchiesi@0 2012 }
danielebarchiesi@0 2013 if (!isset($element['#value']) && isset($input)) {
danielebarchiesi@0 2014 $element['#value'] = $input;
danielebarchiesi@0 2015 }
danielebarchiesi@0 2016 }
danielebarchiesi@0 2017 // Mark all posted values for validation.
danielebarchiesi@0 2018 if (isset($element['#value']) || (!empty($element['#required']))) {
danielebarchiesi@0 2019 $element['#needs_validation'] = TRUE;
danielebarchiesi@0 2020 }
danielebarchiesi@0 2021 }
danielebarchiesi@0 2022 // Load defaults.
danielebarchiesi@0 2023 if (!isset($element['#value'])) {
danielebarchiesi@0 2024 // Call #type_value without a second argument to request default_value handling.
danielebarchiesi@0 2025 if (function_exists($value_callback)) {
danielebarchiesi@0 2026 $element['#value'] = $value_callback($element, FALSE, $form_state);
danielebarchiesi@0 2027 }
danielebarchiesi@0 2028 // Final catch. If we haven't set a value yet, use the explicit default value.
danielebarchiesi@0 2029 // Avoid image buttons (which come with garbage value), so we only get value
danielebarchiesi@0 2030 // for the button actually clicked.
danielebarchiesi@0 2031 if (!isset($element['#value']) && empty($element['#has_garbage_value'])) {
danielebarchiesi@0 2032 $element['#value'] = isset($element['#default_value']) ? $element['#default_value'] : '';
danielebarchiesi@0 2033 }
danielebarchiesi@0 2034 }
danielebarchiesi@0 2035 }
danielebarchiesi@0 2036
danielebarchiesi@0 2037 // Determine which element (if any) triggered the submission of the form and
danielebarchiesi@0 2038 // keep track of all the clickable buttons in the form for
danielebarchiesi@0 2039 // form_state_values_clean(). Enforce the same input processing restrictions
danielebarchiesi@0 2040 // as above.
danielebarchiesi@0 2041 if ($process_input) {
danielebarchiesi@0 2042 // Detect if the element triggered the submission via Ajax.
danielebarchiesi@0 2043 if (_form_element_triggered_scripted_submission($element, $form_state)) {
danielebarchiesi@0 2044 $form_state['triggering_element'] = $element;
danielebarchiesi@0 2045 }
danielebarchiesi@0 2046
danielebarchiesi@0 2047 // If the form was submitted by the browser rather than via Ajax, then it
danielebarchiesi@0 2048 // can only have been triggered by a button, and we need to determine which
danielebarchiesi@0 2049 // button within the constraints of how browsers provide this information.
danielebarchiesi@0 2050 if (isset($element['#button_type'])) {
danielebarchiesi@0 2051 // All buttons in the form need to be tracked for
danielebarchiesi@0 2052 // form_state_values_clean() and for the form_builder() code that handles
danielebarchiesi@0 2053 // a form submission containing no button information in $_POST.
danielebarchiesi@0 2054 $form_state['buttons'][] = $element;
danielebarchiesi@0 2055 if (_form_button_was_clicked($element, $form_state)) {
danielebarchiesi@0 2056 $form_state['triggering_element'] = $element;
danielebarchiesi@0 2057 }
danielebarchiesi@0 2058 }
danielebarchiesi@0 2059 }
danielebarchiesi@0 2060
danielebarchiesi@0 2061 // Set the element's value in $form_state['values'], but only, if its key
danielebarchiesi@0 2062 // does not exist yet (a #value_callback may have already populated it).
danielebarchiesi@0 2063 if (!drupal_array_nested_key_exists($form_state['values'], $element['#parents'])) {
danielebarchiesi@0 2064 form_set_value($element, $element['#value'], $form_state);
danielebarchiesi@0 2065 }
danielebarchiesi@0 2066 }
danielebarchiesi@0 2067
danielebarchiesi@0 2068 /**
danielebarchiesi@0 2069 * Detects if an element triggered the form submission via Ajax.
danielebarchiesi@0 2070 *
danielebarchiesi@0 2071 * This detects button or non-button controls that trigger a form submission via
danielebarchiesi@0 2072 * Ajax or some other scriptable environment. These environments can set the
danielebarchiesi@0 2073 * special input key '_triggering_element_name' to identify the triggering
danielebarchiesi@0 2074 * element. If the name alone doesn't identify the element uniquely, the input
danielebarchiesi@0 2075 * key '_triggering_element_value' may also be set to require a match on element
danielebarchiesi@0 2076 * value. An example where this is needed is if there are several buttons all
danielebarchiesi@0 2077 * named 'op', and only differing in their value.
danielebarchiesi@0 2078 */
danielebarchiesi@0 2079 function _form_element_triggered_scripted_submission($element, &$form_state) {
danielebarchiesi@0 2080 if (!empty($form_state['input']['_triggering_element_name']) && $element['#name'] == $form_state['input']['_triggering_element_name']) {
danielebarchiesi@0 2081 if (empty($form_state['input']['_triggering_element_value']) || $form_state['input']['_triggering_element_value'] == $element['#value']) {
danielebarchiesi@0 2082 return TRUE;
danielebarchiesi@0 2083 }
danielebarchiesi@0 2084 }
danielebarchiesi@0 2085 return FALSE;
danielebarchiesi@0 2086 }
danielebarchiesi@0 2087
danielebarchiesi@0 2088 /**
danielebarchiesi@0 2089 * Determines if a given button triggered the form submission.
danielebarchiesi@0 2090 *
danielebarchiesi@0 2091 * This detects button controls that trigger a form submission by being clicked
danielebarchiesi@0 2092 * and having the click processed by the browser rather than being captured by
danielebarchiesi@0 2093 * JavaScript. Essentially, it detects if the button's name and value are part
danielebarchiesi@0 2094 * of the POST data, but with extra code to deal with the convoluted way in
danielebarchiesi@0 2095 * which browsers submit data for image button clicks.
danielebarchiesi@0 2096 *
danielebarchiesi@0 2097 * This does not detect button clicks processed by Ajax (that is done in
danielebarchiesi@0 2098 * _form_element_triggered_scripted_submission()) and it does not detect form
danielebarchiesi@0 2099 * submissions from Internet Explorer in response to an ENTER key pressed in a
danielebarchiesi@0 2100 * textfield (form_builder() has extra code for that).
danielebarchiesi@0 2101 *
danielebarchiesi@0 2102 * Because this function contains only part of the logic needed to determine
danielebarchiesi@0 2103 * $form_state['triggering_element'], it should not be called from anywhere
danielebarchiesi@0 2104 * other than within the Form API. Form validation and submit handlers needing
danielebarchiesi@0 2105 * to know which button was clicked should get that information from
danielebarchiesi@0 2106 * $form_state['triggering_element'].
danielebarchiesi@0 2107 */
danielebarchiesi@0 2108 function _form_button_was_clicked($element, &$form_state) {
danielebarchiesi@0 2109 // First detect normal 'vanilla' button clicks. Traditionally, all
danielebarchiesi@0 2110 // standard buttons on a form share the same name (usually 'op'),
danielebarchiesi@0 2111 // and the specific return value is used to determine which was
danielebarchiesi@0 2112 // clicked. This ONLY works as long as $form['#name'] puts the
danielebarchiesi@0 2113 // value at the top level of the tree of $_POST data.
danielebarchiesi@0 2114 if (isset($form_state['input'][$element['#name']]) && $form_state['input'][$element['#name']] == $element['#value']) {
danielebarchiesi@0 2115 return TRUE;
danielebarchiesi@0 2116 }
danielebarchiesi@0 2117 // When image buttons are clicked, browsers do NOT pass the form element
danielebarchiesi@0 2118 // value in $_POST. Instead they pass an integer representing the
danielebarchiesi@0 2119 // coordinates of the click on the button image. This means that image
danielebarchiesi@0 2120 // buttons MUST have unique $form['#name'] values, but the details of
danielebarchiesi@0 2121 // their $_POST data should be ignored.
danielebarchiesi@0 2122 elseif (!empty($element['#has_garbage_value']) && isset($element['#value']) && $element['#value'] !== '') {
danielebarchiesi@0 2123 return TRUE;
danielebarchiesi@0 2124 }
danielebarchiesi@0 2125 return FALSE;
danielebarchiesi@0 2126 }
danielebarchiesi@0 2127
danielebarchiesi@0 2128 /**
danielebarchiesi@0 2129 * Removes internal Form API elements and buttons from submitted form values.
danielebarchiesi@0 2130 *
danielebarchiesi@0 2131 * This function can be used when a module wants to store all submitted form
danielebarchiesi@0 2132 * values, for example, by serializing them into a single database column. In
danielebarchiesi@0 2133 * such cases, all internal Form API values and all form button elements should
danielebarchiesi@0 2134 * not be contained, and this function allows to remove them before the module
danielebarchiesi@0 2135 * proceeds to storage. Next to button elements, the following internal values
danielebarchiesi@0 2136 * are removed:
danielebarchiesi@0 2137 * - form_id
danielebarchiesi@0 2138 * - form_token
danielebarchiesi@0 2139 * - form_build_id
danielebarchiesi@0 2140 * - op
danielebarchiesi@0 2141 *
danielebarchiesi@0 2142 * @param $form_state
danielebarchiesi@0 2143 * A keyed array containing the current state of the form, including
danielebarchiesi@0 2144 * submitted form values; altered by reference.
danielebarchiesi@0 2145 */
danielebarchiesi@0 2146 function form_state_values_clean(&$form_state) {
danielebarchiesi@0 2147 // Remove internal Form API values.
danielebarchiesi@0 2148 unset($form_state['values']['form_id'], $form_state['values']['form_token'], $form_state['values']['form_build_id'], $form_state['values']['op']);
danielebarchiesi@0 2149
danielebarchiesi@0 2150 // Remove button values.
danielebarchiesi@0 2151 // form_builder() collects all button elements in a form. We remove the button
danielebarchiesi@0 2152 // value separately for each button element.
danielebarchiesi@0 2153 foreach ($form_state['buttons'] as $button) {
danielebarchiesi@0 2154 // Remove this button's value from the submitted form values by finding
danielebarchiesi@0 2155 // the value corresponding to this button.
danielebarchiesi@0 2156 // We iterate over the #parents of this button and move a reference to
danielebarchiesi@0 2157 // each parent in $form_state['values']. For example, if #parents is:
danielebarchiesi@0 2158 // array('foo', 'bar', 'baz')
danielebarchiesi@0 2159 // then the corresponding $form_state['values'] part will look like this:
danielebarchiesi@0 2160 // array(
danielebarchiesi@0 2161 // 'foo' => array(
danielebarchiesi@0 2162 // 'bar' => array(
danielebarchiesi@0 2163 // 'baz' => 'button_value',
danielebarchiesi@0 2164 // ),
danielebarchiesi@0 2165 // ),
danielebarchiesi@0 2166 // )
danielebarchiesi@0 2167 // We start by (re)moving 'baz' to $last_parent, so we are able unset it
danielebarchiesi@0 2168 // at the end of the iteration. Initially, $values will contain a
danielebarchiesi@0 2169 // reference to $form_state['values'], but in the iteration we move the
danielebarchiesi@0 2170 // reference to $form_state['values']['foo'], and finally to
danielebarchiesi@0 2171 // $form_state['values']['foo']['bar'], which is the level where we can
danielebarchiesi@0 2172 // unset 'baz' (that is stored in $last_parent).
danielebarchiesi@0 2173 $parents = $button['#parents'];
danielebarchiesi@0 2174 $last_parent = array_pop($parents);
danielebarchiesi@0 2175 $key_exists = NULL;
danielebarchiesi@0 2176 $values = &drupal_array_get_nested_value($form_state['values'], $parents, $key_exists);
danielebarchiesi@0 2177 if ($key_exists && is_array($values)) {
danielebarchiesi@0 2178 unset($values[$last_parent]);
danielebarchiesi@0 2179 }
danielebarchiesi@0 2180 }
danielebarchiesi@0 2181 }
danielebarchiesi@0 2182
danielebarchiesi@0 2183 /**
danielebarchiesi@0 2184 * Determines the value for an image button form element.
danielebarchiesi@0 2185 *
danielebarchiesi@0 2186 * @param $form
danielebarchiesi@0 2187 * The form element whose value is being populated.
danielebarchiesi@0 2188 * @param $input
danielebarchiesi@0 2189 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2190 * the element's default value should be returned.
danielebarchiesi@0 2191 * @param $form_state
danielebarchiesi@0 2192 * A keyed array containing the current state of the form.
danielebarchiesi@0 2193 *
danielebarchiesi@0 2194 * @return
danielebarchiesi@0 2195 * The data that will appear in the $form_state['values'] collection
danielebarchiesi@0 2196 * for this element. Return nothing to use the default.
danielebarchiesi@0 2197 */
danielebarchiesi@0 2198 function form_type_image_button_value($form, $input, $form_state) {
danielebarchiesi@0 2199 if ($input !== FALSE) {
danielebarchiesi@0 2200 if (!empty($input)) {
danielebarchiesi@0 2201 // If we're dealing with Mozilla or Opera, we're lucky. It will
danielebarchiesi@0 2202 // return a proper value, and we can get on with things.
danielebarchiesi@0 2203 return $form['#return_value'];
danielebarchiesi@0 2204 }
danielebarchiesi@0 2205 else {
danielebarchiesi@0 2206 // Unfortunately, in IE we never get back a proper value for THIS
danielebarchiesi@0 2207 // form element. Instead, we get back two split values: one for the
danielebarchiesi@0 2208 // X and one for the Y coordinates on which the user clicked the
danielebarchiesi@0 2209 // button. We'll find this element in the #post data, and search
danielebarchiesi@0 2210 // in the same spot for its name, with '_x'.
danielebarchiesi@0 2211 $input = $form_state['input'];
danielebarchiesi@0 2212 foreach (explode('[', $form['#name']) as $element_name) {
danielebarchiesi@0 2213 // chop off the ] that may exist.
danielebarchiesi@0 2214 if (substr($element_name, -1) == ']') {
danielebarchiesi@0 2215 $element_name = substr($element_name, 0, -1);
danielebarchiesi@0 2216 }
danielebarchiesi@0 2217
danielebarchiesi@0 2218 if (!isset($input[$element_name])) {
danielebarchiesi@0 2219 if (isset($input[$element_name . '_x'])) {
danielebarchiesi@0 2220 return $form['#return_value'];
danielebarchiesi@0 2221 }
danielebarchiesi@0 2222 return NULL;
danielebarchiesi@0 2223 }
danielebarchiesi@0 2224 $input = $input[$element_name];
danielebarchiesi@0 2225 }
danielebarchiesi@0 2226 return $form['#return_value'];
danielebarchiesi@0 2227 }
danielebarchiesi@0 2228 }
danielebarchiesi@0 2229 }
danielebarchiesi@0 2230
danielebarchiesi@0 2231 /**
danielebarchiesi@0 2232 * Determines the value for a checkbox form element.
danielebarchiesi@0 2233 *
danielebarchiesi@0 2234 * @param $form
danielebarchiesi@0 2235 * The form element whose value is being populated.
danielebarchiesi@0 2236 * @param $input
danielebarchiesi@0 2237 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2238 * the element's default value should be returned.
danielebarchiesi@0 2239 *
danielebarchiesi@0 2240 * @return
danielebarchiesi@0 2241 * The data that will appear in the $element_state['values'] collection
danielebarchiesi@0 2242 * for this element. Return nothing to use the default.
danielebarchiesi@0 2243 */
danielebarchiesi@0 2244 function form_type_checkbox_value($element, $input = FALSE) {
danielebarchiesi@0 2245 if ($input === FALSE) {
danielebarchiesi@0 2246 // Use #default_value as the default value of a checkbox, except change
danielebarchiesi@0 2247 // NULL to 0, because _form_builder_handle_input_element() would otherwise
danielebarchiesi@0 2248 // replace NULL with empty string, but an empty string is a potentially
danielebarchiesi@0 2249 // valid value for a checked checkbox.
danielebarchiesi@0 2250 return isset($element['#default_value']) ? $element['#default_value'] : 0;
danielebarchiesi@0 2251 }
danielebarchiesi@0 2252 else {
danielebarchiesi@0 2253 // Checked checkboxes are submitted with a value (possibly '0' or ''):
danielebarchiesi@0 2254 // http://www.w3.org/TR/html401/interact/forms.html#successful-controls.
danielebarchiesi@0 2255 // For checked checkboxes, browsers submit the string version of
danielebarchiesi@0 2256 // #return_value, but we return the original #return_value. For unchecked
danielebarchiesi@0 2257 // checkboxes, browsers submit nothing at all, but
danielebarchiesi@0 2258 // _form_builder_handle_input_element() detects this, and calls this
danielebarchiesi@0 2259 // function with $input=NULL. Returning NULL from a value callback means to
danielebarchiesi@0 2260 // use the default value, which is not what is wanted when an unchecked
danielebarchiesi@0 2261 // checkbox is submitted, so we use integer 0 as the value indicating an
danielebarchiesi@0 2262 // unchecked checkbox. Therefore, modules must not use integer 0 as a
danielebarchiesi@0 2263 // #return_value, as doing so results in the checkbox always being treated
danielebarchiesi@0 2264 // as unchecked. The string '0' is allowed for #return_value. The most
danielebarchiesi@0 2265 // common use-case for setting #return_value to either 0 or '0' is for the
danielebarchiesi@0 2266 // first option within a 0-indexed array of checkboxes, and for this,
danielebarchiesi@0 2267 // form_process_checkboxes() uses the string rather than the integer.
danielebarchiesi@0 2268 return isset($input) ? $element['#return_value'] : 0;
danielebarchiesi@0 2269 }
danielebarchiesi@0 2270 }
danielebarchiesi@0 2271
danielebarchiesi@0 2272 /**
danielebarchiesi@0 2273 * Determines the value for a checkboxes form element.
danielebarchiesi@0 2274 *
danielebarchiesi@0 2275 * @param $element
danielebarchiesi@0 2276 * The form element whose value is being populated.
danielebarchiesi@0 2277 * @param $input
danielebarchiesi@0 2278 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2279 * the element's default value should be returned.
danielebarchiesi@0 2280 *
danielebarchiesi@0 2281 * @return
danielebarchiesi@0 2282 * The data that will appear in the $element_state['values'] collection
danielebarchiesi@0 2283 * for this element. Return nothing to use the default.
danielebarchiesi@0 2284 */
danielebarchiesi@0 2285 function form_type_checkboxes_value($element, $input = FALSE) {
danielebarchiesi@0 2286 if ($input === FALSE) {
danielebarchiesi@0 2287 $value = array();
danielebarchiesi@0 2288 $element += array('#default_value' => array());
danielebarchiesi@0 2289 foreach ($element['#default_value'] as $key) {
danielebarchiesi@0 2290 $value[$key] = $key;
danielebarchiesi@0 2291 }
danielebarchiesi@0 2292 return $value;
danielebarchiesi@0 2293 }
danielebarchiesi@0 2294 elseif (is_array($input)) {
danielebarchiesi@0 2295 // Programmatic form submissions use NULL to indicate that a checkbox
danielebarchiesi@0 2296 // should be unchecked; see drupal_form_submit(). We therefore remove all
danielebarchiesi@0 2297 // NULL elements from the array before constructing the return value, to
danielebarchiesi@0 2298 // simulate the behavior of web browsers (which do not send unchecked
danielebarchiesi@0 2299 // checkboxes to the server at all). This will not affect non-programmatic
danielebarchiesi@0 2300 // form submissions, since all values in $_POST are strings.
danielebarchiesi@0 2301 foreach ($input as $key => $value) {
danielebarchiesi@0 2302 if (!isset($value)) {
danielebarchiesi@0 2303 unset($input[$key]);
danielebarchiesi@0 2304 }
danielebarchiesi@0 2305 }
danielebarchiesi@0 2306 return drupal_map_assoc($input);
danielebarchiesi@0 2307 }
danielebarchiesi@0 2308 else {
danielebarchiesi@0 2309 return array();
danielebarchiesi@0 2310 }
danielebarchiesi@0 2311 }
danielebarchiesi@0 2312
danielebarchiesi@0 2313 /**
danielebarchiesi@0 2314 * Determines the value for a tableselect form element.
danielebarchiesi@0 2315 *
danielebarchiesi@0 2316 * @param $element
danielebarchiesi@0 2317 * The form element whose value is being populated.
danielebarchiesi@0 2318 * @param $input
danielebarchiesi@0 2319 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2320 * the element's default value should be returned.
danielebarchiesi@0 2321 *
danielebarchiesi@0 2322 * @return
danielebarchiesi@0 2323 * The data that will appear in the $element_state['values'] collection
danielebarchiesi@0 2324 * for this element. Return nothing to use the default.
danielebarchiesi@0 2325 */
danielebarchiesi@0 2326 function form_type_tableselect_value($element, $input = FALSE) {
danielebarchiesi@0 2327 // If $element['#multiple'] == FALSE, then radio buttons are displayed and
danielebarchiesi@0 2328 // the default value handling is used.
danielebarchiesi@0 2329 if (isset($element['#multiple']) && $element['#multiple']) {
danielebarchiesi@0 2330 // Checkboxes are being displayed with the default value coming from the
danielebarchiesi@0 2331 // keys of the #default_value property. This differs from the checkboxes
danielebarchiesi@0 2332 // element which uses the array values.
danielebarchiesi@0 2333 if ($input === FALSE) {
danielebarchiesi@0 2334 $value = array();
danielebarchiesi@0 2335 $element += array('#default_value' => array());
danielebarchiesi@0 2336 foreach ($element['#default_value'] as $key => $flag) {
danielebarchiesi@0 2337 if ($flag) {
danielebarchiesi@0 2338 $value[$key] = $key;
danielebarchiesi@0 2339 }
danielebarchiesi@0 2340 }
danielebarchiesi@0 2341 return $value;
danielebarchiesi@0 2342 }
danielebarchiesi@0 2343 else {
danielebarchiesi@0 2344 return is_array($input) ? drupal_map_assoc($input) : array();
danielebarchiesi@0 2345 }
danielebarchiesi@0 2346 }
danielebarchiesi@0 2347 }
danielebarchiesi@0 2348
danielebarchiesi@0 2349 /**
danielebarchiesi@0 2350 * Form value callback: Determines the value for a #type radios form element.
danielebarchiesi@0 2351 *
danielebarchiesi@0 2352 * @param $element
danielebarchiesi@0 2353 * The form element whose value is being populated.
danielebarchiesi@0 2354 * @param $input
danielebarchiesi@0 2355 * (optional) The incoming input to populate the form element. If FALSE, the
danielebarchiesi@0 2356 * element's default value is returned. Defaults to FALSE.
danielebarchiesi@0 2357 *
danielebarchiesi@0 2358 * @return
danielebarchiesi@0 2359 * The data that will appear in the $element_state['values'] collection for
danielebarchiesi@0 2360 * this element.
danielebarchiesi@0 2361 */
danielebarchiesi@0 2362 function form_type_radios_value(&$element, $input = FALSE) {
danielebarchiesi@0 2363 if ($input !== FALSE) {
danielebarchiesi@0 2364 // When there's user input (including NULL), return it as the value.
danielebarchiesi@0 2365 // However, if NULL is submitted, _form_builder_handle_input_element() will
danielebarchiesi@0 2366 // apply the default value, and we want that validated against #options
danielebarchiesi@0 2367 // unless it's empty. (An empty #default_value, such as NULL or FALSE, can
danielebarchiesi@0 2368 // be used to indicate that no radio button is selected by default.)
danielebarchiesi@0 2369 if (!isset($input) && !empty($element['#default_value'])) {
danielebarchiesi@0 2370 $element['#needs_validation'] = TRUE;
danielebarchiesi@0 2371 }
danielebarchiesi@0 2372 return $input;
danielebarchiesi@0 2373 }
danielebarchiesi@0 2374 else {
danielebarchiesi@0 2375 // For default value handling, simply return #default_value. Additionally,
danielebarchiesi@0 2376 // for a NULL default value, set #has_garbage_value to prevent
danielebarchiesi@0 2377 // _form_builder_handle_input_element() converting the NULL to an empty
danielebarchiesi@0 2378 // string, so that code can distinguish between nothing selected and the
danielebarchiesi@0 2379 // selection of a radio button whose value is an empty string.
danielebarchiesi@0 2380 $value = isset($element['#default_value']) ? $element['#default_value'] : NULL;
danielebarchiesi@0 2381 if (!isset($value)) {
danielebarchiesi@0 2382 $element['#has_garbage_value'] = TRUE;
danielebarchiesi@0 2383 }
danielebarchiesi@0 2384 return $value;
danielebarchiesi@0 2385 }
danielebarchiesi@0 2386 }
danielebarchiesi@0 2387
danielebarchiesi@0 2388 /**
danielebarchiesi@0 2389 * Determines the value for a password_confirm form element.
danielebarchiesi@0 2390 *
danielebarchiesi@0 2391 * @param $element
danielebarchiesi@0 2392 * The form element whose value is being populated.
danielebarchiesi@0 2393 * @param $input
danielebarchiesi@0 2394 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2395 * the element's default value should be returned.
danielebarchiesi@0 2396 *
danielebarchiesi@0 2397 * @return
danielebarchiesi@0 2398 * The data that will appear in the $element_state['values'] collection
danielebarchiesi@0 2399 * for this element. Return nothing to use the default.
danielebarchiesi@0 2400 */
danielebarchiesi@0 2401 function form_type_password_confirm_value($element, $input = FALSE) {
danielebarchiesi@0 2402 if ($input === FALSE) {
danielebarchiesi@0 2403 $element += array('#default_value' => array());
danielebarchiesi@0 2404 return $element['#default_value'] + array('pass1' => '', 'pass2' => '');
danielebarchiesi@0 2405 }
danielebarchiesi@0 2406 }
danielebarchiesi@0 2407
danielebarchiesi@0 2408 /**
danielebarchiesi@0 2409 * Determines the value for a select form element.
danielebarchiesi@0 2410 *
danielebarchiesi@0 2411 * @param $element
danielebarchiesi@0 2412 * The form element whose value is being populated.
danielebarchiesi@0 2413 * @param $input
danielebarchiesi@0 2414 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2415 * the element's default value should be returned.
danielebarchiesi@0 2416 *
danielebarchiesi@0 2417 * @return
danielebarchiesi@0 2418 * The data that will appear in the $element_state['values'] collection
danielebarchiesi@0 2419 * for this element. Return nothing to use the default.
danielebarchiesi@0 2420 */
danielebarchiesi@0 2421 function form_type_select_value($element, $input = FALSE) {
danielebarchiesi@0 2422 if ($input !== FALSE) {
danielebarchiesi@0 2423 if (isset($element['#multiple']) && $element['#multiple']) {
danielebarchiesi@0 2424 // If an enabled multi-select submits NULL, it means all items are
danielebarchiesi@0 2425 // unselected. A disabled multi-select always submits NULL, and the
danielebarchiesi@0 2426 // default value should be used.
danielebarchiesi@0 2427 if (empty($element['#disabled'])) {
danielebarchiesi@0 2428 return (is_array($input)) ? drupal_map_assoc($input) : array();
danielebarchiesi@0 2429 }
danielebarchiesi@0 2430 else {
danielebarchiesi@0 2431 return (isset($element['#default_value']) && is_array($element['#default_value'])) ? $element['#default_value'] : array();
danielebarchiesi@0 2432 }
danielebarchiesi@0 2433 }
danielebarchiesi@0 2434 // Non-multiple select elements may have an empty option preprended to them
danielebarchiesi@0 2435 // (see form_process_select()). When this occurs, usually #empty_value is
danielebarchiesi@0 2436 // an empty string, but some forms set #empty_value to integer 0 or some
danielebarchiesi@0 2437 // other non-string constant. PHP receives all submitted form input as
danielebarchiesi@0 2438 // strings, but if the empty option is selected, set the value to match the
danielebarchiesi@0 2439 // empty value exactly.
danielebarchiesi@0 2440 elseif (isset($element['#empty_value']) && $input === (string) $element['#empty_value']) {
danielebarchiesi@0 2441 return $element['#empty_value'];
danielebarchiesi@0 2442 }
danielebarchiesi@0 2443 else {
danielebarchiesi@0 2444 return $input;
danielebarchiesi@0 2445 }
danielebarchiesi@0 2446 }
danielebarchiesi@0 2447 }
danielebarchiesi@0 2448
danielebarchiesi@0 2449 /**
danielebarchiesi@0 2450 * Determines the value for a textfield form element.
danielebarchiesi@0 2451 *
danielebarchiesi@0 2452 * @param $element
danielebarchiesi@0 2453 * The form element whose value is being populated.
danielebarchiesi@0 2454 * @param $input
danielebarchiesi@0 2455 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2456 * the element's default value should be returned.
danielebarchiesi@0 2457 *
danielebarchiesi@0 2458 * @return
danielebarchiesi@0 2459 * The data that will appear in the $element_state['values'] collection
danielebarchiesi@0 2460 * for this element. Return nothing to use the default.
danielebarchiesi@0 2461 */
danielebarchiesi@0 2462 function form_type_textfield_value($element, $input = FALSE) {
danielebarchiesi@0 2463 if ($input !== FALSE && $input !== NULL) {
danielebarchiesi@0 2464 // Equate $input to the form value to ensure it's marked for
danielebarchiesi@0 2465 // validation.
danielebarchiesi@0 2466 return str_replace(array("\r", "\n"), '', $input);
danielebarchiesi@0 2467 }
danielebarchiesi@0 2468 }
danielebarchiesi@0 2469
danielebarchiesi@0 2470 /**
danielebarchiesi@0 2471 * Determines the value for form's token value.
danielebarchiesi@0 2472 *
danielebarchiesi@0 2473 * @param $element
danielebarchiesi@0 2474 * The form element whose value is being populated.
danielebarchiesi@0 2475 * @param $input
danielebarchiesi@0 2476 * The incoming input to populate the form element. If this is FALSE,
danielebarchiesi@0 2477 * the element's default value should be returned.
danielebarchiesi@0 2478 *
danielebarchiesi@0 2479 * @return
danielebarchiesi@0 2480 * The data that will appear in the $element_state['values'] collection
danielebarchiesi@0 2481 * for this element. Return nothing to use the default.
danielebarchiesi@0 2482 */
danielebarchiesi@0 2483 function form_type_token_value($element, $input = FALSE) {
danielebarchiesi@0 2484 if ($input !== FALSE) {
danielebarchiesi@0 2485 return (string) $input;
danielebarchiesi@0 2486 }
danielebarchiesi@0 2487 }
danielebarchiesi@0 2488
danielebarchiesi@0 2489 /**
danielebarchiesi@0 2490 * Changes submitted form values during form validation.
danielebarchiesi@0 2491 *
danielebarchiesi@0 2492 * Use this function to change the submitted value of a form element in a form
danielebarchiesi@0 2493 * validation function, so that the changed value persists in $form_state
danielebarchiesi@0 2494 * through the remaining validation and submission handlers. It does not change
danielebarchiesi@0 2495 * the value in $element['#value'], only in $form_state['values'], which is
danielebarchiesi@0 2496 * where submitted values are always stored.
danielebarchiesi@0 2497 *
danielebarchiesi@0 2498 * Note that form validation functions are specified in the '#validate'
danielebarchiesi@0 2499 * component of the form array (the value of $form['#validate'] is an array of
danielebarchiesi@0 2500 * validation function names). If the form does not originate in your module,
danielebarchiesi@0 2501 * you can implement hook_form_FORM_ID_alter() to add a validation function
danielebarchiesi@0 2502 * to $form['#validate'].
danielebarchiesi@0 2503 *
danielebarchiesi@0 2504 * @param $element
danielebarchiesi@0 2505 * The form element that should have its value updated; in most cases you can
danielebarchiesi@0 2506 * just pass in the element from the $form array, although the only component
danielebarchiesi@0 2507 * that is actually used is '#parents'. If constructing yourself, set
danielebarchiesi@0 2508 * $element['#parents'] to be an array giving the path through the form
danielebarchiesi@0 2509 * array's keys to the element whose value you want to update. For instance,
danielebarchiesi@0 2510 * if you want to update the value of $form['elem1']['elem2'], which should be
danielebarchiesi@0 2511 * stored in $form_state['values']['elem1']['elem2'], you would set
danielebarchiesi@0 2512 * $element['#parents'] = array('elem1','elem2').
danielebarchiesi@0 2513 * @param $value
danielebarchiesi@0 2514 * The new value for the form element.
danielebarchiesi@0 2515 * @param $form_state
danielebarchiesi@0 2516 * Form state array where the value change should be recorded.
danielebarchiesi@0 2517 */
danielebarchiesi@0 2518 function form_set_value($element, $value, &$form_state) {
danielebarchiesi@0 2519 drupal_array_set_nested_value($form_state['values'], $element['#parents'], $value, TRUE);
danielebarchiesi@0 2520 }
danielebarchiesi@0 2521
danielebarchiesi@0 2522 /**
danielebarchiesi@0 2523 * Allows PHP array processing of multiple select options with the same value.
danielebarchiesi@0 2524 *
danielebarchiesi@0 2525 * Used for form select elements which need to validate HTML option groups
danielebarchiesi@0 2526 * and multiple options which may return the same value. Associative PHP arrays
danielebarchiesi@0 2527 * cannot handle these structures, since they share a common key.
danielebarchiesi@0 2528 *
danielebarchiesi@0 2529 * @param $array
danielebarchiesi@0 2530 * The form options array to process.
danielebarchiesi@0 2531 *
danielebarchiesi@0 2532 * @return
danielebarchiesi@0 2533 * An array with all hierarchical elements flattened to a single array.
danielebarchiesi@0 2534 */
danielebarchiesi@0 2535 function form_options_flatten($array) {
danielebarchiesi@0 2536 // Always reset static var when first entering the recursion.
danielebarchiesi@0 2537 drupal_static_reset('_form_options_flatten');
danielebarchiesi@0 2538 return _form_options_flatten($array);
danielebarchiesi@0 2539 }
danielebarchiesi@0 2540
danielebarchiesi@0 2541 /**
danielebarchiesi@0 2542 * Iterates over an array and returns a flat array with duplicate keys removed.
danielebarchiesi@0 2543 *
danielebarchiesi@0 2544 * This function also handles cases where objects are passed as array values.
danielebarchiesi@0 2545 */
danielebarchiesi@0 2546 function _form_options_flatten($array) {
danielebarchiesi@0 2547 $return = &drupal_static(__FUNCTION__);
danielebarchiesi@0 2548
danielebarchiesi@0 2549 foreach ($array as $key => $value) {
danielebarchiesi@0 2550 if (is_object($value)) {
danielebarchiesi@0 2551 _form_options_flatten($value->option);
danielebarchiesi@0 2552 }
danielebarchiesi@0 2553 elseif (is_array($value)) {
danielebarchiesi@0 2554 _form_options_flatten($value);
danielebarchiesi@0 2555 }
danielebarchiesi@0 2556 else {
danielebarchiesi@0 2557 $return[$key] = 1;
danielebarchiesi@0 2558 }
danielebarchiesi@0 2559 }
danielebarchiesi@0 2560
danielebarchiesi@0 2561 return $return;
danielebarchiesi@0 2562 }
danielebarchiesi@0 2563
danielebarchiesi@0 2564 /**
danielebarchiesi@0 2565 * Processes a select list form element.
danielebarchiesi@0 2566 *
danielebarchiesi@0 2567 * This process callback is mandatory for select fields, since all user agents
danielebarchiesi@0 2568 * automatically preselect the first available option of single (non-multiple)
danielebarchiesi@0 2569 * select lists.
danielebarchiesi@0 2570 *
danielebarchiesi@0 2571 * @param $element
danielebarchiesi@0 2572 * The form element to process. Properties used:
danielebarchiesi@0 2573 * - #multiple: (optional) Indicates whether one or more options can be
danielebarchiesi@0 2574 * selected. Defaults to FALSE.
danielebarchiesi@0 2575 * - #default_value: Must be NULL or not set in case there is no value for the
danielebarchiesi@0 2576 * element yet, in which case a first default option is inserted by default.
danielebarchiesi@0 2577 * Whether this first option is a valid option depends on whether the field
danielebarchiesi@0 2578 * is #required or not.
danielebarchiesi@0 2579 * - #required: (optional) Whether the user needs to select an option (TRUE)
danielebarchiesi@0 2580 * or not (FALSE). Defaults to FALSE.
danielebarchiesi@0 2581 * - #empty_option: (optional) The label to show for the first default option.
danielebarchiesi@0 2582 * By default, the label is automatically set to "- Please select -" for a
danielebarchiesi@0 2583 * required field and "- None -" for an optional field.
danielebarchiesi@0 2584 * - #empty_value: (optional) The value for the first default option, which is
danielebarchiesi@0 2585 * used to determine whether the user submitted a value or not.
danielebarchiesi@0 2586 * - If #required is TRUE, this defaults to '' (an empty string).
danielebarchiesi@0 2587 * - If #required is not TRUE and this value isn't set, then no extra option
danielebarchiesi@0 2588 * is added to the select control, leaving the control in a slightly
danielebarchiesi@0 2589 * illogical state, because there's no way for the user to select nothing,
danielebarchiesi@0 2590 * since all user agents automatically preselect the first available
danielebarchiesi@0 2591 * option. But people are used to this being the behavior of select
danielebarchiesi@0 2592 * controls.
danielebarchiesi@0 2593 * @todo Address the above issue in Drupal 8.
danielebarchiesi@0 2594 * - If #required is not TRUE and this value is set (most commonly to an
danielebarchiesi@0 2595 * empty string), then an extra option (see #empty_option above)
danielebarchiesi@0 2596 * representing a "non-selection" is added with this as its value.
danielebarchiesi@0 2597 *
danielebarchiesi@0 2598 * @see _form_validate()
danielebarchiesi@0 2599 */
danielebarchiesi@0 2600 function form_process_select($element) {
danielebarchiesi@0 2601 // #multiple select fields need a special #name.
danielebarchiesi@0 2602 if ($element['#multiple']) {
danielebarchiesi@0 2603 $element['#attributes']['multiple'] = 'multiple';
danielebarchiesi@0 2604 $element['#attributes']['name'] = $element['#name'] . '[]';
danielebarchiesi@0 2605 }
danielebarchiesi@0 2606 // A non-#multiple select needs special handling to prevent user agents from
danielebarchiesi@0 2607 // preselecting the first option without intention. #multiple select lists do
danielebarchiesi@0 2608 // not get an empty option, as it would not make sense, user interface-wise.
danielebarchiesi@0 2609 else {
danielebarchiesi@0 2610 $required = $element['#required'];
danielebarchiesi@0 2611 // If the element is required and there is no #default_value, then add an
danielebarchiesi@0 2612 // empty option that will fail validation, so that the user is required to
danielebarchiesi@0 2613 // make a choice. Also, if there's a value for #empty_value or
danielebarchiesi@0 2614 // #empty_option, then add an option that represents emptiness.
danielebarchiesi@0 2615 if (($required && !isset($element['#default_value'])) || isset($element['#empty_value']) || isset($element['#empty_option'])) {
danielebarchiesi@0 2616 $element += array(
danielebarchiesi@0 2617 '#empty_value' => '',
danielebarchiesi@0 2618 '#empty_option' => $required ? t('- Select -') : t('- None -'),
danielebarchiesi@0 2619 );
danielebarchiesi@0 2620 // The empty option is prepended to #options and purposively not merged
danielebarchiesi@0 2621 // to prevent another option in #options mistakenly using the same value
danielebarchiesi@0 2622 // as #empty_value.
danielebarchiesi@0 2623 $empty_option = array($element['#empty_value'] => $element['#empty_option']);
danielebarchiesi@0 2624 $element['#options'] = $empty_option + $element['#options'];
danielebarchiesi@0 2625 }
danielebarchiesi@0 2626 }
danielebarchiesi@0 2627 return $element;
danielebarchiesi@0 2628 }
danielebarchiesi@0 2629
danielebarchiesi@0 2630 /**
danielebarchiesi@0 2631 * Returns HTML for a select form element.
danielebarchiesi@0 2632 *
danielebarchiesi@0 2633 * It is possible to group options together; to do this, change the format of
danielebarchiesi@0 2634 * $options to an associative array in which the keys are group labels, and the
danielebarchiesi@0 2635 * values are associative arrays in the normal $options format.
danielebarchiesi@0 2636 *
danielebarchiesi@0 2637 * @param $variables
danielebarchiesi@0 2638 * An associative array containing:
danielebarchiesi@0 2639 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 2640 * Properties used: #title, #value, #options, #description, #extra,
danielebarchiesi@0 2641 * #multiple, #required, #name, #attributes, #size.
danielebarchiesi@0 2642 *
danielebarchiesi@0 2643 * @ingroup themeable
danielebarchiesi@0 2644 */
danielebarchiesi@0 2645 function theme_select($variables) {
danielebarchiesi@0 2646 $element = $variables['element'];
danielebarchiesi@0 2647 element_set_attributes($element, array('id', 'name', 'size'));
danielebarchiesi@0 2648 _form_set_class($element, array('form-select'));
danielebarchiesi@0 2649
danielebarchiesi@0 2650 return '<select' . drupal_attributes($element['#attributes']) . '>' . form_select_options($element) . '</select>';
danielebarchiesi@0 2651 }
danielebarchiesi@0 2652
danielebarchiesi@0 2653 /**
danielebarchiesi@0 2654 * Converts a select form element's options array into HTML.
danielebarchiesi@0 2655 *
danielebarchiesi@0 2656 * @param $element
danielebarchiesi@0 2657 * An associative array containing the properties of the element.
danielebarchiesi@0 2658 * @param $choices
danielebarchiesi@0 2659 * Mixed: Either an associative array of items to list as choices, or an
danielebarchiesi@0 2660 * object with an 'option' member that is an associative array. This
danielebarchiesi@0 2661 * parameter is only used internally and should not be passed.
danielebarchiesi@0 2662 *
danielebarchiesi@0 2663 * @return
danielebarchiesi@0 2664 * An HTML string of options for the select form element.
danielebarchiesi@0 2665 */
danielebarchiesi@0 2666 function form_select_options($element, $choices = NULL) {
danielebarchiesi@0 2667 if (!isset($choices)) {
danielebarchiesi@0 2668 $choices = $element['#options'];
danielebarchiesi@0 2669 }
danielebarchiesi@0 2670 // array_key_exists() accommodates the rare event where $element['#value'] is NULL.
danielebarchiesi@0 2671 // isset() fails in this situation.
danielebarchiesi@0 2672 $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
danielebarchiesi@0 2673 $value_is_array = $value_valid && is_array($element['#value']);
danielebarchiesi@0 2674 $options = '';
danielebarchiesi@0 2675 foreach ($choices as $key => $choice) {
danielebarchiesi@0 2676 if (is_array($choice)) {
danielebarchiesi@0 2677 $options .= '<optgroup label="' . $key . '">';
danielebarchiesi@0 2678 $options .= form_select_options($element, $choice);
danielebarchiesi@0 2679 $options .= '</optgroup>';
danielebarchiesi@0 2680 }
danielebarchiesi@0 2681 elseif (is_object($choice)) {
danielebarchiesi@0 2682 $options .= form_select_options($element, $choice->option);
danielebarchiesi@0 2683 }
danielebarchiesi@0 2684 else {
danielebarchiesi@0 2685 $key = (string) $key;
danielebarchiesi@0 2686 if ($value_valid && (!$value_is_array && (string) $element['#value'] === $key || ($value_is_array && in_array($key, $element['#value'])))) {
danielebarchiesi@0 2687 $selected = ' selected="selected"';
danielebarchiesi@0 2688 }
danielebarchiesi@0 2689 else {
danielebarchiesi@0 2690 $selected = '';
danielebarchiesi@0 2691 }
danielebarchiesi@0 2692 $options .= '<option value="' . check_plain($key) . '"' . $selected . '>' . check_plain($choice) . '</option>';
danielebarchiesi@0 2693 }
danielebarchiesi@0 2694 }
danielebarchiesi@0 2695 return $options;
danielebarchiesi@0 2696 }
danielebarchiesi@0 2697
danielebarchiesi@0 2698 /**
danielebarchiesi@0 2699 * Returns the indexes of a select element's options matching a given key.
danielebarchiesi@0 2700 *
danielebarchiesi@0 2701 * This function is useful if you need to modify the options that are
danielebarchiesi@0 2702 * already in a form element; for example, to remove choices which are
danielebarchiesi@0 2703 * not valid because of additional filters imposed by another module.
danielebarchiesi@0 2704 * One example might be altering the choices in a taxonomy selector.
danielebarchiesi@0 2705 * To correctly handle the case of a multiple hierarchy taxonomy,
danielebarchiesi@0 2706 * #options arrays can now hold an array of objects, instead of a
danielebarchiesi@0 2707 * direct mapping of keys to labels, so that multiple choices in the
danielebarchiesi@0 2708 * selector can have the same key (and label). This makes it difficult
danielebarchiesi@0 2709 * to manipulate directly, which is why this helper function exists.
danielebarchiesi@0 2710 *
danielebarchiesi@0 2711 * This function does not support optgroups (when the elements of the
danielebarchiesi@0 2712 * #options array are themselves arrays), and will return FALSE if
danielebarchiesi@0 2713 * arrays are found. The caller must either flatten/restore or
danielebarchiesi@0 2714 * manually do their manipulations in this case, since returning the
danielebarchiesi@0 2715 * index is not sufficient, and supporting this would make the
danielebarchiesi@0 2716 * "helper" too complicated and cumbersome to be of any help.
danielebarchiesi@0 2717 *
danielebarchiesi@0 2718 * As usual with functions that can return array() or FALSE, do not
danielebarchiesi@0 2719 * forget to use === and !== if needed.
danielebarchiesi@0 2720 *
danielebarchiesi@0 2721 * @param $element
danielebarchiesi@0 2722 * The select element to search.
danielebarchiesi@0 2723 * @param $key
danielebarchiesi@0 2724 * The key to look for.
danielebarchiesi@0 2725 *
danielebarchiesi@0 2726 * @return
danielebarchiesi@0 2727 * An array of indexes that match the given $key. Array will be
danielebarchiesi@0 2728 * empty if no elements were found. FALSE if optgroups were found.
danielebarchiesi@0 2729 */
danielebarchiesi@0 2730 function form_get_options($element, $key) {
danielebarchiesi@0 2731 $keys = array();
danielebarchiesi@0 2732 foreach ($element['#options'] as $index => $choice) {
danielebarchiesi@0 2733 if (is_array($choice)) {
danielebarchiesi@0 2734 return FALSE;
danielebarchiesi@0 2735 }
danielebarchiesi@0 2736 elseif (is_object($choice)) {
danielebarchiesi@0 2737 if (isset($choice->option[$key])) {
danielebarchiesi@0 2738 $keys[] = $index;
danielebarchiesi@0 2739 }
danielebarchiesi@0 2740 }
danielebarchiesi@0 2741 elseif ($index == $key) {
danielebarchiesi@0 2742 $keys[] = $index;
danielebarchiesi@0 2743 }
danielebarchiesi@0 2744 }
danielebarchiesi@0 2745 return $keys;
danielebarchiesi@0 2746 }
danielebarchiesi@0 2747
danielebarchiesi@0 2748 /**
danielebarchiesi@0 2749 * Returns HTML for a fieldset form element and its children.
danielebarchiesi@0 2750 *
danielebarchiesi@0 2751 * @param $variables
danielebarchiesi@0 2752 * An associative array containing:
danielebarchiesi@0 2753 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 2754 * Properties used: #attributes, #children, #collapsed, #collapsible,
danielebarchiesi@0 2755 * #description, #id, #title, #value.
danielebarchiesi@0 2756 *
danielebarchiesi@0 2757 * @ingroup themeable
danielebarchiesi@0 2758 */
danielebarchiesi@0 2759 function theme_fieldset($variables) {
danielebarchiesi@0 2760 $element = $variables['element'];
danielebarchiesi@0 2761 element_set_attributes($element, array('id'));
danielebarchiesi@0 2762 _form_set_class($element, array('form-wrapper'));
danielebarchiesi@0 2763
danielebarchiesi@0 2764 $output = '<fieldset' . drupal_attributes($element['#attributes']) . '>';
danielebarchiesi@0 2765 if (!empty($element['#title'])) {
danielebarchiesi@0 2766 // Always wrap fieldset legends in a SPAN for CSS positioning.
danielebarchiesi@0 2767 $output .= '<legend><span class="fieldset-legend">' . $element['#title'] . '</span></legend>';
danielebarchiesi@0 2768 }
danielebarchiesi@0 2769 $output .= '<div class="fieldset-wrapper">';
danielebarchiesi@0 2770 if (!empty($element['#description'])) {
danielebarchiesi@0 2771 $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
danielebarchiesi@0 2772 }
danielebarchiesi@0 2773 $output .= $element['#children'];
danielebarchiesi@0 2774 if (isset($element['#value'])) {
danielebarchiesi@0 2775 $output .= $element['#value'];
danielebarchiesi@0 2776 }
danielebarchiesi@0 2777 $output .= '</div>';
danielebarchiesi@0 2778 $output .= "</fieldset>\n";
danielebarchiesi@0 2779 return $output;
danielebarchiesi@0 2780 }
danielebarchiesi@0 2781
danielebarchiesi@0 2782 /**
danielebarchiesi@0 2783 * Returns HTML for a radio button form element.
danielebarchiesi@0 2784 *
danielebarchiesi@0 2785 * Note: The input "name" attribute needs to be sanitized before output, which
danielebarchiesi@0 2786 * is currently done by passing all attributes to drupal_attributes().
danielebarchiesi@0 2787 *
danielebarchiesi@0 2788 * @param $variables
danielebarchiesi@0 2789 * An associative array containing:
danielebarchiesi@0 2790 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 2791 * Properties used: #required, #return_value, #value, #attributes, #title,
danielebarchiesi@0 2792 * #description
danielebarchiesi@0 2793 *
danielebarchiesi@0 2794 * @ingroup themeable
danielebarchiesi@0 2795 */
danielebarchiesi@0 2796 function theme_radio($variables) {
danielebarchiesi@0 2797 $element = $variables['element'];
danielebarchiesi@0 2798 $element['#attributes']['type'] = 'radio';
danielebarchiesi@0 2799 element_set_attributes($element, array('id', 'name', '#return_value' => 'value'));
danielebarchiesi@0 2800
danielebarchiesi@0 2801 if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) {
danielebarchiesi@0 2802 $element['#attributes']['checked'] = 'checked';
danielebarchiesi@0 2803 }
danielebarchiesi@0 2804 _form_set_class($element, array('form-radio'));
danielebarchiesi@0 2805
danielebarchiesi@0 2806 return '<input' . drupal_attributes($element['#attributes']) . ' />';
danielebarchiesi@0 2807 }
danielebarchiesi@0 2808
danielebarchiesi@0 2809 /**
danielebarchiesi@0 2810 * Returns HTML for a set of radio button form elements.
danielebarchiesi@0 2811 *
danielebarchiesi@0 2812 * @param $variables
danielebarchiesi@0 2813 * An associative array containing:
danielebarchiesi@0 2814 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 2815 * Properties used: #title, #value, #options, #description, #required,
danielebarchiesi@0 2816 * #attributes, #children.
danielebarchiesi@0 2817 *
danielebarchiesi@0 2818 * @ingroup themeable
danielebarchiesi@0 2819 */
danielebarchiesi@0 2820 function theme_radios($variables) {
danielebarchiesi@0 2821 $element = $variables['element'];
danielebarchiesi@0 2822 $attributes = array();
danielebarchiesi@0 2823 if (isset($element['#id'])) {
danielebarchiesi@0 2824 $attributes['id'] = $element['#id'];
danielebarchiesi@0 2825 }
danielebarchiesi@0 2826 $attributes['class'] = 'form-radios';
danielebarchiesi@0 2827 if (!empty($element['#attributes']['class'])) {
danielebarchiesi@0 2828 $attributes['class'] .= ' ' . implode(' ', $element['#attributes']['class']);
danielebarchiesi@0 2829 }
danielebarchiesi@0 2830 if (isset($element['#attributes']['title'])) {
danielebarchiesi@0 2831 $attributes['title'] = $element['#attributes']['title'];
danielebarchiesi@0 2832 }
danielebarchiesi@0 2833 return '<div' . drupal_attributes($attributes) . '>' . (!empty($element['#children']) ? $element['#children'] : '') . '</div>';
danielebarchiesi@0 2834 }
danielebarchiesi@0 2835
danielebarchiesi@0 2836 /**
danielebarchiesi@0 2837 * Expand a password_confirm field into two text boxes.
danielebarchiesi@0 2838 */
danielebarchiesi@0 2839 function form_process_password_confirm($element) {
danielebarchiesi@0 2840 $element['pass1'] = array(
danielebarchiesi@0 2841 '#type' => 'password',
danielebarchiesi@0 2842 '#title' => t('Password'),
danielebarchiesi@0 2843 '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'],
danielebarchiesi@0 2844 '#required' => $element['#required'],
danielebarchiesi@0 2845 '#attributes' => array('class' => array('password-field')),
danielebarchiesi@0 2846 );
danielebarchiesi@0 2847 $element['pass2'] = array(
danielebarchiesi@0 2848 '#type' => 'password',
danielebarchiesi@0 2849 '#title' => t('Confirm password'),
danielebarchiesi@0 2850 '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'],
danielebarchiesi@0 2851 '#required' => $element['#required'],
danielebarchiesi@0 2852 '#attributes' => array('class' => array('password-confirm')),
danielebarchiesi@0 2853 );
danielebarchiesi@0 2854 $element['#element_validate'] = array('password_confirm_validate');
danielebarchiesi@0 2855 $element['#tree'] = TRUE;
danielebarchiesi@0 2856
danielebarchiesi@0 2857 if (isset($element['#size'])) {
danielebarchiesi@0 2858 $element['pass1']['#size'] = $element['pass2']['#size'] = $element['#size'];
danielebarchiesi@0 2859 }
danielebarchiesi@0 2860
danielebarchiesi@0 2861 return $element;
danielebarchiesi@0 2862 }
danielebarchiesi@0 2863
danielebarchiesi@0 2864 /**
danielebarchiesi@0 2865 * Validates a password_confirm element.
danielebarchiesi@0 2866 */
danielebarchiesi@0 2867 function password_confirm_validate($element, &$element_state) {
danielebarchiesi@0 2868 $pass1 = trim($element['pass1']['#value']);
danielebarchiesi@0 2869 $pass2 = trim($element['pass2']['#value']);
danielebarchiesi@0 2870 if (!empty($pass1) || !empty($pass2)) {
danielebarchiesi@0 2871 if (strcmp($pass1, $pass2)) {
danielebarchiesi@0 2872 form_error($element, t('The specified passwords do not match.'));
danielebarchiesi@0 2873 }
danielebarchiesi@0 2874 }
danielebarchiesi@0 2875 elseif ($element['#required'] && !empty($element_state['input'])) {
danielebarchiesi@0 2876 form_error($element, t('Password field is required.'));
danielebarchiesi@0 2877 }
danielebarchiesi@0 2878
danielebarchiesi@0 2879 // Password field must be converted from a two-element array into a single
danielebarchiesi@0 2880 // string regardless of validation results.
danielebarchiesi@0 2881 form_set_value($element['pass1'], NULL, $element_state);
danielebarchiesi@0 2882 form_set_value($element['pass2'], NULL, $element_state);
danielebarchiesi@0 2883 form_set_value($element, $pass1, $element_state);
danielebarchiesi@0 2884
danielebarchiesi@0 2885 return $element;
danielebarchiesi@0 2886
danielebarchiesi@0 2887 }
danielebarchiesi@0 2888
danielebarchiesi@0 2889 /**
danielebarchiesi@0 2890 * Returns HTML for a date selection form element.
danielebarchiesi@0 2891 *
danielebarchiesi@0 2892 * @param $variables
danielebarchiesi@0 2893 * An associative array containing:
danielebarchiesi@0 2894 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 2895 * Properties used: #title, #value, #options, #description, #required,
danielebarchiesi@0 2896 * #attributes.
danielebarchiesi@0 2897 *
danielebarchiesi@0 2898 * @ingroup themeable
danielebarchiesi@0 2899 */
danielebarchiesi@0 2900 function theme_date($variables) {
danielebarchiesi@0 2901 $element = $variables['element'];
danielebarchiesi@0 2902
danielebarchiesi@0 2903 $attributes = array();
danielebarchiesi@0 2904 if (isset($element['#id'])) {
danielebarchiesi@0 2905 $attributes['id'] = $element['#id'];
danielebarchiesi@0 2906 }
danielebarchiesi@0 2907 if (!empty($element['#attributes']['class'])) {
danielebarchiesi@0 2908 $attributes['class'] = (array) $element['#attributes']['class'];
danielebarchiesi@0 2909 }
danielebarchiesi@0 2910 $attributes['class'][] = 'container-inline';
danielebarchiesi@0 2911
danielebarchiesi@0 2912 return '<div' . drupal_attributes($attributes) . '>' . drupal_render_children($element) . '</div>';
danielebarchiesi@0 2913 }
danielebarchiesi@0 2914
danielebarchiesi@0 2915 /**
danielebarchiesi@0 2916 * Expands a date element into year, month, and day select elements.
danielebarchiesi@0 2917 */
danielebarchiesi@0 2918 function form_process_date($element) {
danielebarchiesi@0 2919 // Default to current date
danielebarchiesi@0 2920 if (empty($element['#value'])) {
danielebarchiesi@0 2921 $element['#value'] = array(
danielebarchiesi@0 2922 'day' => format_date(REQUEST_TIME, 'custom', 'j'),
danielebarchiesi@0 2923 'month' => format_date(REQUEST_TIME, 'custom', 'n'),
danielebarchiesi@0 2924 'year' => format_date(REQUEST_TIME, 'custom', 'Y'),
danielebarchiesi@0 2925 );
danielebarchiesi@0 2926 }
danielebarchiesi@0 2927
danielebarchiesi@0 2928 $element['#tree'] = TRUE;
danielebarchiesi@0 2929
danielebarchiesi@0 2930 // Determine the order of day, month, year in the site's chosen date format.
danielebarchiesi@0 2931 $format = variable_get('date_format_short', 'm/d/Y - H:i');
danielebarchiesi@0 2932 $sort = array();
danielebarchiesi@0 2933 $sort['day'] = max(strpos($format, 'd'), strpos($format, 'j'));
danielebarchiesi@0 2934 $sort['month'] = max(strpos($format, 'm'), strpos($format, 'M'));
danielebarchiesi@0 2935 $sort['year'] = strpos($format, 'Y');
danielebarchiesi@0 2936 asort($sort);
danielebarchiesi@0 2937 $order = array_keys($sort);
danielebarchiesi@0 2938
danielebarchiesi@0 2939 // Output multi-selector for date.
danielebarchiesi@0 2940 foreach ($order as $type) {
danielebarchiesi@0 2941 switch ($type) {
danielebarchiesi@0 2942 case 'day':
danielebarchiesi@0 2943 $options = drupal_map_assoc(range(1, 31));
danielebarchiesi@0 2944 $title = t('Day');
danielebarchiesi@0 2945 break;
danielebarchiesi@0 2946
danielebarchiesi@0 2947 case 'month':
danielebarchiesi@0 2948 $options = drupal_map_assoc(range(1, 12), 'map_month');
danielebarchiesi@0 2949 $title = t('Month');
danielebarchiesi@0 2950 break;
danielebarchiesi@0 2951
danielebarchiesi@0 2952 case 'year':
danielebarchiesi@0 2953 $options = drupal_map_assoc(range(1900, 2050));
danielebarchiesi@0 2954 $title = t('Year');
danielebarchiesi@0 2955 break;
danielebarchiesi@0 2956 }
danielebarchiesi@0 2957
danielebarchiesi@0 2958 $element[$type] = array(
danielebarchiesi@0 2959 '#type' => 'select',
danielebarchiesi@0 2960 '#title' => $title,
danielebarchiesi@0 2961 '#title_display' => 'invisible',
danielebarchiesi@0 2962 '#value' => $element['#value'][$type],
danielebarchiesi@0 2963 '#attributes' => $element['#attributes'],
danielebarchiesi@0 2964 '#options' => $options,
danielebarchiesi@0 2965 );
danielebarchiesi@0 2966 }
danielebarchiesi@0 2967
danielebarchiesi@0 2968 return $element;
danielebarchiesi@0 2969 }
danielebarchiesi@0 2970
danielebarchiesi@0 2971 /**
danielebarchiesi@0 2972 * Validates the date type to prevent invalid dates (e.g., February 30, 2006).
danielebarchiesi@0 2973 */
danielebarchiesi@0 2974 function date_validate($element) {
danielebarchiesi@0 2975 if (!checkdate($element['#value']['month'], $element['#value']['day'], $element['#value']['year'])) {
danielebarchiesi@0 2976 form_error($element, t('The specified date is invalid.'));
danielebarchiesi@0 2977 }
danielebarchiesi@0 2978 }
danielebarchiesi@0 2979
danielebarchiesi@0 2980 /**
danielebarchiesi@0 2981 * Helper function for usage with drupal_map_assoc to display month names.
danielebarchiesi@0 2982 */
danielebarchiesi@0 2983 function map_month($month) {
danielebarchiesi@0 2984 $months = &drupal_static(__FUNCTION__, array(
danielebarchiesi@0 2985 1 => 'Jan',
danielebarchiesi@0 2986 2 => 'Feb',
danielebarchiesi@0 2987 3 => 'Mar',
danielebarchiesi@0 2988 4 => 'Apr',
danielebarchiesi@0 2989 5 => 'May',
danielebarchiesi@0 2990 6 => 'Jun',
danielebarchiesi@0 2991 7 => 'Jul',
danielebarchiesi@0 2992 8 => 'Aug',
danielebarchiesi@0 2993 9 => 'Sep',
danielebarchiesi@0 2994 10 => 'Oct',
danielebarchiesi@0 2995 11 => 'Nov',
danielebarchiesi@0 2996 12 => 'Dec',
danielebarchiesi@0 2997 ));
danielebarchiesi@0 2998 return t($months[$month]);
danielebarchiesi@0 2999 }
danielebarchiesi@0 3000
danielebarchiesi@0 3001 /**
danielebarchiesi@0 3002 * Sets the value for a weight element, with zero as a default.
danielebarchiesi@0 3003 */
danielebarchiesi@0 3004 function weight_value(&$form) {
danielebarchiesi@0 3005 if (isset($form['#default_value'])) {
danielebarchiesi@0 3006 $form['#value'] = $form['#default_value'];
danielebarchiesi@0 3007 }
danielebarchiesi@0 3008 else {
danielebarchiesi@0 3009 $form['#value'] = 0;
danielebarchiesi@0 3010 }
danielebarchiesi@0 3011 }
danielebarchiesi@0 3012
danielebarchiesi@0 3013 /**
danielebarchiesi@0 3014 * Expands a radios element into individual radio elements.
danielebarchiesi@0 3015 */
danielebarchiesi@0 3016 function form_process_radios($element) {
danielebarchiesi@0 3017 if (count($element['#options']) > 0) {
danielebarchiesi@0 3018 $weight = 0;
danielebarchiesi@0 3019 foreach ($element['#options'] as $key => $choice) {
danielebarchiesi@0 3020 // Maintain order of options as defined in #options, in case the element
danielebarchiesi@0 3021 // defines custom option sub-elements, but does not define all option
danielebarchiesi@0 3022 // sub-elements.
danielebarchiesi@0 3023 $weight += 0.001;
danielebarchiesi@0 3024
danielebarchiesi@0 3025 $element += array($key => array());
danielebarchiesi@0 3026 // Generate the parents as the autogenerator does, so we will have a
danielebarchiesi@0 3027 // unique id for each radio button.
danielebarchiesi@0 3028 $parents_for_id = array_merge($element['#parents'], array($key));
danielebarchiesi@0 3029 $element[$key] += array(
danielebarchiesi@0 3030 '#type' => 'radio',
danielebarchiesi@0 3031 '#title' => $choice,
danielebarchiesi@0 3032 // The key is sanitized in drupal_attributes() during output from the
danielebarchiesi@0 3033 // theme function.
danielebarchiesi@0 3034 '#return_value' => $key,
danielebarchiesi@0 3035 // Use default or FALSE. A value of FALSE means that the radio button is
danielebarchiesi@0 3036 // not 'checked'.
danielebarchiesi@0 3037 '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : FALSE,
danielebarchiesi@0 3038 '#attributes' => $element['#attributes'],
danielebarchiesi@0 3039 '#parents' => $element['#parents'],
danielebarchiesi@0 3040 '#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
danielebarchiesi@0 3041 '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
danielebarchiesi@0 3042 '#weight' => $weight,
danielebarchiesi@0 3043 );
danielebarchiesi@0 3044 }
danielebarchiesi@0 3045 }
danielebarchiesi@0 3046 return $element;
danielebarchiesi@0 3047 }
danielebarchiesi@0 3048
danielebarchiesi@0 3049 /**
danielebarchiesi@0 3050 * Returns HTML for a checkbox form element.
danielebarchiesi@0 3051 *
danielebarchiesi@0 3052 * @param $variables
danielebarchiesi@0 3053 * An associative array containing:
danielebarchiesi@0 3054 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3055 * Properties used: #title, #value, #return_value, #description, #required,
danielebarchiesi@0 3056 * #attributes, #checked.
danielebarchiesi@0 3057 *
danielebarchiesi@0 3058 * @ingroup themeable
danielebarchiesi@0 3059 */
danielebarchiesi@0 3060 function theme_checkbox($variables) {
danielebarchiesi@0 3061 $element = $variables['element'];
danielebarchiesi@0 3062 $element['#attributes']['type'] = 'checkbox';
danielebarchiesi@0 3063 element_set_attributes($element, array('id', 'name', '#return_value' => 'value'));
danielebarchiesi@0 3064
danielebarchiesi@0 3065 // Unchecked checkbox has #value of integer 0.
danielebarchiesi@0 3066 if (!empty($element['#checked'])) {
danielebarchiesi@0 3067 $element['#attributes']['checked'] = 'checked';
danielebarchiesi@0 3068 }
danielebarchiesi@0 3069 _form_set_class($element, array('form-checkbox'));
danielebarchiesi@0 3070
danielebarchiesi@0 3071 return '<input' . drupal_attributes($element['#attributes']) . ' />';
danielebarchiesi@0 3072 }
danielebarchiesi@0 3073
danielebarchiesi@0 3074 /**
danielebarchiesi@0 3075 * Returns HTML for a set of checkbox form elements.
danielebarchiesi@0 3076 *
danielebarchiesi@0 3077 * @param $variables
danielebarchiesi@0 3078 * An associative array containing:
danielebarchiesi@0 3079 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3080 * Properties used: #children, #attributes.
danielebarchiesi@0 3081 *
danielebarchiesi@0 3082 * @ingroup themeable
danielebarchiesi@0 3083 */
danielebarchiesi@0 3084 function theme_checkboxes($variables) {
danielebarchiesi@0 3085 $element = $variables['element'];
danielebarchiesi@0 3086 $attributes = array();
danielebarchiesi@0 3087 if (isset($element['#id'])) {
danielebarchiesi@0 3088 $attributes['id'] = $element['#id'];
danielebarchiesi@0 3089 }
danielebarchiesi@0 3090 $attributes['class'][] = 'form-checkboxes';
danielebarchiesi@0 3091 if (!empty($element['#attributes']['class'])) {
danielebarchiesi@0 3092 $attributes['class'] = array_merge($attributes['class'], $element['#attributes']['class']);
danielebarchiesi@0 3093 }
danielebarchiesi@0 3094 if (isset($element['#attributes']['title'])) {
danielebarchiesi@0 3095 $attributes['title'] = $element['#attributes']['title'];
danielebarchiesi@0 3096 }
danielebarchiesi@0 3097 return '<div' . drupal_attributes($attributes) . '>' . (!empty($element['#children']) ? $element['#children'] : '') . '</div>';
danielebarchiesi@0 3098 }
danielebarchiesi@0 3099
danielebarchiesi@0 3100 /**
danielebarchiesi@0 3101 * Adds form element theming to an element if its title or description is set.
danielebarchiesi@0 3102 *
danielebarchiesi@0 3103 * This is used as a pre render function for checkboxes and radios.
danielebarchiesi@0 3104 */
danielebarchiesi@0 3105 function form_pre_render_conditional_form_element($element) {
danielebarchiesi@0 3106 $t = get_t();
danielebarchiesi@0 3107 // Set the element's title attribute to show #title as a tooltip, if needed.
danielebarchiesi@0 3108 if (isset($element['#title']) && $element['#title_display'] == 'attribute') {
danielebarchiesi@0 3109 $element['#attributes']['title'] = $element['#title'];
danielebarchiesi@0 3110 if (!empty($element['#required'])) {
danielebarchiesi@0 3111 // Append an indication that this field is required.
danielebarchiesi@0 3112 $element['#attributes']['title'] .= ' (' . $t('Required') . ')';
danielebarchiesi@0 3113 }
danielebarchiesi@0 3114 }
danielebarchiesi@0 3115
danielebarchiesi@0 3116 if (isset($element['#title']) || isset($element['#description'])) {
danielebarchiesi@0 3117 $element['#theme_wrappers'][] = 'form_element';
danielebarchiesi@0 3118 }
danielebarchiesi@0 3119 return $element;
danielebarchiesi@0 3120 }
danielebarchiesi@0 3121
danielebarchiesi@0 3122 /**
danielebarchiesi@0 3123 * Sets the #checked property of a checkbox element.
danielebarchiesi@0 3124 */
danielebarchiesi@0 3125 function form_process_checkbox($element, $form_state) {
danielebarchiesi@0 3126 $value = $element['#value'];
danielebarchiesi@0 3127 $return_value = $element['#return_value'];
danielebarchiesi@0 3128 // On form submission, the #value of an available and enabled checked
danielebarchiesi@0 3129 // checkbox is #return_value, and the #value of an available and enabled
danielebarchiesi@0 3130 // unchecked checkbox is integer 0. On not submitted forms, and for
danielebarchiesi@0 3131 // checkboxes with #access=FALSE or #disabled=TRUE, the #value is
danielebarchiesi@0 3132 // #default_value (integer 0 if #default_value is NULL). Most of the time,
danielebarchiesi@0 3133 // a string comparison of #value and #return_value is sufficient for
danielebarchiesi@0 3134 // determining the "checked" state, but a value of TRUE always means checked
danielebarchiesi@0 3135 // (even if #return_value is 'foo'), and a value of FALSE or integer 0 always
danielebarchiesi@0 3136 // means unchecked (even if #return_value is '' or '0').
danielebarchiesi@0 3137 if ($value === TRUE || $value === FALSE || $value === 0) {
danielebarchiesi@0 3138 $element['#checked'] = (bool) $value;
danielebarchiesi@0 3139 }
danielebarchiesi@0 3140 else {
danielebarchiesi@0 3141 // Compare as strings, so that 15 is not considered equal to '15foo', but 1
danielebarchiesi@0 3142 // is considered equal to '1'. This cast does not imply that either #value
danielebarchiesi@0 3143 // or #return_value is expected to be a string.
danielebarchiesi@0 3144 $element['#checked'] = ((string) $value === (string) $return_value);
danielebarchiesi@0 3145 }
danielebarchiesi@0 3146 return $element;
danielebarchiesi@0 3147 }
danielebarchiesi@0 3148
danielebarchiesi@0 3149 /**
danielebarchiesi@0 3150 * Processes a checkboxes form element.
danielebarchiesi@0 3151 */
danielebarchiesi@0 3152 function form_process_checkboxes($element) {
danielebarchiesi@0 3153 $value = is_array($element['#value']) ? $element['#value'] : array();
danielebarchiesi@0 3154 $element['#tree'] = TRUE;
danielebarchiesi@0 3155 if (count($element['#options']) > 0) {
danielebarchiesi@0 3156 if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
danielebarchiesi@0 3157 $element['#default_value'] = array();
danielebarchiesi@0 3158 }
danielebarchiesi@0 3159 $weight = 0;
danielebarchiesi@0 3160 foreach ($element['#options'] as $key => $choice) {
danielebarchiesi@0 3161 // Integer 0 is not a valid #return_value, so use '0' instead.
danielebarchiesi@0 3162 // @see form_type_checkbox_value().
danielebarchiesi@0 3163 // @todo For Drupal 8, cast all integer keys to strings for consistency
danielebarchiesi@0 3164 // with form_process_radios().
danielebarchiesi@0 3165 if ($key === 0) {
danielebarchiesi@0 3166 $key = '0';
danielebarchiesi@0 3167 }
danielebarchiesi@0 3168 // Maintain order of options as defined in #options, in case the element
danielebarchiesi@0 3169 // defines custom option sub-elements, but does not define all option
danielebarchiesi@0 3170 // sub-elements.
danielebarchiesi@0 3171 $weight += 0.001;
danielebarchiesi@0 3172
danielebarchiesi@0 3173 $element += array($key => array());
danielebarchiesi@0 3174 $element[$key] += array(
danielebarchiesi@0 3175 '#type' => 'checkbox',
danielebarchiesi@0 3176 '#title' => $choice,
danielebarchiesi@0 3177 '#return_value' => $key,
danielebarchiesi@0 3178 '#default_value' => isset($value[$key]) ? $key : NULL,
danielebarchiesi@0 3179 '#attributes' => $element['#attributes'],
danielebarchiesi@0 3180 '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
danielebarchiesi@0 3181 '#weight' => $weight,
danielebarchiesi@0 3182 );
danielebarchiesi@0 3183 }
danielebarchiesi@0 3184 }
danielebarchiesi@0 3185 return $element;
danielebarchiesi@0 3186 }
danielebarchiesi@0 3187
danielebarchiesi@0 3188 /**
danielebarchiesi@0 3189 * Processes a form actions container element.
danielebarchiesi@0 3190 *
danielebarchiesi@0 3191 * @param $element
danielebarchiesi@0 3192 * An associative array containing the properties and children of the
danielebarchiesi@0 3193 * form actions container.
danielebarchiesi@0 3194 * @param $form_state
danielebarchiesi@0 3195 * The $form_state array for the form this element belongs to.
danielebarchiesi@0 3196 *
danielebarchiesi@0 3197 * @return
danielebarchiesi@0 3198 * The processed element.
danielebarchiesi@0 3199 */
danielebarchiesi@0 3200 function form_process_actions($element, &$form_state) {
danielebarchiesi@0 3201 $element['#attributes']['class'][] = 'form-actions';
danielebarchiesi@0 3202 return $element;
danielebarchiesi@0 3203 }
danielebarchiesi@0 3204
danielebarchiesi@0 3205 /**
danielebarchiesi@0 3206 * Processes a container element.
danielebarchiesi@0 3207 *
danielebarchiesi@0 3208 * @param $element
danielebarchiesi@0 3209 * An associative array containing the properties and children of the
danielebarchiesi@0 3210 * container.
danielebarchiesi@0 3211 * @param $form_state
danielebarchiesi@0 3212 * The $form_state array for the form this element belongs to.
danielebarchiesi@0 3213 *
danielebarchiesi@0 3214 * @return
danielebarchiesi@0 3215 * The processed element.
danielebarchiesi@0 3216 */
danielebarchiesi@0 3217 function form_process_container($element, &$form_state) {
danielebarchiesi@0 3218 // Generate the ID of the element if it's not explicitly given.
danielebarchiesi@0 3219 if (!isset($element['#id'])) {
danielebarchiesi@0 3220 $element['#id'] = drupal_html_id(implode('-', $element['#parents']) . '-wrapper');
danielebarchiesi@0 3221 }
danielebarchiesi@0 3222 return $element;
danielebarchiesi@0 3223 }
danielebarchiesi@0 3224
danielebarchiesi@0 3225 /**
danielebarchiesi@0 3226 * Returns HTML to wrap child elements in a container.
danielebarchiesi@0 3227 *
danielebarchiesi@0 3228 * Used for grouped form items. Can also be used as a #theme_wrapper for any
danielebarchiesi@0 3229 * renderable element, to surround it with a <div> and add attributes such as
danielebarchiesi@0 3230 * classes or an HTML id.
danielebarchiesi@0 3231 *
danielebarchiesi@0 3232 * @param $variables
danielebarchiesi@0 3233 * An associative array containing:
danielebarchiesi@0 3234 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3235 * Properties used: #id, #attributes, #children.
danielebarchiesi@0 3236 *
danielebarchiesi@0 3237 * @ingroup themeable
danielebarchiesi@0 3238 */
danielebarchiesi@0 3239 function theme_container($variables) {
danielebarchiesi@0 3240 $element = $variables['element'];
danielebarchiesi@0 3241
danielebarchiesi@0 3242 // Special handling for form elements.
danielebarchiesi@0 3243 if (isset($element['#array_parents'])) {
danielebarchiesi@0 3244 // Assign an html ID.
danielebarchiesi@0 3245 if (!isset($element['#attributes']['id'])) {
danielebarchiesi@0 3246 $element['#attributes']['id'] = $element['#id'];
danielebarchiesi@0 3247 }
danielebarchiesi@0 3248 // Add the 'form-wrapper' class.
danielebarchiesi@0 3249 $element['#attributes']['class'][] = 'form-wrapper';
danielebarchiesi@0 3250 }
danielebarchiesi@0 3251
danielebarchiesi@0 3252 return '<div' . drupal_attributes($element['#attributes']) . '>' . $element['#children'] . '</div>';
danielebarchiesi@0 3253 }
danielebarchiesi@0 3254
danielebarchiesi@0 3255 /**
danielebarchiesi@0 3256 * Returns HTML for a table with radio buttons or checkboxes.
danielebarchiesi@0 3257 *
danielebarchiesi@0 3258 * @param $variables
danielebarchiesi@0 3259 * An associative array containing:
danielebarchiesi@0 3260 * - element: An associative array containing the properties and children of
danielebarchiesi@0 3261 * the tableselect element. Properties used: #header, #options, #empty,
danielebarchiesi@0 3262 * and #js_select. The #options property is an array of selection options;
danielebarchiesi@0 3263 * each array element of #options is an array of properties. These
danielebarchiesi@0 3264 * properties can include #attributes, which is added to the
danielebarchiesi@0 3265 * table row's HTML attributes; see theme_table(). An example of per-row
danielebarchiesi@0 3266 * options:
danielebarchiesi@0 3267 * @code
danielebarchiesi@0 3268 * $options = array(
danielebarchiesi@0 3269 * array(
danielebarchiesi@0 3270 * 'title' => 'How to Learn Drupal',
danielebarchiesi@0 3271 * 'content_type' => 'Article',
danielebarchiesi@0 3272 * 'status' => 'published',
danielebarchiesi@0 3273 * '#attributes' => array('class' => array('article-row')),
danielebarchiesi@0 3274 * ),
danielebarchiesi@0 3275 * array(
danielebarchiesi@0 3276 * 'title' => 'Privacy Policy',
danielebarchiesi@0 3277 * 'content_type' => 'Page',
danielebarchiesi@0 3278 * 'status' => 'published',
danielebarchiesi@0 3279 * '#attributes' => array('class' => array('page-row')),
danielebarchiesi@0 3280 * ),
danielebarchiesi@0 3281 * );
danielebarchiesi@0 3282 * $header = array(
danielebarchiesi@0 3283 * 'title' => t('Title'),
danielebarchiesi@0 3284 * 'content_type' => t('Content type'),
danielebarchiesi@0 3285 * 'status' => t('Status'),
danielebarchiesi@0 3286 * );
danielebarchiesi@0 3287 * $form['table'] = array(
danielebarchiesi@0 3288 * '#type' => 'tableselect',
danielebarchiesi@0 3289 * '#header' => $header,
danielebarchiesi@0 3290 * '#options' => $options,
danielebarchiesi@0 3291 * '#empty' => t('No content available.'),
danielebarchiesi@0 3292 * );
danielebarchiesi@0 3293 * @endcode
danielebarchiesi@0 3294 *
danielebarchiesi@0 3295 * @ingroup themeable
danielebarchiesi@0 3296 */
danielebarchiesi@0 3297 function theme_tableselect($variables) {
danielebarchiesi@0 3298 $element = $variables['element'];
danielebarchiesi@0 3299 $rows = array();
danielebarchiesi@0 3300 $header = $element['#header'];
danielebarchiesi@0 3301 if (!empty($element['#options'])) {
danielebarchiesi@0 3302 // Generate a table row for each selectable item in #options.
danielebarchiesi@0 3303 foreach (element_children($element) as $key) {
danielebarchiesi@0 3304 $row = array();
danielebarchiesi@0 3305
danielebarchiesi@0 3306 $row['data'] = array();
danielebarchiesi@0 3307 if (isset($element['#options'][$key]['#attributes'])) {
danielebarchiesi@0 3308 $row += $element['#options'][$key]['#attributes'];
danielebarchiesi@0 3309 }
danielebarchiesi@0 3310 // Render the checkbox / radio element.
danielebarchiesi@0 3311 $row['data'][] = drupal_render($element[$key]);
danielebarchiesi@0 3312
danielebarchiesi@0 3313 // As theme_table only maps header and row columns by order, create the
danielebarchiesi@0 3314 // correct order by iterating over the header fields.
danielebarchiesi@0 3315 foreach ($element['#header'] as $fieldname => $title) {
danielebarchiesi@0 3316 $row['data'][] = $element['#options'][$key][$fieldname];
danielebarchiesi@0 3317 }
danielebarchiesi@0 3318 $rows[] = $row;
danielebarchiesi@0 3319 }
danielebarchiesi@0 3320 // Add an empty header or a "Select all" checkbox to provide room for the
danielebarchiesi@0 3321 // checkboxes/radios in the first table column.
danielebarchiesi@0 3322 if ($element['#js_select']) {
danielebarchiesi@0 3323 // Add a "Select all" checkbox.
danielebarchiesi@0 3324 drupal_add_js('misc/tableselect.js');
danielebarchiesi@0 3325 array_unshift($header, array('class' => array('select-all')));
danielebarchiesi@0 3326 }
danielebarchiesi@0 3327 else {
danielebarchiesi@0 3328 // Add an empty header when radio buttons are displayed or a "Select all"
danielebarchiesi@0 3329 // checkbox is not desired.
danielebarchiesi@0 3330 array_unshift($header, '');
danielebarchiesi@0 3331 }
danielebarchiesi@0 3332 }
danielebarchiesi@0 3333 return theme('table', array('header' => $header, 'rows' => $rows, 'empty' => $element['#empty'], 'attributes' => $element['#attributes']));
danielebarchiesi@0 3334 }
danielebarchiesi@0 3335
danielebarchiesi@0 3336 /**
danielebarchiesi@0 3337 * Creates checkbox or radio elements to populate a tableselect table.
danielebarchiesi@0 3338 *
danielebarchiesi@0 3339 * @param $element
danielebarchiesi@0 3340 * An associative array containing the properties and children of the
danielebarchiesi@0 3341 * tableselect element.
danielebarchiesi@0 3342 *
danielebarchiesi@0 3343 * @return
danielebarchiesi@0 3344 * The processed element.
danielebarchiesi@0 3345 */
danielebarchiesi@0 3346 function form_process_tableselect($element) {
danielebarchiesi@0 3347
danielebarchiesi@0 3348 if ($element['#multiple']) {
danielebarchiesi@0 3349 $value = is_array($element['#value']) ? $element['#value'] : array();
danielebarchiesi@0 3350 }
danielebarchiesi@0 3351 else {
danielebarchiesi@0 3352 // Advanced selection behavior makes no sense for radios.
danielebarchiesi@0 3353 $element['#js_select'] = FALSE;
danielebarchiesi@0 3354 }
danielebarchiesi@0 3355
danielebarchiesi@0 3356 $element['#tree'] = TRUE;
danielebarchiesi@0 3357
danielebarchiesi@0 3358 if (count($element['#options']) > 0) {
danielebarchiesi@0 3359 if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
danielebarchiesi@0 3360 $element['#default_value'] = array();
danielebarchiesi@0 3361 }
danielebarchiesi@0 3362
danielebarchiesi@0 3363 // Create a checkbox or radio for each item in #options in such a way that
danielebarchiesi@0 3364 // the value of the tableselect element behaves as if it had been of type
danielebarchiesi@0 3365 // checkboxes or radios.
danielebarchiesi@0 3366 foreach ($element['#options'] as $key => $choice) {
danielebarchiesi@0 3367 // Do not overwrite manually created children.
danielebarchiesi@0 3368 if (!isset($element[$key])) {
danielebarchiesi@0 3369 if ($element['#multiple']) {
danielebarchiesi@0 3370 $title = '';
danielebarchiesi@0 3371 if (!empty($element['#options'][$key]['title']['data']['#title'])) {
danielebarchiesi@0 3372 $title = t('Update @title', array(
danielebarchiesi@0 3373 '@title' => $element['#options'][$key]['title']['data']['#title'],
danielebarchiesi@0 3374 ));
danielebarchiesi@0 3375 }
danielebarchiesi@0 3376 $element[$key] = array(
danielebarchiesi@0 3377 '#type' => 'checkbox',
danielebarchiesi@0 3378 '#title' => $title,
danielebarchiesi@0 3379 '#title_display' => 'invisible',
danielebarchiesi@0 3380 '#return_value' => $key,
danielebarchiesi@0 3381 '#default_value' => isset($value[$key]) ? $key : NULL,
danielebarchiesi@0 3382 '#attributes' => $element['#attributes'],
danielebarchiesi@0 3383 );
danielebarchiesi@0 3384 }
danielebarchiesi@0 3385 else {
danielebarchiesi@0 3386 // Generate the parents as the autogenerator does, so we will have a
danielebarchiesi@0 3387 // unique id for each radio button.
danielebarchiesi@0 3388 $parents_for_id = array_merge($element['#parents'], array($key));
danielebarchiesi@0 3389 $element[$key] = array(
danielebarchiesi@0 3390 '#type' => 'radio',
danielebarchiesi@0 3391 '#title' => '',
danielebarchiesi@0 3392 '#return_value' => $key,
danielebarchiesi@0 3393 '#default_value' => ($element['#default_value'] == $key) ? $key : NULL,
danielebarchiesi@0 3394 '#attributes' => $element['#attributes'],
danielebarchiesi@0 3395 '#parents' => $element['#parents'],
danielebarchiesi@0 3396 '#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
danielebarchiesi@0 3397 '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
danielebarchiesi@0 3398 );
danielebarchiesi@0 3399 }
danielebarchiesi@0 3400 if (isset($element['#options'][$key]['#weight'])) {
danielebarchiesi@0 3401 $element[$key]['#weight'] = $element['#options'][$key]['#weight'];
danielebarchiesi@0 3402 }
danielebarchiesi@0 3403 }
danielebarchiesi@0 3404 }
danielebarchiesi@0 3405 }
danielebarchiesi@0 3406 else {
danielebarchiesi@0 3407 $element['#value'] = array();
danielebarchiesi@0 3408 }
danielebarchiesi@0 3409 return $element;
danielebarchiesi@0 3410 }
danielebarchiesi@0 3411
danielebarchiesi@0 3412 /**
danielebarchiesi@0 3413 * Processes a machine-readable name form element.
danielebarchiesi@0 3414 *
danielebarchiesi@0 3415 * @param $element
danielebarchiesi@0 3416 * The form element to process. Properties used:
danielebarchiesi@0 3417 * - #machine_name: An associative array containing:
danielebarchiesi@0 3418 * - exists: A function name to invoke for checking whether a submitted
danielebarchiesi@0 3419 * machine name value already exists. The submitted value is passed as
danielebarchiesi@0 3420 * argument. In most cases, an existing API or menu argument loader
danielebarchiesi@0 3421 * function can be re-used. The callback is only invoked, if the submitted
danielebarchiesi@0 3422 * value differs from the element's #default_value.
danielebarchiesi@0 3423 * - source: (optional) The #array_parents of the form element containing
danielebarchiesi@0 3424 * the human-readable name (i.e., as contained in the $form structure) to
danielebarchiesi@0 3425 * use as source for the machine name. Defaults to array('name').
danielebarchiesi@0 3426 * - label: (optional) A text to display as label for the machine name value
danielebarchiesi@0 3427 * after the human-readable name form element. Defaults to "Machine name".
danielebarchiesi@0 3428 * - replace_pattern: (optional) A regular expression (without delimiters)
danielebarchiesi@0 3429 * matching disallowed characters in the machine name. Defaults to
danielebarchiesi@0 3430 * '[^a-z0-9_]+'.
danielebarchiesi@0 3431 * - replace: (optional) A character to replace disallowed characters in the
danielebarchiesi@0 3432 * machine name via JavaScript. Defaults to '_' (underscore). When using a
danielebarchiesi@0 3433 * different character, 'replace_pattern' needs to be set accordingly.
danielebarchiesi@0 3434 * - error: (optional) A custom form error message string to show, if the
danielebarchiesi@0 3435 * machine name contains disallowed characters.
danielebarchiesi@0 3436 * - standalone: (optional) Whether the live preview should stay in its own
danielebarchiesi@0 3437 * form element rather than in the suffix of the source element. Defaults
danielebarchiesi@0 3438 * to FALSE.
danielebarchiesi@0 3439 * - #maxlength: (optional) Should be set to the maximum allowed length of the
danielebarchiesi@0 3440 * machine name. Defaults to 64.
danielebarchiesi@0 3441 * - #disabled: (optional) Should be set to TRUE in case an existing machine
danielebarchiesi@0 3442 * name must not be changed after initial creation.
danielebarchiesi@0 3443 */
danielebarchiesi@0 3444 function form_process_machine_name($element, &$form_state) {
danielebarchiesi@0 3445 // Apply default form element properties.
danielebarchiesi@0 3446 $element += array(
danielebarchiesi@0 3447 '#title' => t('Machine-readable name'),
danielebarchiesi@0 3448 '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
danielebarchiesi@0 3449 '#machine_name' => array(),
danielebarchiesi@0 3450 '#field_prefix' => '',
danielebarchiesi@0 3451 '#field_suffix' => '',
danielebarchiesi@0 3452 '#suffix' => '',
danielebarchiesi@0 3453 );
danielebarchiesi@0 3454 // A form element that only wants to set one #machine_name property (usually
danielebarchiesi@0 3455 // 'source' only) would leave all other properties undefined, if the defaults
danielebarchiesi@0 3456 // were defined in hook_element_info(). Therefore, we apply the defaults here.
danielebarchiesi@0 3457 $element['#machine_name'] += array(
danielebarchiesi@0 3458 'source' => array('name'),
danielebarchiesi@0 3459 'target' => '#' . $element['#id'],
danielebarchiesi@0 3460 'label' => t('Machine name'),
danielebarchiesi@0 3461 'replace_pattern' => '[^a-z0-9_]+',
danielebarchiesi@0 3462 'replace' => '_',
danielebarchiesi@0 3463 'standalone' => FALSE,
danielebarchiesi@0 3464 'field_prefix' => $element['#field_prefix'],
danielebarchiesi@0 3465 'field_suffix' => $element['#field_suffix'],
danielebarchiesi@0 3466 );
danielebarchiesi@0 3467
danielebarchiesi@0 3468 // By default, machine names are restricted to Latin alphanumeric characters.
danielebarchiesi@0 3469 // So, default to LTR directionality.
danielebarchiesi@0 3470 if (!isset($element['#attributes'])) {
danielebarchiesi@0 3471 $element['#attributes'] = array();
danielebarchiesi@0 3472 }
danielebarchiesi@0 3473 $element['#attributes'] += array('dir' => 'ltr');
danielebarchiesi@0 3474
danielebarchiesi@0 3475 // The source element defaults to array('name'), but may have been overidden.
danielebarchiesi@0 3476 if (empty($element['#machine_name']['source'])) {
danielebarchiesi@0 3477 return $element;
danielebarchiesi@0 3478 }
danielebarchiesi@0 3479
danielebarchiesi@0 3480 // Retrieve the form element containing the human-readable name from the
danielebarchiesi@0 3481 // complete form in $form_state. By reference, because we may need to append
danielebarchiesi@0 3482 // a #field_suffix that will hold the live preview.
danielebarchiesi@0 3483 $key_exists = NULL;
danielebarchiesi@0 3484 $source = drupal_array_get_nested_value($form_state['complete form'], $element['#machine_name']['source'], $key_exists);
danielebarchiesi@0 3485 if (!$key_exists) {
danielebarchiesi@0 3486 return $element;
danielebarchiesi@0 3487 }
danielebarchiesi@0 3488
danielebarchiesi@0 3489 $suffix_id = $source['#id'] . '-machine-name-suffix';
danielebarchiesi@0 3490 $element['#machine_name']['suffix'] = '#' . $suffix_id;
danielebarchiesi@0 3491
danielebarchiesi@0 3492 if ($element['#machine_name']['standalone']) {
danielebarchiesi@0 3493 $element['#suffix'] .= ' <small id="' . $suffix_id . '">&nbsp;</small>';
danielebarchiesi@0 3494 }
danielebarchiesi@0 3495 else {
danielebarchiesi@0 3496 // Append a field suffix to the source form element, which will contain
danielebarchiesi@0 3497 // the live preview of the machine name.
danielebarchiesi@0 3498 $source += array('#field_suffix' => '');
danielebarchiesi@0 3499 $source['#field_suffix'] .= ' <small id="' . $suffix_id . '">&nbsp;</small>';
danielebarchiesi@0 3500
danielebarchiesi@0 3501 $parents = array_merge($element['#machine_name']['source'], array('#field_suffix'));
danielebarchiesi@0 3502 drupal_array_set_nested_value($form_state['complete form'], $parents, $source['#field_suffix']);
danielebarchiesi@0 3503 }
danielebarchiesi@0 3504
danielebarchiesi@0 3505 $js_settings = array(
danielebarchiesi@0 3506 'type' => 'setting',
danielebarchiesi@0 3507 'data' => array(
danielebarchiesi@0 3508 'machineName' => array(
danielebarchiesi@0 3509 '#' . $source['#id'] => $element['#machine_name'],
danielebarchiesi@0 3510 ),
danielebarchiesi@0 3511 ),
danielebarchiesi@0 3512 );
danielebarchiesi@0 3513 $element['#attached']['js'][] = 'misc/machine-name.js';
danielebarchiesi@0 3514 $element['#attached']['js'][] = $js_settings;
danielebarchiesi@0 3515
danielebarchiesi@0 3516 return $element;
danielebarchiesi@0 3517 }
danielebarchiesi@0 3518
danielebarchiesi@0 3519 /**
danielebarchiesi@0 3520 * Form element validation handler for machine_name elements.
danielebarchiesi@0 3521 *
danielebarchiesi@0 3522 * Note that #maxlength is validated by _form_validate() already.
danielebarchiesi@0 3523 */
danielebarchiesi@0 3524 function form_validate_machine_name(&$element, &$form_state) {
danielebarchiesi@0 3525 // Verify that the machine name not only consists of replacement tokens.
danielebarchiesi@0 3526 if (preg_match('@^' . $element['#machine_name']['replace'] . '+$@', $element['#value'])) {
danielebarchiesi@0 3527 form_error($element, t('The machine-readable name must contain unique characters.'));
danielebarchiesi@0 3528 }
danielebarchiesi@0 3529
danielebarchiesi@0 3530 // Verify that the machine name contains no disallowed characters.
danielebarchiesi@0 3531 if (preg_match('@' . $element['#machine_name']['replace_pattern'] . '@', $element['#value'])) {
danielebarchiesi@0 3532 if (!isset($element['#machine_name']['error'])) {
danielebarchiesi@0 3533 // Since a hyphen is the most common alternative replacement character,
danielebarchiesi@0 3534 // a corresponding validation error message is supported here.
danielebarchiesi@0 3535 if ($element['#machine_name']['replace'] == '-') {
danielebarchiesi@0 3536 form_error($element, t('The machine-readable name must contain only lowercase letters, numbers, and hyphens.'));
danielebarchiesi@0 3537 }
danielebarchiesi@0 3538 // Otherwise, we assume the default (underscore).
danielebarchiesi@0 3539 else {
danielebarchiesi@0 3540 form_error($element, t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
danielebarchiesi@0 3541 }
danielebarchiesi@0 3542 }
danielebarchiesi@0 3543 else {
danielebarchiesi@0 3544 form_error($element, $element['#machine_name']['error']);
danielebarchiesi@0 3545 }
danielebarchiesi@0 3546 }
danielebarchiesi@0 3547
danielebarchiesi@0 3548 // Verify that the machine name is unique.
danielebarchiesi@0 3549 if ($element['#default_value'] !== $element['#value']) {
danielebarchiesi@0 3550 $function = $element['#machine_name']['exists'];
danielebarchiesi@0 3551 if ($function($element['#value'], $element, $form_state)) {
danielebarchiesi@0 3552 form_error($element, t('The machine-readable name is already in use. It must be unique.'));
danielebarchiesi@0 3553 }
danielebarchiesi@0 3554 }
danielebarchiesi@0 3555 }
danielebarchiesi@0 3556
danielebarchiesi@0 3557 /**
danielebarchiesi@0 3558 * Arranges fieldsets into groups.
danielebarchiesi@0 3559 *
danielebarchiesi@0 3560 * @param $element
danielebarchiesi@0 3561 * An associative array containing the properties and children of the
danielebarchiesi@0 3562 * fieldset. Note that $element must be taken by reference here, so processed
danielebarchiesi@0 3563 * child elements are taken over into $form_state.
danielebarchiesi@0 3564 * @param $form_state
danielebarchiesi@0 3565 * The $form_state array for the form this fieldset belongs to.
danielebarchiesi@0 3566 *
danielebarchiesi@0 3567 * @return
danielebarchiesi@0 3568 * The processed element.
danielebarchiesi@0 3569 */
danielebarchiesi@0 3570 function form_process_fieldset(&$element, &$form_state) {
danielebarchiesi@0 3571 $parents = implode('][', $element['#parents']);
danielebarchiesi@0 3572
danielebarchiesi@0 3573 // Each fieldset forms a new group. The #type 'vertical_tabs' basically only
danielebarchiesi@0 3574 // injects a new fieldset.
danielebarchiesi@0 3575 $form_state['groups'][$parents]['#group_exists'] = TRUE;
danielebarchiesi@0 3576 $element['#groups'] = &$form_state['groups'];
danielebarchiesi@0 3577
danielebarchiesi@0 3578 // Process vertical tabs group member fieldsets.
danielebarchiesi@0 3579 if (isset($element['#group'])) {
danielebarchiesi@0 3580 // Add this fieldset to the defined group (by reference).
danielebarchiesi@0 3581 $group = $element['#group'];
danielebarchiesi@0 3582 $form_state['groups'][$group][] = &$element;
danielebarchiesi@0 3583 }
danielebarchiesi@0 3584
danielebarchiesi@0 3585 // Contains form element summary functionalities.
danielebarchiesi@0 3586 $element['#attached']['library'][] = array('system', 'drupal.form');
danielebarchiesi@0 3587
danielebarchiesi@0 3588 // The .form-wrapper class is required for #states to treat fieldsets like
danielebarchiesi@0 3589 // containers.
danielebarchiesi@0 3590 if (!isset($element['#attributes']['class'])) {
danielebarchiesi@0 3591 $element['#attributes']['class'] = array();
danielebarchiesi@0 3592 }
danielebarchiesi@0 3593
danielebarchiesi@0 3594 // Collapsible fieldsets
danielebarchiesi@0 3595 if (!empty($element['#collapsible'])) {
danielebarchiesi@0 3596 $element['#attached']['library'][] = array('system', 'drupal.collapse');
danielebarchiesi@0 3597 $element['#attributes']['class'][] = 'collapsible';
danielebarchiesi@0 3598 if (!empty($element['#collapsed'])) {
danielebarchiesi@0 3599 $element['#attributes']['class'][] = 'collapsed';
danielebarchiesi@0 3600 }
danielebarchiesi@0 3601 }
danielebarchiesi@0 3602
danielebarchiesi@0 3603 return $element;
danielebarchiesi@0 3604 }
danielebarchiesi@0 3605
danielebarchiesi@0 3606 /**
danielebarchiesi@0 3607 * Adds members of this group as actual elements for rendering.
danielebarchiesi@0 3608 *
danielebarchiesi@0 3609 * @param $element
danielebarchiesi@0 3610 * An associative array containing the properties and children of the
danielebarchiesi@0 3611 * fieldset.
danielebarchiesi@0 3612 *
danielebarchiesi@0 3613 * @return
danielebarchiesi@0 3614 * The modified element with all group members.
danielebarchiesi@0 3615 */
danielebarchiesi@0 3616 function form_pre_render_fieldset($element) {
danielebarchiesi@0 3617 // Fieldsets may be rendered outside of a Form API context.
danielebarchiesi@0 3618 if (!isset($element['#parents']) || !isset($element['#groups'])) {
danielebarchiesi@0 3619 return $element;
danielebarchiesi@0 3620 }
danielebarchiesi@0 3621 // Inject group member elements belonging to this group.
danielebarchiesi@0 3622 $parents = implode('][', $element['#parents']);
danielebarchiesi@0 3623 $children = element_children($element['#groups'][$parents]);
danielebarchiesi@0 3624 if (!empty($children)) {
danielebarchiesi@0 3625 foreach ($children as $key) {
danielebarchiesi@0 3626 // Break references and indicate that the element should be rendered as
danielebarchiesi@0 3627 // group member.
danielebarchiesi@0 3628 $child = (array) $element['#groups'][$parents][$key];
danielebarchiesi@0 3629 $child['#group_fieldset'] = TRUE;
danielebarchiesi@0 3630 // Inject the element as new child element.
danielebarchiesi@0 3631 $element[] = $child;
danielebarchiesi@0 3632
danielebarchiesi@0 3633 $sort = TRUE;
danielebarchiesi@0 3634 }
danielebarchiesi@0 3635 // Re-sort the element's children if we injected group member elements.
danielebarchiesi@0 3636 if (isset($sort)) {
danielebarchiesi@0 3637 $element['#sorted'] = FALSE;
danielebarchiesi@0 3638 }
danielebarchiesi@0 3639 }
danielebarchiesi@0 3640
danielebarchiesi@0 3641 if (isset($element['#group'])) {
danielebarchiesi@0 3642 $group = $element['#group'];
danielebarchiesi@0 3643 // If this element belongs to a group, but the group-holding element does
danielebarchiesi@0 3644 // not exist, we need to render it (at its original location).
danielebarchiesi@0 3645 if (!isset($element['#groups'][$group]['#group_exists'])) {
danielebarchiesi@0 3646 // Intentionally empty to clarify the flow; we simply return $element.
danielebarchiesi@0 3647 }
danielebarchiesi@0 3648 // If we injected this element into the group, then we want to render it.
danielebarchiesi@0 3649 elseif (!empty($element['#group_fieldset'])) {
danielebarchiesi@0 3650 // Intentionally empty to clarify the flow; we simply return $element.
danielebarchiesi@0 3651 }
danielebarchiesi@0 3652 // Otherwise, this element belongs to a group and the group exists, so we do
danielebarchiesi@0 3653 // not render it.
danielebarchiesi@0 3654 elseif (element_children($element['#groups'][$group])) {
danielebarchiesi@0 3655 $element['#printed'] = TRUE;
danielebarchiesi@0 3656 }
danielebarchiesi@0 3657 }
danielebarchiesi@0 3658
danielebarchiesi@0 3659 return $element;
danielebarchiesi@0 3660 }
danielebarchiesi@0 3661
danielebarchiesi@0 3662 /**
danielebarchiesi@0 3663 * Creates a group formatted as vertical tabs.
danielebarchiesi@0 3664 *
danielebarchiesi@0 3665 * @param $element
danielebarchiesi@0 3666 * An associative array containing the properties and children of the
danielebarchiesi@0 3667 * fieldset.
danielebarchiesi@0 3668 * @param $form_state
danielebarchiesi@0 3669 * The $form_state array for the form this vertical tab widget belongs to.
danielebarchiesi@0 3670 *
danielebarchiesi@0 3671 * @return
danielebarchiesi@0 3672 * The processed element.
danielebarchiesi@0 3673 */
danielebarchiesi@0 3674 function form_process_vertical_tabs($element, &$form_state) {
danielebarchiesi@0 3675 // Inject a new fieldset as child, so that form_process_fieldset() processes
danielebarchiesi@0 3676 // this fieldset like any other fieldset.
danielebarchiesi@0 3677 $element['group'] = array(
danielebarchiesi@0 3678 '#type' => 'fieldset',
danielebarchiesi@0 3679 '#theme_wrappers' => array(),
danielebarchiesi@0 3680 '#parents' => $element['#parents'],
danielebarchiesi@0 3681 );
danielebarchiesi@0 3682
danielebarchiesi@0 3683 // The JavaScript stores the currently selected tab in this hidden
danielebarchiesi@0 3684 // field so that the active tab can be restored the next time the
danielebarchiesi@0 3685 // form is rendered, e.g. on preview pages or when form validation
danielebarchiesi@0 3686 // fails.
danielebarchiesi@0 3687 $name = implode('__', $element['#parents']);
danielebarchiesi@0 3688 if (isset($form_state['values'][$name . '__active_tab'])) {
danielebarchiesi@0 3689 $element['#default_tab'] = $form_state['values'][$name . '__active_tab'];
danielebarchiesi@0 3690 }
danielebarchiesi@0 3691 $element[$name . '__active_tab'] = array(
danielebarchiesi@0 3692 '#type' => 'hidden',
danielebarchiesi@0 3693 '#default_value' => $element['#default_tab'],
danielebarchiesi@0 3694 '#attributes' => array('class' => array('vertical-tabs-active-tab')),
danielebarchiesi@0 3695 );
danielebarchiesi@0 3696
danielebarchiesi@0 3697 return $element;
danielebarchiesi@0 3698 }
danielebarchiesi@0 3699
danielebarchiesi@0 3700 /**
danielebarchiesi@0 3701 * Returns HTML for an element's children fieldsets as vertical tabs.
danielebarchiesi@0 3702 *
danielebarchiesi@0 3703 * @param $variables
danielebarchiesi@0 3704 * An associative array containing:
danielebarchiesi@0 3705 * - element: An associative array containing the properties and children of
danielebarchiesi@0 3706 * the fieldset. Properties used: #children.
danielebarchiesi@0 3707 *
danielebarchiesi@0 3708 * @ingroup themeable
danielebarchiesi@0 3709 */
danielebarchiesi@0 3710 function theme_vertical_tabs($variables) {
danielebarchiesi@0 3711 $element = $variables['element'];
danielebarchiesi@0 3712 // Add required JavaScript and Stylesheet.
danielebarchiesi@0 3713 drupal_add_library('system', 'drupal.vertical-tabs');
danielebarchiesi@0 3714
danielebarchiesi@0 3715 $output = '<h2 class="element-invisible">' . t('Vertical Tabs') . '</h2>';
danielebarchiesi@0 3716 $output .= '<div class="vertical-tabs-panes">' . $element['#children'] . '</div>';
danielebarchiesi@0 3717 return $output;
danielebarchiesi@0 3718 }
danielebarchiesi@0 3719
danielebarchiesi@0 3720 /**
danielebarchiesi@0 3721 * Returns HTML for a submit button form element.
danielebarchiesi@0 3722 *
danielebarchiesi@0 3723 * @param $variables
danielebarchiesi@0 3724 * An associative array containing:
danielebarchiesi@0 3725 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3726 * Properties used: #attributes, #button_type, #name, #value.
danielebarchiesi@0 3727 *
danielebarchiesi@0 3728 * @ingroup themeable
danielebarchiesi@0 3729 */
danielebarchiesi@0 3730 function theme_submit($variables) {
danielebarchiesi@0 3731 return theme('button', $variables['element']);
danielebarchiesi@0 3732 }
danielebarchiesi@0 3733
danielebarchiesi@0 3734 /**
danielebarchiesi@0 3735 * Returns HTML for a button form element.
danielebarchiesi@0 3736 *
danielebarchiesi@0 3737 * @param $variables
danielebarchiesi@0 3738 * An associative array containing:
danielebarchiesi@0 3739 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3740 * Properties used: #attributes, #button_type, #name, #value.
danielebarchiesi@0 3741 *
danielebarchiesi@0 3742 * @ingroup themeable
danielebarchiesi@0 3743 */
danielebarchiesi@0 3744 function theme_button($variables) {
danielebarchiesi@0 3745 $element = $variables['element'];
danielebarchiesi@0 3746 $element['#attributes']['type'] = 'submit';
danielebarchiesi@0 3747 element_set_attributes($element, array('id', 'name', 'value'));
danielebarchiesi@0 3748
danielebarchiesi@0 3749 $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
danielebarchiesi@0 3750 if (!empty($element['#attributes']['disabled'])) {
danielebarchiesi@0 3751 $element['#attributes']['class'][] = 'form-button-disabled';
danielebarchiesi@0 3752 }
danielebarchiesi@0 3753
danielebarchiesi@0 3754 return '<input' . drupal_attributes($element['#attributes']) . ' />';
danielebarchiesi@0 3755 }
danielebarchiesi@0 3756
danielebarchiesi@0 3757 /**
danielebarchiesi@0 3758 * Returns HTML for an image button form element.
danielebarchiesi@0 3759 *
danielebarchiesi@0 3760 * @param $variables
danielebarchiesi@0 3761 * An associative array containing:
danielebarchiesi@0 3762 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3763 * Properties used: #attributes, #button_type, #name, #value, #title, #src.
danielebarchiesi@0 3764 *
danielebarchiesi@0 3765 * @ingroup themeable
danielebarchiesi@0 3766 */
danielebarchiesi@0 3767 function theme_image_button($variables) {
danielebarchiesi@0 3768 $element = $variables['element'];
danielebarchiesi@0 3769 $element['#attributes']['type'] = 'image';
danielebarchiesi@0 3770 element_set_attributes($element, array('id', 'name', 'value'));
danielebarchiesi@0 3771
danielebarchiesi@0 3772 $element['#attributes']['src'] = file_create_url($element['#src']);
danielebarchiesi@0 3773 if (!empty($element['#title'])) {
danielebarchiesi@0 3774 $element['#attributes']['alt'] = $element['#title'];
danielebarchiesi@0 3775 $element['#attributes']['title'] = $element['#title'];
danielebarchiesi@0 3776 }
danielebarchiesi@0 3777
danielebarchiesi@0 3778 $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
danielebarchiesi@0 3779 if (!empty($element['#attributes']['disabled'])) {
danielebarchiesi@0 3780 $element['#attributes']['class'][] = 'form-button-disabled';
danielebarchiesi@0 3781 }
danielebarchiesi@0 3782
danielebarchiesi@0 3783 return '<input' . drupal_attributes($element['#attributes']) . ' />';
danielebarchiesi@0 3784 }
danielebarchiesi@0 3785
danielebarchiesi@0 3786 /**
danielebarchiesi@0 3787 * Returns HTML for a hidden form element.
danielebarchiesi@0 3788 *
danielebarchiesi@0 3789 * @param $variables
danielebarchiesi@0 3790 * An associative array containing:
danielebarchiesi@0 3791 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3792 * Properties used: #name, #value, #attributes.
danielebarchiesi@0 3793 *
danielebarchiesi@0 3794 * @ingroup themeable
danielebarchiesi@0 3795 */
danielebarchiesi@0 3796 function theme_hidden($variables) {
danielebarchiesi@0 3797 $element = $variables['element'];
danielebarchiesi@0 3798 $element['#attributes']['type'] = 'hidden';
danielebarchiesi@0 3799 element_set_attributes($element, array('name', 'value'));
danielebarchiesi@0 3800 return '<input' . drupal_attributes($element['#attributes']) . " />\n";
danielebarchiesi@0 3801 }
danielebarchiesi@0 3802
danielebarchiesi@0 3803 /**
danielebarchiesi@0 3804 * Returns HTML for a textfield form element.
danielebarchiesi@0 3805 *
danielebarchiesi@0 3806 * @param $variables
danielebarchiesi@0 3807 * An associative array containing:
danielebarchiesi@0 3808 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3809 * Properties used: #title, #value, #description, #size, #maxlength,
danielebarchiesi@0 3810 * #required, #attributes, #autocomplete_path.
danielebarchiesi@0 3811 *
danielebarchiesi@0 3812 * @ingroup themeable
danielebarchiesi@0 3813 */
danielebarchiesi@0 3814 function theme_textfield($variables) {
danielebarchiesi@0 3815 $element = $variables['element'];
danielebarchiesi@0 3816 $element['#attributes']['type'] = 'text';
danielebarchiesi@0 3817 element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
danielebarchiesi@0 3818 _form_set_class($element, array('form-text'));
danielebarchiesi@0 3819
danielebarchiesi@0 3820 $extra = '';
danielebarchiesi@0 3821 if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'])) {
danielebarchiesi@0 3822 drupal_add_library('system', 'drupal.autocomplete');
danielebarchiesi@0 3823 $element['#attributes']['class'][] = 'form-autocomplete';
danielebarchiesi@0 3824
danielebarchiesi@0 3825 $attributes = array();
danielebarchiesi@0 3826 $attributes['type'] = 'hidden';
danielebarchiesi@0 3827 $attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
danielebarchiesi@0 3828 $attributes['value'] = url($element['#autocomplete_path'], array('absolute' => TRUE));
danielebarchiesi@0 3829 $attributes['disabled'] = 'disabled';
danielebarchiesi@0 3830 $attributes['class'][] = 'autocomplete';
danielebarchiesi@0 3831 $extra = '<input' . drupal_attributes($attributes) . ' />';
danielebarchiesi@0 3832 }
danielebarchiesi@0 3833
danielebarchiesi@0 3834 $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
danielebarchiesi@0 3835
danielebarchiesi@0 3836 return $output . $extra;
danielebarchiesi@0 3837 }
danielebarchiesi@0 3838
danielebarchiesi@0 3839 /**
danielebarchiesi@0 3840 * Returns HTML for a form.
danielebarchiesi@0 3841 *
danielebarchiesi@0 3842 * @param $variables
danielebarchiesi@0 3843 * An associative array containing:
danielebarchiesi@0 3844 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3845 * Properties used: #action, #method, #attributes, #children
danielebarchiesi@0 3846 *
danielebarchiesi@0 3847 * @ingroup themeable
danielebarchiesi@0 3848 */
danielebarchiesi@0 3849 function theme_form($variables) {
danielebarchiesi@0 3850 $element = $variables['element'];
danielebarchiesi@0 3851 if (isset($element['#action'])) {
danielebarchiesi@0 3852 $element['#attributes']['action'] = drupal_strip_dangerous_protocols($element['#action']);
danielebarchiesi@0 3853 }
danielebarchiesi@0 3854 element_set_attributes($element, array('method', 'id'));
danielebarchiesi@0 3855 if (empty($element['#attributes']['accept-charset'])) {
danielebarchiesi@0 3856 $element['#attributes']['accept-charset'] = "UTF-8";
danielebarchiesi@0 3857 }
danielebarchiesi@0 3858 // Anonymous DIV to satisfy XHTML compliance.
danielebarchiesi@0 3859 return '<form' . drupal_attributes($element['#attributes']) . '><div>' . $element['#children'] . '</div></form>';
danielebarchiesi@0 3860 }
danielebarchiesi@0 3861
danielebarchiesi@0 3862 /**
danielebarchiesi@0 3863 * Returns HTML for a textarea form element.
danielebarchiesi@0 3864 *
danielebarchiesi@0 3865 * @param $variables
danielebarchiesi@0 3866 * An associative array containing:
danielebarchiesi@0 3867 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3868 * Properties used: #title, #value, #description, #rows, #cols, #required,
danielebarchiesi@0 3869 * #attributes
danielebarchiesi@0 3870 *
danielebarchiesi@0 3871 * @ingroup themeable
danielebarchiesi@0 3872 */
danielebarchiesi@0 3873 function theme_textarea($variables) {
danielebarchiesi@0 3874 $element = $variables['element'];
danielebarchiesi@0 3875 element_set_attributes($element, array('id', 'name', 'cols', 'rows'));
danielebarchiesi@0 3876 _form_set_class($element, array('form-textarea'));
danielebarchiesi@0 3877
danielebarchiesi@0 3878 $wrapper_attributes = array(
danielebarchiesi@0 3879 'class' => array('form-textarea-wrapper'),
danielebarchiesi@0 3880 );
danielebarchiesi@0 3881
danielebarchiesi@0 3882 // Add resizable behavior.
danielebarchiesi@0 3883 if (!empty($element['#resizable'])) {
danielebarchiesi@0 3884 drupal_add_library('system', 'drupal.textarea');
danielebarchiesi@0 3885 $wrapper_attributes['class'][] = 'resizable';
danielebarchiesi@0 3886 }
danielebarchiesi@0 3887
danielebarchiesi@0 3888 $output = '<div' . drupal_attributes($wrapper_attributes) . '>';
danielebarchiesi@0 3889 $output .= '<textarea' . drupal_attributes($element['#attributes']) . '>' . check_plain($element['#value']) . '</textarea>';
danielebarchiesi@0 3890 $output .= '</div>';
danielebarchiesi@0 3891 return $output;
danielebarchiesi@0 3892 }
danielebarchiesi@0 3893
danielebarchiesi@0 3894 /**
danielebarchiesi@0 3895 * Returns HTML for a password form element.
danielebarchiesi@0 3896 *
danielebarchiesi@0 3897 * @param $variables
danielebarchiesi@0 3898 * An associative array containing:
danielebarchiesi@0 3899 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3900 * Properties used: #title, #value, #description, #size, #maxlength,
danielebarchiesi@0 3901 * #required, #attributes.
danielebarchiesi@0 3902 *
danielebarchiesi@0 3903 * @ingroup themeable
danielebarchiesi@0 3904 */
danielebarchiesi@0 3905 function theme_password($variables) {
danielebarchiesi@0 3906 $element = $variables['element'];
danielebarchiesi@0 3907 $element['#attributes']['type'] = 'password';
danielebarchiesi@0 3908 element_set_attributes($element, array('id', 'name', 'size', 'maxlength'));
danielebarchiesi@0 3909 _form_set_class($element, array('form-text'));
danielebarchiesi@0 3910
danielebarchiesi@0 3911 return '<input' . drupal_attributes($element['#attributes']) . ' />';
danielebarchiesi@0 3912 }
danielebarchiesi@0 3913
danielebarchiesi@0 3914 /**
danielebarchiesi@0 3915 * Expands a weight element into a select element.
danielebarchiesi@0 3916 */
danielebarchiesi@0 3917 function form_process_weight($element) {
danielebarchiesi@0 3918 $element['#is_weight'] = TRUE;
danielebarchiesi@0 3919
danielebarchiesi@0 3920 // If the number of options is small enough, use a select field.
danielebarchiesi@0 3921 $max_elements = variable_get('drupal_weight_select_max', DRUPAL_WEIGHT_SELECT_MAX);
danielebarchiesi@0 3922 if ($element['#delta'] <= $max_elements) {
danielebarchiesi@0 3923 $element['#type'] = 'select';
danielebarchiesi@0 3924 for ($n = (-1 * $element['#delta']); $n <= $element['#delta']; $n++) {
danielebarchiesi@0 3925 $weights[$n] = $n;
danielebarchiesi@0 3926 }
danielebarchiesi@0 3927 $element['#options'] = $weights;
danielebarchiesi@0 3928 $element += element_info('select');
danielebarchiesi@0 3929 }
danielebarchiesi@0 3930 // Otherwise, use a text field.
danielebarchiesi@0 3931 else {
danielebarchiesi@0 3932 $element['#type'] = 'textfield';
danielebarchiesi@0 3933 // Use a field big enough to fit most weights.
danielebarchiesi@0 3934 $element['#size'] = 10;
danielebarchiesi@0 3935 $element['#element_validate'] = array('element_validate_integer');
danielebarchiesi@0 3936 $element += element_info('textfield');
danielebarchiesi@0 3937 }
danielebarchiesi@0 3938
danielebarchiesi@0 3939 return $element;
danielebarchiesi@0 3940 }
danielebarchiesi@0 3941
danielebarchiesi@0 3942 /**
danielebarchiesi@0 3943 * Returns HTML for a file upload form element.
danielebarchiesi@0 3944 *
danielebarchiesi@0 3945 * For assistance with handling the uploaded file correctly, see the API
danielebarchiesi@0 3946 * provided by file.inc.
danielebarchiesi@0 3947 *
danielebarchiesi@0 3948 * @param $variables
danielebarchiesi@0 3949 * An associative array containing:
danielebarchiesi@0 3950 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 3951 * Properties used: #title, #name, #size, #description, #required,
danielebarchiesi@0 3952 * #attributes.
danielebarchiesi@0 3953 *
danielebarchiesi@0 3954 * @ingroup themeable
danielebarchiesi@0 3955 */
danielebarchiesi@0 3956 function theme_file($variables) {
danielebarchiesi@0 3957 $element = $variables['element'];
danielebarchiesi@0 3958 $element['#attributes']['type'] = 'file';
danielebarchiesi@0 3959 element_set_attributes($element, array('id', 'name', 'size'));
danielebarchiesi@0 3960 _form_set_class($element, array('form-file'));
danielebarchiesi@0 3961
danielebarchiesi@0 3962 return '<input' . drupal_attributes($element['#attributes']) . ' />';
danielebarchiesi@0 3963 }
danielebarchiesi@0 3964
danielebarchiesi@0 3965 /**
danielebarchiesi@0 3966 * Returns HTML for a form element.
danielebarchiesi@0 3967 *
danielebarchiesi@0 3968 * Each form element is wrapped in a DIV container having the following CSS
danielebarchiesi@0 3969 * classes:
danielebarchiesi@0 3970 * - form-item: Generic for all form elements.
danielebarchiesi@0 3971 * - form-type-#type: The internal element #type.
danielebarchiesi@0 3972 * - form-item-#name: The internal form element #name (usually derived from the
danielebarchiesi@0 3973 * $form structure and set via form_builder()).
danielebarchiesi@0 3974 * - form-disabled: Only set if the form element is #disabled.
danielebarchiesi@0 3975 *
danielebarchiesi@0 3976 * In addition to the element itself, the DIV contains a label for the element
danielebarchiesi@0 3977 * based on the optional #title_display property, and an optional #description.
danielebarchiesi@0 3978 *
danielebarchiesi@0 3979 * The optional #title_display property can have these values:
danielebarchiesi@0 3980 * - before: The label is output before the element. This is the default.
danielebarchiesi@0 3981 * The label includes the #title and the required marker, if #required.
danielebarchiesi@0 3982 * - after: The label is output after the element. For example, this is used
danielebarchiesi@0 3983 * for radio and checkbox #type elements as set in system_element_info().
danielebarchiesi@0 3984 * If the #title is empty but the field is #required, the label will
danielebarchiesi@0 3985 * contain only the required marker.
danielebarchiesi@0 3986 * - invisible: Labels are critical for screen readers to enable them to
danielebarchiesi@0 3987 * properly navigate through forms but can be visually distracting. This
danielebarchiesi@0 3988 * property hides the label for everyone except screen readers.
danielebarchiesi@0 3989 * - attribute: Set the title attribute on the element to create a tooltip
danielebarchiesi@0 3990 * but output no label element. This is supported only for checkboxes
danielebarchiesi@0 3991 * and radios in form_pre_render_conditional_form_element(). It is used
danielebarchiesi@0 3992 * where a visual label is not needed, such as a table of checkboxes where
danielebarchiesi@0 3993 * the row and column provide the context. The tooltip will include the
danielebarchiesi@0 3994 * title and required marker.
danielebarchiesi@0 3995 *
danielebarchiesi@0 3996 * If the #title property is not set, then the label and any required marker
danielebarchiesi@0 3997 * will not be output, regardless of the #title_display or #required values.
danielebarchiesi@0 3998 * This can be useful in cases such as the password_confirm element, which
danielebarchiesi@0 3999 * creates children elements that have their own labels and required markers,
danielebarchiesi@0 4000 * but the parent element should have neither. Use this carefully because a
danielebarchiesi@0 4001 * field without an associated label can cause accessibility challenges.
danielebarchiesi@0 4002 *
danielebarchiesi@0 4003 * @param $variables
danielebarchiesi@0 4004 * An associative array containing:
danielebarchiesi@0 4005 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 4006 * Properties used: #title, #title_display, #description, #id, #required,
danielebarchiesi@0 4007 * #children, #type, #name.
danielebarchiesi@0 4008 *
danielebarchiesi@0 4009 * @ingroup themeable
danielebarchiesi@0 4010 */
danielebarchiesi@0 4011 function theme_form_element($variables) {
danielebarchiesi@0 4012 $element = &$variables['element'];
danielebarchiesi@0 4013
danielebarchiesi@0 4014 // This function is invoked as theme wrapper, but the rendered form element
danielebarchiesi@0 4015 // may not necessarily have been processed by form_builder().
danielebarchiesi@0 4016 $element += array(
danielebarchiesi@0 4017 '#title_display' => 'before',
danielebarchiesi@0 4018 );
danielebarchiesi@0 4019
danielebarchiesi@0 4020 // Add element #id for #type 'item'.
danielebarchiesi@0 4021 if (isset($element['#markup']) && !empty($element['#id'])) {
danielebarchiesi@0 4022 $attributes['id'] = $element['#id'];
danielebarchiesi@0 4023 }
danielebarchiesi@0 4024 // Add element's #type and #name as class to aid with JS/CSS selectors.
danielebarchiesi@0 4025 $attributes['class'] = array('form-item');
danielebarchiesi@0 4026 if (!empty($element['#type'])) {
danielebarchiesi@0 4027 $attributes['class'][] = 'form-type-' . strtr($element['#type'], '_', '-');
danielebarchiesi@0 4028 }
danielebarchiesi@0 4029 if (!empty($element['#name'])) {
danielebarchiesi@0 4030 $attributes['class'][] = 'form-item-' . strtr($element['#name'], array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
danielebarchiesi@0 4031 }
danielebarchiesi@0 4032 // Add a class for disabled elements to facilitate cross-browser styling.
danielebarchiesi@0 4033 if (!empty($element['#attributes']['disabled'])) {
danielebarchiesi@0 4034 $attributes['class'][] = 'form-disabled';
danielebarchiesi@0 4035 }
danielebarchiesi@0 4036 $output = '<div' . drupal_attributes($attributes) . '>' . "\n";
danielebarchiesi@0 4037
danielebarchiesi@0 4038 // If #title is not set, we don't display any label or required marker.
danielebarchiesi@0 4039 if (!isset($element['#title'])) {
danielebarchiesi@0 4040 $element['#title_display'] = 'none';
danielebarchiesi@0 4041 }
danielebarchiesi@0 4042 $prefix = isset($element['#field_prefix']) ? '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ' : '';
danielebarchiesi@0 4043 $suffix = isset($element['#field_suffix']) ? ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>' : '';
danielebarchiesi@0 4044
danielebarchiesi@0 4045 switch ($element['#title_display']) {
danielebarchiesi@0 4046 case 'before':
danielebarchiesi@0 4047 case 'invisible':
danielebarchiesi@0 4048 $output .= ' ' . theme('form_element_label', $variables);
danielebarchiesi@0 4049 $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
danielebarchiesi@0 4050 break;
danielebarchiesi@0 4051
danielebarchiesi@0 4052 case 'after':
danielebarchiesi@0 4053 $output .= ' ' . $prefix . $element['#children'] . $suffix;
danielebarchiesi@0 4054 $output .= ' ' . theme('form_element_label', $variables) . "\n";
danielebarchiesi@0 4055 break;
danielebarchiesi@0 4056
danielebarchiesi@0 4057 case 'none':
danielebarchiesi@0 4058 case 'attribute':
danielebarchiesi@0 4059 // Output no label and no required marker, only the children.
danielebarchiesi@0 4060 $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
danielebarchiesi@0 4061 break;
danielebarchiesi@0 4062 }
danielebarchiesi@0 4063
danielebarchiesi@0 4064 if (!empty($element['#description'])) {
danielebarchiesi@0 4065 $output .= '<div class="description">' . $element['#description'] . "</div>\n";
danielebarchiesi@0 4066 }
danielebarchiesi@0 4067
danielebarchiesi@0 4068 $output .= "</div>\n";
danielebarchiesi@0 4069
danielebarchiesi@0 4070 return $output;
danielebarchiesi@0 4071 }
danielebarchiesi@0 4072
danielebarchiesi@0 4073 /**
danielebarchiesi@0 4074 * Returns HTML for a marker for required form elements.
danielebarchiesi@0 4075 *
danielebarchiesi@0 4076 * @param $variables
danielebarchiesi@0 4077 * An associative array containing:
danielebarchiesi@0 4078 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 4079 *
danielebarchiesi@0 4080 * @ingroup themeable
danielebarchiesi@0 4081 */
danielebarchiesi@0 4082 function theme_form_required_marker($variables) {
danielebarchiesi@0 4083 // This is also used in the installer, pre-database setup.
danielebarchiesi@0 4084 $t = get_t();
danielebarchiesi@0 4085 $attributes = array(
danielebarchiesi@0 4086 'class' => 'form-required',
danielebarchiesi@0 4087 'title' => $t('This field is required.'),
danielebarchiesi@0 4088 );
danielebarchiesi@0 4089 return '<span' . drupal_attributes($attributes) . '>*</span>';
danielebarchiesi@0 4090 }
danielebarchiesi@0 4091
danielebarchiesi@0 4092 /**
danielebarchiesi@0 4093 * Returns HTML for a form element label and required marker.
danielebarchiesi@0 4094 *
danielebarchiesi@0 4095 * Form element labels include the #title and a #required marker. The label is
danielebarchiesi@0 4096 * associated with the element itself by the element #id. Labels may appear
danielebarchiesi@0 4097 * before or after elements, depending on theme_form_element() and
danielebarchiesi@0 4098 * #title_display.
danielebarchiesi@0 4099 *
danielebarchiesi@0 4100 * This function will not be called for elements with no labels, depending on
danielebarchiesi@0 4101 * #title_display. For elements that have an empty #title and are not required,
danielebarchiesi@0 4102 * this function will output no label (''). For required elements that have an
danielebarchiesi@0 4103 * empty #title, this will output the required marker alone within the label.
danielebarchiesi@0 4104 * The label will use the #id to associate the marker with the field that is
danielebarchiesi@0 4105 * required. That is especially important for screenreader users to know
danielebarchiesi@0 4106 * which field is required.
danielebarchiesi@0 4107 *
danielebarchiesi@0 4108 * @param $variables
danielebarchiesi@0 4109 * An associative array containing:
danielebarchiesi@0 4110 * - element: An associative array containing the properties of the element.
danielebarchiesi@0 4111 * Properties used: #required, #title, #id, #value, #description.
danielebarchiesi@0 4112 *
danielebarchiesi@0 4113 * @ingroup themeable
danielebarchiesi@0 4114 */
danielebarchiesi@0 4115 function theme_form_element_label($variables) {
danielebarchiesi@0 4116 $element = $variables['element'];
danielebarchiesi@0 4117 // This is also used in the installer, pre-database setup.
danielebarchiesi@0 4118 $t = get_t();
danielebarchiesi@0 4119
danielebarchiesi@0 4120 // If title and required marker are both empty, output no label.
danielebarchiesi@0 4121 if ((!isset($element['#title']) || $element['#title'] === '') && empty($element['#required'])) {
danielebarchiesi@0 4122 return '';
danielebarchiesi@0 4123 }
danielebarchiesi@0 4124
danielebarchiesi@0 4125 // If the element is required, a required marker is appended to the label.
danielebarchiesi@0 4126 $required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '';
danielebarchiesi@0 4127
danielebarchiesi@0 4128 $title = filter_xss_admin($element['#title']);
danielebarchiesi@0 4129
danielebarchiesi@0 4130 $attributes = array();
danielebarchiesi@0 4131 // Style the label as class option to display inline with the element.
danielebarchiesi@0 4132 if ($element['#title_display'] == 'after') {
danielebarchiesi@0 4133 $attributes['class'] = 'option';
danielebarchiesi@0 4134 }
danielebarchiesi@0 4135 // Show label only to screen readers to avoid disruption in visual flows.
danielebarchiesi@0 4136 elseif ($element['#title_display'] == 'invisible') {
danielebarchiesi@0 4137 $attributes['class'] = 'element-invisible';
danielebarchiesi@0 4138 }
danielebarchiesi@0 4139
danielebarchiesi@0 4140 if (!empty($element['#id'])) {
danielebarchiesi@0 4141 $attributes['for'] = $element['#id'];
danielebarchiesi@0 4142 }
danielebarchiesi@0 4143
danielebarchiesi@0 4144 // The leading whitespace helps visually separate fields from inline labels.
danielebarchiesi@0 4145 return ' <label' . drupal_attributes($attributes) . '>' . $t('!title !required', array('!title' => $title, '!required' => $required)) . "</label>\n";
danielebarchiesi@0 4146 }
danielebarchiesi@0 4147
danielebarchiesi@0 4148 /**
danielebarchiesi@0 4149 * Sets a form element's class attribute.
danielebarchiesi@0 4150 *
danielebarchiesi@0 4151 * Adds 'required' and 'error' classes as needed.
danielebarchiesi@0 4152 *
danielebarchiesi@0 4153 * @param $element
danielebarchiesi@0 4154 * The form element.
danielebarchiesi@0 4155 * @param $name
danielebarchiesi@0 4156 * Array of new class names to be added.
danielebarchiesi@0 4157 */
danielebarchiesi@0 4158 function _form_set_class(&$element, $class = array()) {
danielebarchiesi@0 4159 if (!empty($class)) {
danielebarchiesi@0 4160 if (!isset($element['#attributes']['class'])) {
danielebarchiesi@0 4161 $element['#attributes']['class'] = array();
danielebarchiesi@0 4162 }
danielebarchiesi@0 4163 $element['#attributes']['class'] = array_merge($element['#attributes']['class'], $class);
danielebarchiesi@0 4164 }
danielebarchiesi@0 4165 // This function is invoked from form element theme functions, but the
danielebarchiesi@0 4166 // rendered form element may not necessarily have been processed by
danielebarchiesi@0 4167 // form_builder().
danielebarchiesi@0 4168 if (!empty($element['#required'])) {
danielebarchiesi@0 4169 $element['#attributes']['class'][] = 'required';
danielebarchiesi@0 4170 }
danielebarchiesi@0 4171 if (isset($element['#parents']) && form_get_error($element) !== NULL && !empty($element['#validated'])) {
danielebarchiesi@0 4172 $element['#attributes']['class'][] = 'error';
danielebarchiesi@0 4173 }
danielebarchiesi@0 4174 }
danielebarchiesi@0 4175
danielebarchiesi@0 4176 /**
danielebarchiesi@0 4177 * Form element validation handler for integer elements.
danielebarchiesi@0 4178 */
danielebarchiesi@0 4179 function element_validate_integer($element, &$form_state) {
danielebarchiesi@0 4180 $value = $element['#value'];
danielebarchiesi@0 4181 if ($value !== '' && (!is_numeric($value) || intval($value) != $value)) {
danielebarchiesi@0 4182 form_error($element, t('%name must be an integer.', array('%name' => $element['#title'])));
danielebarchiesi@0 4183 }
danielebarchiesi@0 4184 }
danielebarchiesi@0 4185
danielebarchiesi@0 4186 /**
danielebarchiesi@0 4187 * Form element validation handler for integer elements that must be positive.
danielebarchiesi@0 4188 */
danielebarchiesi@0 4189 function element_validate_integer_positive($element, &$form_state) {
danielebarchiesi@0 4190 $value = $element['#value'];
danielebarchiesi@0 4191 if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value <= 0)) {
danielebarchiesi@0 4192 form_error($element, t('%name must be a positive integer.', array('%name' => $element['#title'])));
danielebarchiesi@0 4193 }
danielebarchiesi@0 4194 }
danielebarchiesi@0 4195
danielebarchiesi@0 4196 /**
danielebarchiesi@0 4197 * Form element validation handler for number elements.
danielebarchiesi@0 4198 */
danielebarchiesi@0 4199 function element_validate_number($element, &$form_state) {
danielebarchiesi@0 4200 $value = $element['#value'];
danielebarchiesi@0 4201 if ($value != '' && !is_numeric($value)) {
danielebarchiesi@0 4202 form_error($element, t('%name must be a number.', array('%name' => $element['#title'])));
danielebarchiesi@0 4203 }
danielebarchiesi@0 4204 }
danielebarchiesi@0 4205
danielebarchiesi@0 4206 /**
danielebarchiesi@0 4207 * @} End of "defgroup form_api".
danielebarchiesi@0 4208 */
danielebarchiesi@0 4209
danielebarchiesi@0 4210 /**
danielebarchiesi@0 4211 * @defgroup batch Batch operations
danielebarchiesi@0 4212 * @{
danielebarchiesi@0 4213 * Creates and processes batch operations.
danielebarchiesi@0 4214 *
danielebarchiesi@0 4215 * Functions allowing forms processing to be spread out over several page
danielebarchiesi@0 4216 * requests, thus ensuring that the processing does not get interrupted
danielebarchiesi@0 4217 * because of a PHP timeout, while allowing the user to receive feedback
danielebarchiesi@0 4218 * on the progress of the ongoing operations.
danielebarchiesi@0 4219 *
danielebarchiesi@0 4220 * The API is primarily designed to integrate nicely with the Form API
danielebarchiesi@0 4221 * workflow, but can also be used by non-Form API scripts (like update.php)
danielebarchiesi@0 4222 * or even simple page callbacks (which should probably be used sparingly).
danielebarchiesi@0 4223 *
danielebarchiesi@0 4224 * Example:
danielebarchiesi@0 4225 * @code
danielebarchiesi@0 4226 * $batch = array(
danielebarchiesi@0 4227 * 'title' => t('Exporting'),
danielebarchiesi@0 4228 * 'operations' => array(
danielebarchiesi@0 4229 * array('my_function_1', array($account->uid, 'story')),
danielebarchiesi@0 4230 * array('my_function_2', array()),
danielebarchiesi@0 4231 * ),
danielebarchiesi@0 4232 * 'finished' => 'my_finished_callback',
danielebarchiesi@0 4233 * 'file' => 'path_to_file_containing_myfunctions',
danielebarchiesi@0 4234 * );
danielebarchiesi@0 4235 * batch_set($batch);
danielebarchiesi@0 4236 * // Only needed if not inside a form _submit handler.
danielebarchiesi@0 4237 * // Setting redirect in batch_process.
danielebarchiesi@0 4238 * batch_process('node/1');
danielebarchiesi@0 4239 * @endcode
danielebarchiesi@0 4240 *
danielebarchiesi@0 4241 * Note: if the batch 'title', 'init_message', 'progress_message', or
danielebarchiesi@0 4242 * 'error_message' could contain any user input, it is the responsibility of
danielebarchiesi@0 4243 * the code calling batch_set() to sanitize them first with a function like
danielebarchiesi@0 4244 * check_plain() or filter_xss(). Furthermore, if the batch operation
danielebarchiesi@0 4245 * returns any user input in the 'results' or 'message' keys of $context,
danielebarchiesi@0 4246 * it must also sanitize them first.
danielebarchiesi@0 4247 *
danielebarchiesi@0 4248 * Sample batch operations:
danielebarchiesi@0 4249 * @code
danielebarchiesi@0 4250 * // Simple and artificial: load a node of a given type for a given user
danielebarchiesi@0 4251 * function my_function_1($uid, $type, &$context) {
danielebarchiesi@0 4252 * // The $context array gathers batch context information about the execution (read),
danielebarchiesi@0 4253 * // as well as 'return values' for the current operation (write)
danielebarchiesi@0 4254 * // The following keys are provided :
danielebarchiesi@0 4255 * // 'results' (read / write): The array of results gathered so far by
danielebarchiesi@0 4256 * // the batch processing, for the current operation to append its own.
danielebarchiesi@0 4257 * // 'message' (write): A text message displayed in the progress page.
danielebarchiesi@0 4258 * // The following keys allow for multi-step operations :
danielebarchiesi@0 4259 * // 'sandbox' (read / write): An array that can be freely used to
danielebarchiesi@0 4260 * // store persistent data between iterations. It is recommended to
danielebarchiesi@0 4261 * // use this instead of $_SESSION, which is unsafe if the user
danielebarchiesi@0 4262 * // continues browsing in a separate window while the batch is processing.
danielebarchiesi@0 4263 * // 'finished' (write): A float number between 0 and 1 informing
danielebarchiesi@0 4264 * // the processing engine of the completion level for the operation.
danielebarchiesi@0 4265 * // 1 (or no value explicitly set) means the operation is finished
danielebarchiesi@0 4266 * // and the batch processing can continue to the next operation.
danielebarchiesi@0 4267 *
danielebarchiesi@0 4268 * $node = node_load(array('uid' => $uid, 'type' => $type));
danielebarchiesi@0 4269 * $context['results'][] = $node->nid . ' : ' . check_plain($node->title);
danielebarchiesi@0 4270 * $context['message'] = check_plain($node->title);
danielebarchiesi@0 4271 * }
danielebarchiesi@0 4272 *
danielebarchiesi@0 4273 * // More advanced example: multi-step operation - load all nodes, five by five
danielebarchiesi@0 4274 * function my_function_2(&$context) {
danielebarchiesi@0 4275 * if (empty($context['sandbox'])) {
danielebarchiesi@0 4276 * $context['sandbox']['progress'] = 0;
danielebarchiesi@0 4277 * $context['sandbox']['current_node'] = 0;
danielebarchiesi@0 4278 * $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
danielebarchiesi@0 4279 * }
danielebarchiesi@0 4280 * $limit = 5;
danielebarchiesi@0 4281 * $result = db_select('node')
danielebarchiesi@0 4282 * ->fields('node', array('nid'))
danielebarchiesi@0 4283 * ->condition('nid', $context['sandbox']['current_node'], '>')
danielebarchiesi@0 4284 * ->orderBy('nid')
danielebarchiesi@0 4285 * ->range(0, $limit)
danielebarchiesi@0 4286 * ->execute();
danielebarchiesi@0 4287 * foreach ($result as $row) {
danielebarchiesi@0 4288 * $node = node_load($row->nid, NULL, TRUE);
danielebarchiesi@0 4289 * $context['results'][] = $node->nid . ' : ' . check_plain($node->title);
danielebarchiesi@0 4290 * $context['sandbox']['progress']++;
danielebarchiesi@0 4291 * $context['sandbox']['current_node'] = $node->nid;
danielebarchiesi@0 4292 * $context['message'] = check_plain($node->title);
danielebarchiesi@0 4293 * }
danielebarchiesi@0 4294 * if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
danielebarchiesi@0 4295 * $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
danielebarchiesi@0 4296 * }
danielebarchiesi@0 4297 * }
danielebarchiesi@0 4298 * @endcode
danielebarchiesi@0 4299 *
danielebarchiesi@0 4300 * Sample 'finished' callback:
danielebarchiesi@0 4301 * @code
danielebarchiesi@0 4302 * function batch_test_finished($success, $results, $operations) {
danielebarchiesi@0 4303 * // The 'success' parameter means no fatal PHP errors were detected. All
danielebarchiesi@0 4304 * // other error management should be handled using 'results'.
danielebarchiesi@0 4305 * if ($success) {
danielebarchiesi@0 4306 * $message = format_plural(count($results), 'One post processed.', '@count posts processed.');
danielebarchiesi@0 4307 * }
danielebarchiesi@0 4308 * else {
danielebarchiesi@0 4309 * $message = t('Finished with an error.');
danielebarchiesi@0 4310 * }
danielebarchiesi@0 4311 * drupal_set_message($message);
danielebarchiesi@0 4312 * // Providing data for the redirected page is done through $_SESSION.
danielebarchiesi@0 4313 * foreach ($results as $result) {
danielebarchiesi@0 4314 * $items[] = t('Loaded node %title.', array('%title' => $result));
danielebarchiesi@0 4315 * }
danielebarchiesi@0 4316 * $_SESSION['my_batch_results'] = $items;
danielebarchiesi@0 4317 * }
danielebarchiesi@0 4318 * @endcode
danielebarchiesi@0 4319 */
danielebarchiesi@0 4320
danielebarchiesi@0 4321 /**
danielebarchiesi@0 4322 * Adds a new batch.
danielebarchiesi@0 4323 *
danielebarchiesi@0 4324 * Batch operations are added as new batch sets. Batch sets are used to spread
danielebarchiesi@0 4325 * processing (primarily, but not exclusively, forms processing) over several
danielebarchiesi@0 4326 * page requests. This helps to ensure that the processing is not interrupted
danielebarchiesi@0 4327 * due to PHP timeouts, while users are still able to receive feedback on the
danielebarchiesi@0 4328 * progress of the ongoing operations. Combining related operations into
danielebarchiesi@0 4329 * distinct batch sets provides clean code independence for each batch set,
danielebarchiesi@0 4330 * ensuring that two or more batches, submitted independently, can be processed
danielebarchiesi@0 4331 * without mutual interference. Each batch set may specify its own set of
danielebarchiesi@0 4332 * operations and results, produce its own UI messages, and trigger its own
danielebarchiesi@0 4333 * 'finished' callback. Batch sets are processed sequentially, with the progress
danielebarchiesi@0 4334 * bar starting afresh for each new set.
danielebarchiesi@0 4335 *
danielebarchiesi@0 4336 * @param $batch_definition
danielebarchiesi@0 4337 * An associative array defining the batch, with the following elements (all
danielebarchiesi@0 4338 * are optional except as noted):
danielebarchiesi@0 4339 * - operations: (required) Array of function calls to be performed.
danielebarchiesi@0 4340 * Example:
danielebarchiesi@0 4341 * @code
danielebarchiesi@0 4342 * array(
danielebarchiesi@0 4343 * array('my_function_1', array($arg1)),
danielebarchiesi@0 4344 * array('my_function_2', array($arg2_1, $arg2_2)),
danielebarchiesi@0 4345 * )
danielebarchiesi@0 4346 * @endcode
danielebarchiesi@0 4347 * - title: A safe, translated string to use as the title for the progress
danielebarchiesi@0 4348 * page. Defaults to t('Processing').
danielebarchiesi@0 4349 * - init_message: Message displayed while the processing is initialized.
danielebarchiesi@0 4350 * Defaults to t('Initializing.').
danielebarchiesi@0 4351 * - progress_message: Message displayed while processing the batch. Available
danielebarchiesi@0 4352 * placeholders are @current, @remaining, @total, @percentage, @estimate and
danielebarchiesi@0 4353 * @elapsed. Defaults to t('Completed @current of @total.').
danielebarchiesi@0 4354 * - error_message: Message displayed if an error occurred while processing
danielebarchiesi@0 4355 * the batch. Defaults to t('An error has occurred.').
danielebarchiesi@0 4356 * - finished: Name of a function to be executed after the batch has
danielebarchiesi@0 4357 * completed. This should be used to perform any result massaging that may
danielebarchiesi@0 4358 * be needed, and possibly save data in $_SESSION for display after final
danielebarchiesi@0 4359 * page redirection.
danielebarchiesi@0 4360 * - file: Path to the file containing the definitions of the 'operations' and
danielebarchiesi@0 4361 * 'finished' functions, for instance if they don't reside in the main
danielebarchiesi@0 4362 * .module file. The path should be relative to base_path(), and thus should
danielebarchiesi@0 4363 * be built using drupal_get_path().
danielebarchiesi@0 4364 * - css: Array of paths to CSS files to be used on the progress page.
danielebarchiesi@0 4365 * - url_options: options passed to url() when constructing redirect URLs for
danielebarchiesi@0 4366 * the batch.
danielebarchiesi@0 4367 */
danielebarchiesi@0 4368 function batch_set($batch_definition) {
danielebarchiesi@0 4369 if ($batch_definition) {
danielebarchiesi@0 4370 $batch =& batch_get();
danielebarchiesi@0 4371
danielebarchiesi@0 4372 // Initialize the batch if needed.
danielebarchiesi@0 4373 if (empty($batch)) {
danielebarchiesi@0 4374 $batch = array(
danielebarchiesi@0 4375 'sets' => array(),
danielebarchiesi@0 4376 'has_form_submits' => FALSE,
danielebarchiesi@0 4377 );
danielebarchiesi@0 4378 }
danielebarchiesi@0 4379
danielebarchiesi@0 4380 // Base and default properties for the batch set.
danielebarchiesi@0 4381 // Use get_t() to allow batches during installation.
danielebarchiesi@0 4382 $t = get_t();
danielebarchiesi@0 4383 $init = array(
danielebarchiesi@0 4384 'sandbox' => array(),
danielebarchiesi@0 4385 'results' => array(),
danielebarchiesi@0 4386 'success' => FALSE,
danielebarchiesi@0 4387 'start' => 0,
danielebarchiesi@0 4388 'elapsed' => 0,
danielebarchiesi@0 4389 );
danielebarchiesi@0 4390 $defaults = array(
danielebarchiesi@0 4391 'title' => $t('Processing'),
danielebarchiesi@0 4392 'init_message' => $t('Initializing.'),
danielebarchiesi@0 4393 'progress_message' => $t('Completed @current of @total.'),
danielebarchiesi@0 4394 'error_message' => $t('An error has occurred.'),
danielebarchiesi@0 4395 'css' => array(),
danielebarchiesi@0 4396 );
danielebarchiesi@0 4397 $batch_set = $init + $batch_definition + $defaults;
danielebarchiesi@0 4398
danielebarchiesi@0 4399 // Tweak init_message to avoid the bottom of the page flickering down after
danielebarchiesi@0 4400 // init phase.
danielebarchiesi@0 4401 $batch_set['init_message'] .= '<br/>&nbsp;';
danielebarchiesi@0 4402
danielebarchiesi@0 4403 // The non-concurrent workflow of batch execution allows us to save
danielebarchiesi@0 4404 // numberOfItems() queries by handling our own counter.
danielebarchiesi@0 4405 $batch_set['total'] = count($batch_set['operations']);
danielebarchiesi@0 4406 $batch_set['count'] = $batch_set['total'];
danielebarchiesi@0 4407
danielebarchiesi@0 4408 // Add the set to the batch.
danielebarchiesi@0 4409 if (empty($batch['id'])) {
danielebarchiesi@0 4410 // The batch is not running yet. Simply add the new set.
danielebarchiesi@0 4411 $batch['sets'][] = $batch_set;
danielebarchiesi@0 4412 }
danielebarchiesi@0 4413 else {
danielebarchiesi@0 4414 // The set is being added while the batch is running. Insert the new set
danielebarchiesi@0 4415 // right after the current one to ensure execution order, and store its
danielebarchiesi@0 4416 // operations in a queue.
danielebarchiesi@0 4417 $index = $batch['current_set'] + 1;
danielebarchiesi@0 4418 $slice1 = array_slice($batch['sets'], 0, $index);
danielebarchiesi@0 4419 $slice2 = array_slice($batch['sets'], $index);
danielebarchiesi@0 4420 $batch['sets'] = array_merge($slice1, array($batch_set), $slice2);
danielebarchiesi@0 4421 _batch_populate_queue($batch, $index);
danielebarchiesi@0 4422 }
danielebarchiesi@0 4423 }
danielebarchiesi@0 4424 }
danielebarchiesi@0 4425
danielebarchiesi@0 4426 /**
danielebarchiesi@0 4427 * Processes the batch.
danielebarchiesi@0 4428 *
danielebarchiesi@0 4429 * Unless the batch has been marked with 'progressive' = FALSE, the function
danielebarchiesi@0 4430 * issues a drupal_goto and thus ends page execution.
danielebarchiesi@0 4431 *
danielebarchiesi@0 4432 * This function is generally not needed in form submit handlers;
danielebarchiesi@0 4433 * Form API takes care of batches that were set during form submission.
danielebarchiesi@0 4434 *
danielebarchiesi@0 4435 * @param $redirect
danielebarchiesi@0 4436 * (optional) Path to redirect to when the batch has finished processing.
danielebarchiesi@0 4437 * @param $url
danielebarchiesi@0 4438 * (optional - should only be used for separate scripts like update.php)
danielebarchiesi@0 4439 * URL of the batch processing page.
danielebarchiesi@0 4440 * @param $redirect_callback
danielebarchiesi@0 4441 * (optional) Specify a function to be called to redirect to the progressive
danielebarchiesi@0 4442 * processing page. By default drupal_goto() will be used to redirect to a
danielebarchiesi@0 4443 * page which will do the progressive page. Specifying another function will
danielebarchiesi@0 4444 * allow the progressive processing to be processed differently.
danielebarchiesi@0 4445 */
danielebarchiesi@0 4446 function batch_process($redirect = NULL, $url = 'batch', $redirect_callback = 'drupal_goto') {
danielebarchiesi@0 4447 $batch =& batch_get();
danielebarchiesi@0 4448
danielebarchiesi@0 4449 drupal_theme_initialize();
danielebarchiesi@0 4450
danielebarchiesi@0 4451 if (isset($batch)) {
danielebarchiesi@0 4452 // Add process information
danielebarchiesi@0 4453 $process_info = array(
danielebarchiesi@0 4454 'current_set' => 0,
danielebarchiesi@0 4455 'progressive' => TRUE,
danielebarchiesi@0 4456 'url' => $url,
danielebarchiesi@0 4457 'url_options' => array(),
danielebarchiesi@0 4458 'source_url' => $_GET['q'],
danielebarchiesi@0 4459 'redirect' => $redirect,
danielebarchiesi@0 4460 'theme' => $GLOBALS['theme_key'],
danielebarchiesi@0 4461 'redirect_callback' => $redirect_callback,
danielebarchiesi@0 4462 );
danielebarchiesi@0 4463 $batch += $process_info;
danielebarchiesi@0 4464
danielebarchiesi@0 4465 // The batch is now completely built. Allow other modules to make changes
danielebarchiesi@0 4466 // to the batch so that it is easier to reuse batch processes in other
danielebarchiesi@0 4467 // environments.
danielebarchiesi@0 4468 drupal_alter('batch', $batch);
danielebarchiesi@0 4469
danielebarchiesi@0 4470 // Assign an arbitrary id: don't rely on a serial column in the 'batch'
danielebarchiesi@0 4471 // table, since non-progressive batches skip database storage completely.
danielebarchiesi@0 4472 $batch['id'] = db_next_id();
danielebarchiesi@0 4473
danielebarchiesi@0 4474 // Move operations to a job queue. Non-progressive batches will use a
danielebarchiesi@0 4475 // memory-based queue.
danielebarchiesi@0 4476 foreach ($batch['sets'] as $key => $batch_set) {
danielebarchiesi@0 4477 _batch_populate_queue($batch, $key);
danielebarchiesi@0 4478 }
danielebarchiesi@0 4479
danielebarchiesi@0 4480 // Initiate processing.
danielebarchiesi@0 4481 if ($batch['progressive']) {
danielebarchiesi@0 4482 // Now that we have a batch id, we can generate the redirection link in
danielebarchiesi@0 4483 // the generic error message.
danielebarchiesi@0 4484 $t = get_t();
danielebarchiesi@0 4485 $batch['error_message'] = $t('Please continue to <a href="@error_url">the error page</a>', array('@error_url' => url($url, array('query' => array('id' => $batch['id'], 'op' => 'finished')))));
danielebarchiesi@0 4486
danielebarchiesi@0 4487 // Clear the way for the drupal_goto() redirection to the batch processing
danielebarchiesi@0 4488 // page, by saving and unsetting the 'destination', if there is any.
danielebarchiesi@0 4489 if (isset($_GET['destination'])) {
danielebarchiesi@0 4490 $batch['destination'] = $_GET['destination'];
danielebarchiesi@0 4491 unset($_GET['destination']);
danielebarchiesi@0 4492 }
danielebarchiesi@0 4493
danielebarchiesi@0 4494 // Store the batch.
danielebarchiesi@0 4495 db_insert('batch')
danielebarchiesi@0 4496 ->fields(array(
danielebarchiesi@0 4497 'bid' => $batch['id'],
danielebarchiesi@0 4498 'timestamp' => REQUEST_TIME,
danielebarchiesi@0 4499 'token' => drupal_get_token($batch['id']),
danielebarchiesi@0 4500 'batch' => serialize($batch),
danielebarchiesi@0 4501 ))
danielebarchiesi@0 4502 ->execute();
danielebarchiesi@0 4503
danielebarchiesi@0 4504 // Set the batch number in the session to guarantee that it will stay alive.
danielebarchiesi@0 4505 $_SESSION['batches'][$batch['id']] = TRUE;
danielebarchiesi@0 4506
danielebarchiesi@0 4507 // Redirect for processing.
danielebarchiesi@0 4508 $function = $batch['redirect_callback'];
danielebarchiesi@0 4509 if (function_exists($function)) {
danielebarchiesi@0 4510 $function($batch['url'], array('query' => array('op' => 'start', 'id' => $batch['id'])));
danielebarchiesi@0 4511 }
danielebarchiesi@0 4512 }
danielebarchiesi@0 4513 else {
danielebarchiesi@0 4514 // Non-progressive execution: bypass the whole progressbar workflow
danielebarchiesi@0 4515 // and execute the batch in one pass.
danielebarchiesi@0 4516 require_once DRUPAL_ROOT . '/includes/batch.inc';
danielebarchiesi@0 4517 _batch_process();
danielebarchiesi@0 4518 }
danielebarchiesi@0 4519 }
danielebarchiesi@0 4520 }
danielebarchiesi@0 4521
danielebarchiesi@0 4522 /**
danielebarchiesi@0 4523 * Retrieves the current batch.
danielebarchiesi@0 4524 */
danielebarchiesi@0 4525 function &batch_get() {
danielebarchiesi@0 4526 // Not drupal_static(), because Batch API operates at a lower level than most
danielebarchiesi@0 4527 // use-cases for resetting static variables, and we specifically do not want a
danielebarchiesi@0 4528 // global drupal_static_reset() resetting the batch information. Functions
danielebarchiesi@0 4529 // that are part of the Batch API and need to reset the batch information may
danielebarchiesi@0 4530 // call batch_get() and manipulate the result by reference. Functions that are
danielebarchiesi@0 4531 // not part of the Batch API can also do this, but shouldn't.
danielebarchiesi@0 4532 static $batch = array();
danielebarchiesi@0 4533 return $batch;
danielebarchiesi@0 4534 }
danielebarchiesi@0 4535
danielebarchiesi@0 4536 /**
danielebarchiesi@0 4537 * Populates a job queue with the operations of a batch set.
danielebarchiesi@0 4538 *
danielebarchiesi@0 4539 * Depending on whether the batch is progressive or not, the BatchQueue or
danielebarchiesi@0 4540 * BatchMemoryQueue handler classes will be used.
danielebarchiesi@0 4541 *
danielebarchiesi@0 4542 * @param $batch
danielebarchiesi@0 4543 * The batch array.
danielebarchiesi@0 4544 * @param $set_id
danielebarchiesi@0 4545 * The id of the set to process.
danielebarchiesi@0 4546 *
danielebarchiesi@0 4547 * @return
danielebarchiesi@0 4548 * The name and class of the queue are added by reference to the batch set.
danielebarchiesi@0 4549 */
danielebarchiesi@0 4550 function _batch_populate_queue(&$batch, $set_id) {
danielebarchiesi@0 4551 $batch_set = &$batch['sets'][$set_id];
danielebarchiesi@0 4552
danielebarchiesi@0 4553 if (isset($batch_set['operations'])) {
danielebarchiesi@0 4554 $batch_set += array(
danielebarchiesi@0 4555 'queue' => array(
danielebarchiesi@0 4556 'name' => 'drupal_batch:' . $batch['id'] . ':' . $set_id,
danielebarchiesi@0 4557 'class' => $batch['progressive'] ? 'BatchQueue' : 'BatchMemoryQueue',
danielebarchiesi@0 4558 ),
danielebarchiesi@0 4559 );
danielebarchiesi@0 4560
danielebarchiesi@0 4561 $queue = _batch_queue($batch_set);
danielebarchiesi@0 4562 $queue->createQueue();
danielebarchiesi@0 4563 foreach ($batch_set['operations'] as $operation) {
danielebarchiesi@0 4564 $queue->createItem($operation);
danielebarchiesi@0 4565 }
danielebarchiesi@0 4566
danielebarchiesi@0 4567 unset($batch_set['operations']);
danielebarchiesi@0 4568 }
danielebarchiesi@0 4569 }
danielebarchiesi@0 4570
danielebarchiesi@0 4571 /**
danielebarchiesi@0 4572 * Returns a queue object for a batch set.
danielebarchiesi@0 4573 *
danielebarchiesi@0 4574 * @param $batch_set
danielebarchiesi@0 4575 * The batch set.
danielebarchiesi@0 4576 *
danielebarchiesi@0 4577 * @return
danielebarchiesi@0 4578 * The queue object.
danielebarchiesi@0 4579 */
danielebarchiesi@0 4580 function _batch_queue($batch_set) {
danielebarchiesi@0 4581 static $queues;
danielebarchiesi@0 4582
danielebarchiesi@0 4583 // The class autoloader is not available when running update.php, so make
danielebarchiesi@0 4584 // sure the files are manually included.
danielebarchiesi@0 4585 if (!isset($queues)) {
danielebarchiesi@0 4586 $queues = array();
danielebarchiesi@0 4587 require_once DRUPAL_ROOT . '/modules/system/system.queue.inc';
danielebarchiesi@0 4588 require_once DRUPAL_ROOT . '/includes/batch.queue.inc';
danielebarchiesi@0 4589 }
danielebarchiesi@0 4590
danielebarchiesi@0 4591 if (isset($batch_set['queue'])) {
danielebarchiesi@0 4592 $name = $batch_set['queue']['name'];
danielebarchiesi@0 4593 $class = $batch_set['queue']['class'];
danielebarchiesi@0 4594
danielebarchiesi@0 4595 if (!isset($queues[$class][$name])) {
danielebarchiesi@0 4596 $queues[$class][$name] = new $class($name);
danielebarchiesi@0 4597 }
danielebarchiesi@0 4598 return $queues[$class][$name];
danielebarchiesi@0 4599 }
danielebarchiesi@0 4600 }
danielebarchiesi@0 4601
danielebarchiesi@0 4602 /**
danielebarchiesi@0 4603 * @} End of "defgroup batch".
danielebarchiesi@0 4604 */