From 42f1893f4910001139f11d5cafceb0463b751d30 Mon Sep 17 00:00:00 2001 From: Jason Skomorowski Date: Tue, 29 Mar 2011 01:17:51 +0000 Subject: [PATCH] Dynamic facet creation. --- mkdru.module | 96 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 28 deletions(-) diff --git a/mkdru.module b/mkdru.module index 24e36c2..9185042 100644 --- a/mkdru.module +++ b/mkdru.module @@ -88,6 +88,11 @@ function mkdru_menu() { 'access arguments' => array('administer site configuration'), 'type' => MENU_NORMAL_ITEM, ); + $items['ahah-mkdru-facet'] = array( + 'page callback' => 'mkdru_add_facet_callback', + 'access arguments' => array('create metasearch interface'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -106,7 +111,7 @@ function mkdru_init() { // function mkdru_settings_form($form, &$form_state) { function mkdru_settings_form(&$form_state) { if (isset($form_state['values']['settings'])) { - $settings = $form_state['build_info']['args']['settings']; + $settings = $form_state['values']['settings']; } else if (isset($form_state['build_info']['args']['settings'])) { $settings = $form_state['build_info']['args']['settings']; @@ -115,6 +120,8 @@ function mkdru_settings_form(&$form_state) { $settings = variable_get('mkdru_defaults', NULL); } + $form['#cache'] = TRUE; + $form['settings'] = array( '#tree' => TRUE, ); @@ -163,10 +170,8 @@ function mkdru_settings_form(&$form_state) { '#collapsible' => TRUE, '#collapsed' => FALSE ); - if (empty($form_state['facet_names'])) { - $form_state['facet_names'] = array('source', 'author', 'subject'); - } - foreach ($form_state['facet_names'] as $facet) { + + foreach (array_keys($settings['facets']) as $facet) { $form['settings']['facets'][$facet] = array( '#type' => 'fieldset', '#title' => $facet . ' ' . t('facet'), @@ -207,31 +212,67 @@ function mkdru_settings_form(&$form_state) { '#maxlength' => 3, ); } -// $form['new_facet'] = array( -// '#type' => 'fieldset', -// '#title' => t('Add new facet...'), -// '#tree' => TRUE, -// '#collapsible' => TRUE, -// '#collapsed' => FALSE -// ); -// $form['new_facet']['canonical'] = array( -// '#type' => 'textfield', -// '#title' => t('Canonical name of new facet'), -// ); -// $form['new_facet']['button'] = array( -// '#type' => 'submit', -// '#value' => t('Add facet'), -// '#description' => t('Configure additional facets based on Pazpar2/SP termlists'), -// '#weight' => 1, -// '#submit' => array('mkdru_add_facet_form'), -// '#ajax' => array( -// 'callback' => 'mkdru_add_facet_callback', -// 'wrapper' => 'mkdru-facets-form-wrapper', -// ), -// ); + $form['new_facet'] = array( + '#type' => 'fieldset', + '#title' => t('Add new facet...'), + '#tree' => TRUE, + '#collapsible' => TRUE, + '#collapsed' => FALSE + ); + $form['new_facet']['canonical'] = array( + '#type' => 'textfield', + '#title' => t('Canonical name of new facet'), + ); + $form['new_facet']['button'] = array( + '#type' => 'submit', + '#value' => t('Add facet'), + '#description' => t('Configure additional facets based on Pazpar2/SP termlists'), + '#submit' => array('mkdru_add_facet_submit'), + '#ahah' => array( + 'path' => 'ahah-mkdru-facet', + 'wrapper' => 'mkdru-facets-form-wrapper', + ), + ); return $form; } +function mkdru_add_facet_submit($form, &$form_state) { +// $newfacet = $form_state['values']['new_facet']['canonical']; + $newfacet = 'PFUI'; + $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)); +} + // Ding config @@ -263,7 +304,6 @@ function mkdru_form(&$node, &$form_state) { } $form = drupal_retrieve_form('mkdru_settings_form', &$form_state); - $type = node_get_types('type', $node); $form['title'] = array( '#type' => 'textfield', -- 1.7.10.4