You are here

function hierarchical_select_process in Hierarchical Select 5.2

Same name and namespace in other branches
  1. 5.3 hierarchical_select.module \hierarchical_select_process()
  2. 5 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 171
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select an option in a hierarchy.

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) ? '' : 'index.php?q=';
    $url .= 'hierarchical_select_json';

    // 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++;
  }
  extract(_hierarchical_select_extract_settings($element));

  // If the form item is not required, then we must ensure we have a "<none>"
  // option in the original select. If one exists already, we overwrite it: we
  // want exactly the same text here as in the hierarchical select.
  if (!$required) {
    $element['#options'] = array(
      '' => '<' . t('none') . '>',
    ) + $element['#options'];
  }

  // Render the initial HTML.
  $dropbox = !$multiple ? FALSE : hierarchical_select_get_dropbox($module, $selection, $save_lineage, $level_labels, $params, $dropbox_title);
  $hierarchy = hierarchical_select_get_hierarchy($module, $multiple ? -1 : $selection, $save_lineage, $enforce_deepest, $all_option, $level_labels, $params, $required, $dropbox);
  $initial = theme('hierarchical_select_render_initial_html', $hsid, $hierarchy, $dropbox);
  drupal_add_js(array(
    'hierarchical_select' => array(
      'settings' => array(
        $hsid => array(
          'animationDelay' => $animation_delay == 0 ? variable_get('hierarchical_select_animation_delay', 400) : $animation_delay,
          'initial' => $initial,
          'initialDropboxLineagesSelections' => !$dropbox ? NULL : $dropbox->lineages_selections,
          'addButton' => theme('hierarchical_select_dropbox_add_button', $hsid),
          'module' => $module,
          'enforceDeepest' => $enforce_deepest,
          'saveLineage' => $save_lineage,
          'allOption' => $all_option,
          'levelLabels' => implode('|', $level_labels),
          'params' => serialize($params),
          'dropboxTitle' => $dropbox_title,
          'dropboxLimit' => $dropbox_limit,
          'required' => $required,
          'multiple' => $multiple,
        ),
      ),
    ),
  ), 'setting');

  // If the "save lineage" option is enabled, make the select a multiple select.
  if ($save_lineage) {
    $element['#multiple'] = TRUE;
  }

  // If multiple select is enabled, then add a validate callback that will
  // ensure the dropbox limit won't be exceeded.
  $element['#validate'] = array(
    '_hierarchical_select_validate' => array(
      _hierarchical_select_extract_settings($element),
    ),
  );

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