You are here

function hierarchical_select_process in Hierarchical Select 5

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

Hierarchical select form element processing function.

File

./hierarchical_select.module, line 100
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select an option in a hierarchy. Out of the box, this module supports the taxonomy and content_taxonomy modules, but that…

Code

function hierarchical_select_process($element) {
  static $hsid;

  // Render a hierarchical select as a normal select, it's the JavaScript that
  // will turn it into a hierarchical select.
  $element['#type'] = 'select';
  if (!isset($hsid)) {
    $hsid = 0;
    $url = base_path();
    $url .= variable_get('clean_url', 0) ? '' : '?q=';
    $url .= 'hierarchical_select_ahah';

    // Add the CSS and JS, set the URL that should be used by all hierarchical
    // selects.
    drupal_add_css(drupal_get_path('module', 'hierarchical_select') . '/hierarchical_select.css');
    jquery_interface_add();
    drupal_add_js(drupal_get_path('module', 'hierarchical_select') . '/hierarchical_select.js');
    drupal_add_js(array(
      'hierarchical_select' => array(
        'url' => $url,
      ),
    ), 'setting');
  }
  else {
    $hsid++;
  }

  // Pass some settings for this hierarchical select.
  $module = $element['#hierarchical_select_settings']['module'];
  $params = $element['#hierarchical_select_settings']['params'];
  $lineage = $element['#hierarchical_select_settings']['lineage'];

  // When the #value property is empty, we're rendering this form (and thus
  // the form element) for the first time. When it's no longer empty, this
  // means that the validation failed and that we must keep the option that
  // was selected by the user.
  if (is_array($element['#value']) && empty($element['#value'])) {
    $selection = is_array($element['#default_value']) ? $element['#default_value'][0] : $element['#default_value'];
  }
  else {
    $selection = is_array($element['#value']) ? $element['#value'][0] : $element['#value'];
  }
  $initial = _hierarchical_select_render($hsid, $module, $selection, FALSE, TRUE, (bool) $element['#required'], $params);
  drupal_add_js(array(
    'hierarchical_select' => array(
      'settings' => array(
        $hsid => array(
          'initial' => $initial,
          'module' => $module,
          'required' => (bool) $element['#required'],
          'lineage' => (bool) $lineage,
          'params' => $params,
        ),
      ),
    ),
  ), 'setting');

  // If the lineage property is set to TRUE, make the select a multiple select.
  if ($lineage) {
    $element['#multiple'] = TRUE;
  }

  // Set the unique class.
  $element['#attributes']['class'] .= " hierarchical-select-{$hsid} hierarchical-select";
  return $element;
}