Revision 4003efde
Added by Assos Assos over 4 years ago
drupal7/sites/all/modules/views/views.module | ||
---|---|---|
2197 | 2197 |
$form['form_build_id']['#access'] = FALSE; |
2198 | 2198 |
$form['form_token']['#access'] = FALSE; |
2199 | 2199 |
$form['form_id']['#access'] = FALSE; |
2200 |
|
|
2201 |
// AJAX behaviors need these data, so we add it back in #after_build. |
|
2202 |
$form['#after_build'][] = 'views_exposed_form_ajax_enable'; |
|
2203 |
} |
|
2204 |
|
|
2205 |
/** |
|
2206 |
* Checks whether the exposed form will use AJAX. |
|
2207 |
* |
|
2208 |
* Passes required form information removed in |
|
2209 |
* views_form_views_exposed_form_alter(). |
|
2210 |
* |
|
2211 |
* @param array $form |
|
2212 |
* The form being processed. |
|
2213 |
* @param array $form_state |
|
2214 |
* The form state. |
|
2215 |
* |
|
2216 |
* @return array |
|
2217 |
* The form being processed. |
|
2218 |
*/ |
|
2219 |
function views_exposed_form_ajax_enable(array &$form, array &$form_state) { |
|
2220 |
// In order for AJAX to work the form build info is needed. Here check if |
|
2221 |
// #ajax has been added to any form elements, and if so, pass this info as |
|
2222 |
// settings via JavaScript, which get attached to the submitted form on AJAX |
|
2223 |
// form submissions. #ajax property can be set not only for the first level of |
|
2224 |
// the form, so look for it in the whole form. |
|
2225 |
$ajax_info = array(); |
|
2226 |
$ajax_elements = views_exposed_form_ajax_lookup_recursive($form); |
|
2227 |
// Determine if form is being processed. |
|
2228 |
$triggering_element_name = ''; |
|
2229 |
if (!empty($form_state['input']['_triggering_element_name'])) { |
|
2230 |
$triggering_element_name = $form_state['input']['_triggering_element_name']; |
|
2231 |
} |
|
2232 |
|
|
2233 |
// When there are multiple elements with #ajax set on exposed form, pass only |
|
2234 |
// triggering element name to JavaScript, otherwise #ajax will work only for |
|
2235 |
// the first element. |
|
2236 |
if (!empty($triggering_element_name) && !empty($ajax_elements)) { |
|
2237 |
// Check if element has #ajax property set. |
|
2238 |
if (in_array($triggering_element_name, $ajax_elements)) { |
|
2239 |
$ajax_elements = array( |
|
2240 |
$triggering_element_name => $triggering_element_name, |
|
2241 |
); |
|
2242 |
} |
|
2243 |
else { |
|
2244 |
$ajax_elements = array(); |
|
2245 |
} |
|
2246 |
} |
|
2247 |
|
|
2248 |
if (!empty($ajax_elements)) { |
|
2249 |
$form_info = array( |
|
2250 |
'form_id' => $form['#form_id'], |
|
2251 |
'form_build_id' => $form['#build_id'], |
|
2252 |
); |
|
2253 |
// Anonymous users don't get a token. |
|
2254 |
if (!empty($form['#token'])) { |
|
2255 |
$form_info['form_token'] = drupal_get_token($form['#token']); |
|
2256 |
} |
|
2257 |
foreach ($ajax_elements as $element_name) { |
|
2258 |
$ajax_info[$element_name] = $form_info; |
|
2259 |
} |
|
2260 |
drupal_add_js(array('ViewsExposedFormInfo' => $ajax_info), 'setting'); |
|
2261 |
|
|
2262 |
// Add the javascript behavior that will handle this data. |
|
2263 |
$form['#attached']['js'][] = array( |
|
2264 |
'weight' => 100, |
|
2265 |
'data' => drupal_get_path('module', 'views') . '/js/exposed-form-ajax.js', |
|
2266 |
); |
|
2267 |
} |
|
2268 |
|
|
2269 |
return $form; |
|
2270 |
} |
|
2271 |
|
|
2272 |
/** |
|
2273 |
* Recursively looks for the #ajax property for every form elemet. |
|
2274 |
* |
|
2275 |
* @param array $elements |
|
2276 |
* The element array to look for #ajax property. |
|
2277 |
* |
|
2278 |
* @return array |
|
2279 |
* Array of the elements names where #ajax was found. |
|
2280 |
*/ |
|
2281 |
function views_exposed_form_ajax_lookup_recursive(array $elements) { |
|
2282 |
$ajax_elements = array(); |
|
2283 |
foreach (element_children($elements) as $key) { |
|
2284 |
if (!empty($elements[$key]['#name']) && !empty($elements[$key]['#ajax'])) { |
|
2285 |
$ajax_elements[$elements[$key]['#name']] = $elements[$key]['#name']; |
|
2286 |
} |
|
2287 |
// Recursive call to look for #ajax in element's childrens. |
|
2288 |
$ajax_elements += views_exposed_form_ajax_lookup_recursive($elements[$key]); |
|
2289 |
} |
|
2290 |
return $ajax_elements; |
|
2200 | 2291 |
} |
2201 | 2292 |
|
2202 | 2293 |
/** |
Also available in: Unified diff
Weekly update of contrib modules