1.20.x views_ui.admin.inc | views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_parents) |
Converts a form element in the add view wizard to be AJAX-enabled.
This function takes a form element and adds AJAX behaviors to it such that changing it triggers another part of the form to update automatically. It also adds a submit button to the form that appears next to the triggering element and that duplicates its functionality for users who do not have JavaScript enabled (the button is automatically hidden for users who do have JavaScript).
To use this function, call it directly from your form builder function immediately after you have defined the form element that will serve as the JavaScript trigger. Calling it elsewhere (such as in hook_form_alter()) may mean that the non-JavaScript fallback button does not appear in the correct place in the form.
Parameters
$wrapping_element: The element whose child will server as the AJAX trigger. For example, if $form['some_wrapper']['triggering_element'] represents the element which will trigger the AJAX behavior, you would pass $form['some_wrapper'] for this parameter.
$trigger_key: The key within the wrapping element that identifies which of its children serves as the AJAX trigger. In the above example, you would pass 'triggering_element' for this parameter.
$refresh_parents: An array of parent keys that point to the part of the form that will be refreshed by AJAX. For example, if triggering the AJAX behavior should cause $form['dynamic_content']['section'] to be refreshed, you would pass array('dynamic_content', 'section') for this parameter.
File
- modules/
views_ui/ views_ui.admin.inc, line 612 - Admin page callbacks for the Views UI module.
Code
function views_ui_add_ajax_trigger(&$wrapping_element, $trigger_key, $refresh_parents) {
$seen_ids = &backdrop_static(__FUNCTION__ . ':seen_ids', array());
$seen_buttons = &backdrop_static(__FUNCTION__ . ':seen_buttons', array());
// Add the AJAX behavior to the triggering element.
$triggering_element = &$wrapping_element[$trigger_key];
$triggering_element['#ajax']['callback'] = 'views_ui_ajax_update_form';
// We do not use backdrop_html_id() to get an ID for the AJAX wrapper, because
// it remembers IDs across AJAX requests (and won't reuse them), but in our
// case we need to use the same ID from request to request so that the
// wrapper can be recognized by the AJAX system and its content can be
// dynamically updated. So instead, we will keep track of duplicate IDs
// (within a single request) on our own, later in this function.
$triggering_element['#ajax']['wrapper'] = 'edit-view-' . implode('-', $refresh_parents) . '-wrapper';
// Add a submit button for users who do not have JavaScript enabled. It
// should be displayed next to the triggering element on the form.
$button_key = $trigger_key . '_trigger_update';
$wrapping_element[$button_key] = array(
'#type' => 'submit',
// Hide this button when JavaScript is enabled.
'#attributes' => array('class' => array('js-hide')),
'#submit' => array('views_ui_nojs_submit'),
// Add a process function to limit this button's validation errors to the
// triggering element only. We have to do this in #process since until the
// form API has added the #parents property to the triggering element for
// us, we don't have any (easy) way to find out where its submitted values
// will eventually appear in $form_state['values'].
'#process' => array_merge(array('views_ui_add_limited_validation'), element_info_property('submit', '#process', array())),
// Add an after-build function that inserts a wrapper around the region of
// the form that needs to be refreshed by AJAX (so that the AJAX system can
// detect and dynamically update it). This is done in #after_build because
// it's a convenient place where we have automatic access to the complete
// form array, but also to minimize the chance that the HTML we add will
// get clobbered by code that runs after we have added it.
'#after_build' => array_merge(element_info_property('submit', '#after_build', array()), array('views_ui_add_ajax_wrapper')),
);
// Copy #weight and #access from the triggering element to the button, so
// that the two elements will be displayed together.
foreach (array('#weight', '#access') as $property) {
if (isset($triggering_element[$property])) {
$wrapping_element[$button_key][$property] = $triggering_element[$property];
}
}
// For easiest integration with the form API and the testing framework, we
// always give the button a unique #value, rather than playing around with
// #name.
$button_title = !empty($triggering_element['#title']) ? $triggering_element['#title'] : $trigger_key;
if (empty($seen_buttons[$button_title])) {
$wrapping_element[$button_key]['#value'] = t('Update "@title" choice', array(
'@title' => $button_title,
));
$seen_buttons[$button_title] = 1;
}
else {
$wrapping_element[$button_key]['#value'] = t('Update "@title" choice (@number)', array(
'@title' => $button_title,
'@number' => ++$seen_buttons[$button_title],
));
}
// Attach custom data to the triggering element and submit button, so we can
// use it in both the process function and AJAX callback.
$ajax_data = array(
'wrapper' => $triggering_element['#ajax']['wrapper'],
'trigger_key' => $trigger_key,
'refresh_parents' => $refresh_parents,
// Keep track of duplicate wrappers so we don't add the same wrapper to the
// page more than once.
'duplicate_wrapper' => !empty($seen_ids[$triggering_element['#ajax']['wrapper']]),
);
$seen_ids[$triggering_element['#ajax']['wrapper']] = TRUE;
$triggering_element['#views_ui_ajax_data'] = $ajax_data;
$wrapping_element[$button_key]['#views_ui_ajax_data'] = $ajax_data;
}