You are here

function hierarchical_select_json in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 5.2 hierarchical_select.module \hierarchical_select_json()
  2. 6.3 hierarchical_select.module \hierarchical_select_json()

Menu callback; format=text/json; generates and outputs the appropriate HTML.

2 string references to 'hierarchical_select_json'
hierarchical_select_menu in ./hierarchical_select.module
Implementation of hook_menu().
_hierarchical_select_setup_js in ./hierarchical_select.module
Helper function to add the required Javascript files and settings.

File

./hierarchical_select.module, line 250
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select items in a hierarchy.

Code

function hierarchical_select_json() {

  // We are returning Javascript, so tell the browser. Ripped from Drupal 6's
  // drupal_json() function.
  drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  $hs_form_build_id = $_POST['hs_form_build_id'];
  $hs_form_build_id = _hierarchical_select_get_hs_form_build_id();

  // Collect all necessary variables.
  $cached = cache_get($hs_form_build_id, 'cache');
  $storage = unserialize($cached->data);

  // Ensure that the form id in the POST array is the same as the one of the
  // stored parameters of the original form. For 99% of the forms, this step
  // is not necessary, but when a hierarchical_select form item is inside a
  // form in a subform_element in a form, then it is necessary.
  $form_id = $_POST['form_id'] = $storage['parameters'][0];

  // Pick the correct language.
  if (module_exists('i18n')) {
    i18n_selection_mode($storage['i18n']['mode'], $storage['i18n']['params']);
  }
  if (HS_DEVELOPER_MODE) {
    _hierarchical_select_log("form_id: {$form_id}");
    _hierarchical_select_log("hs_form_build_id: {$hs_form_build_id}");
  }

  // Retrieve and process the form.
  $form = call_user_func_array('drupal_retrieve_form', $storage['parameters']);
  drupal_prepare_form($form_id, $form);

  // Render only the relevant part of the form (i.e. the hierarchical_select
  // form item that has triggered this AJAX callback).
  $hsid = $_POST['hsid'];
  $name = $storage['#names'][$hsid];
  $part_of_form = _hierarchical_select_get_form_item($form, $name);
  $output = drupal_render($part_of_form);

  // If the user's browser supports the active cache system, then send the
  // currently requested hierarchy in an easy-to-manage form.
  $cache = array();
  if ($_POST['client_supports_caching'] == 'true') {
    $cache = _hierarchical_select_json_convert_hierarchy_to_cache($part_of_form['hierarchy']['#value']);
  }
  else {
    if ($_POST['client_supports_caching'] == 'false') {

      // This indicates that a client-side cache is installed, but not working
      // properly.
      // TODO: figure out a clean way to notify the administrator.
    }
  }
  print drupal_to_js(array(
    'cache' => $cache,
    'output' => $output,
    'log' => $part_of_form['log']['#value'],
  ));
  exit;
}