You are here

function hierarchical_select_json in Hierarchical Select 6.3

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

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

1 call to hierarchical_select_json()
hs_taxonomy_views_json in modules/hs_taxonomy_views.module
Menu callback; wrapper around hierarchical_select_json, with support for preloading the view thats is referenced from within the form.
2 string references to 'hierarchical_select_json'
hierarchical_select_elements in ./hierarchical_select.module
Implementation of hook_elements().
hierarchical_select_menu in ./hierarchical_select.module
Implementation of hook_menu().

File

./hierarchical_select.module, line 314
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'];

  // In this context, we're in a node selection mode.
  // See http://drupal.org/node/683574#comment-3117926.
  if (module_exists('i18n') && isset($_POST['language'])) {
    i18n_selection_mode('node', $_POST['language']);
  }

  // Collect all necessary variables.
  $cached = cache_get($hs_form_build_id, 'cache_hierarchical_select');
  $storage = $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];
  if (HS_DEVELOPER_MODE) {
    _hierarchical_select_log("form_id: {$form_id}");
    _hierarchical_select_log("hs_form_build_id: {$hs_form_build_id}");
  }
  $form_state =& $storage['parameters'][1];

  // Include the file in which the form definition function lives.
  if (!empty($storage['file'])) {
    require_once $storage['file'];
  }

  // Also include files set in $form_state['form_load_files']. Set by CTools
  // Delegator, which is used by Panels (i.e. this is necessary for Panels
  // compatibility).
  if (isset($form_state['form_load_files'])) {
    foreach ($form_state['form_load_files'] as $file) {
      require_once './' . $file;
    }
  }

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

  // 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 (isset($_POST['client_supports_caching'])) {
    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' => isset($part_of_form['log']['#value']) ? $part_of_form['log']['#value'] : NULL,
  ));
  exit;
}