You are here

function hierarchical_select_after_build in Hierarchical Select 6.3

Same name and namespace in other branches
  1. 5.3 hierarchical_select.module \hierarchical_select_after_build()

Hierarchical select form element type #after_build callback.

1 string reference to 'hierarchical_select_after_build'
hierarchical_select_form_alter in ./hierarchical_select.module
Implementation of hook_form_alter().

File

./hierarchical_select.module, line 725
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_after_build($form, &$form_state) {

  // TRICKY: Pageroute compatibility: avoid that the body of this #after_build
  // callback is executed twice.
  if (isset($form['hs_form_build_id'])) {
    return $form;
  }
  $names = _hierarchical_select_store_name(NULL, NULL, TRUE);
  if (!isset($_POST['hs_form_build_id']) && count($names)) {
    $parameters = isset($form['#parameters']) ? $form['#parameters'] : array();
    $menu_item = menu_get_item();

    // Collect information in this array, which will be used in dynamic form
    // updates, to …
    $storage = array(
      // … retrieve $form.
      'parameters' => $parameters,
      // … determine which part of $form should be rendered.
      '#names' => $names,
      // … include the file in which the form function lives.
      'file' => $menu_item['file'],
    );

    // Store the information needed for dynamic form updates in the cache, so
    // we can retrieve this in our JSON callbacks (to be able to rebuild and
    // render part of the form).
    $hs_form_build_id = 'hs_form_' . md5(mt_rand());
    $lifetime = variable_get('hierarchical_select_cache_lifetime', HS_CACHE_LIFETIME_DEFAULT);
    cache_set($hs_form_build_id, $storage, 'cache_hierarchical_select', time() + $lifetime);
  }
  elseif (isset($_POST['hs_form_build_id'])) {

    // Don't generate a new hs_form_build_id if this is a re-rendering of the
    // same form!
    $hs_form_build_id = $_POST['hs_form_build_id'];
  }

  // Store the hs_form_build_id in a hidden value, so that it gets POSTed.
  $form_element = array(
    '#type' => 'hidden',
    '#value' => $hs_form_build_id,
    // We have to set #parents manually because we want to send only
    // $form_element through form_builder(), not $form. If we set #parents,
    // form_builder() has all info it needs to generate #name and #id.
    '#parents' => array(
      'hs_form_build_id',
    ),
  );
  $form['hs_form_build_id'] = form_builder($form['form_id']['#value'], $form_element, $form_state);
  return $form;
}