+function mkdru_add_facet_submit($form, &$form_state) {
+ $newfacet = $form_state['values']['new_facet']['canonical'];
+ $form_state['values']['settings']['facets'][$newfacet] = NULL;
+ $form_state['rebuild'] = TRUE;
+ return $form_state['values'];
+}
+
+function mkdru_add_facet_callback() {
+ // Necessary to work with hook_form
+ module_load_include('inc', 'node', 'node.pages');
+
+ // Retrieve form from cache
+ $form_state = array('storage' => NULL, 'submitted' => FALSE);
+ $form_build_id = $_POST['form_build_id'];
+ $form = form_get_cache($form_build_id, $form_state);
+
+ // Run drupal_process_form to call submit handler and update $form_state
+ $args = $form['#parameters'];
+ $form_id = array_shift($args);
+ $form_state['post'] = $form['#post'] = $_POST;
+ $form['#programmed'] = $form['#redirect'] = FALSE;
+ drupal_process_form($form_id, $form, $form_state);
+
+ // Regenerate form so we can render the new facet part
+ $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
+
+ // Choose subset of form to redraw.
+ $facet_form = $form['settings']['facets'];
+ // Prevent duplicate wrappers.
+ unset($facet_form['#prefix'], $facet_form['#suffix']);
+ $output = theme('status_messages') . drupal_render($facet_form);
+
+ // Final rendering callback
+ drupal_json(array('status' => TRUE, 'data' => $output));
+}
+