You are here

function subscriptions_taxonomy_hierarchical_select_form_alter in Hierarchical Select 5.2

Implementation of hook_hierarchical_select_form_alter().

File

modules/subscriptions_taxonomy.inc, line 16
Makes the Taxonomy Subscriptions module use Hierarchical Select form items at user/<uid>/subscriptions/taxa.

Code

function subscriptions_taxonomy_hierarchical_select_form_alter($form_id, &$form) {
  if ($form_id == 'subscriptions_taxonomy_taxa_form') {
    foreach ($form as $name => $element) {
      if (is_numeric($name)) {

        // Rip the per-vocabulary settings out of the fieldsets.
        $form[$name] = $form[$name][0];

        // Remove the #theme property, we don't need that anymore.
        unset($form[$name]['#theme']);

        // 1) Get the tid of the first checkbox.
        // 2) Get the term object for this first term, from which we can get
        //    the vid.
        // 3) Get the vocabulary object using that vid.
        $tid = reset(array_keys($form[$name]['checkboxes']));
        $term = taxonomy_get_term($tid);
        $vocabulary = taxonomy_get_vocabulary($term->vid);

        // 1) Extract the tids of the selected terms.
        // 2) Make the checkboxes value form elements, to keep the form
        // structure (and the $form_values structure) the same as the original.
        $selected_tids = array();
        foreach ($form[$name]['checkboxes'] as $tid => $element) {

          // Selected?
          if ($element[-1]['#default_value'] == 1) {

            // -1 => never selected, 0 => once selected, but not at the moment, 1 => selected
            $selected_tids[] = $tid;
          }
          $form[$name]['checkboxes'][$tid][-1]['#type'] = 'value';
          $form[$name]['checkboxes'][$tid][-1]['#value'] = $form[$name]['checkboxes'][$tid][-1]['#default_value'];
          unset($form[$name]['checkboxes'][$tid][-1]['#default_value']);
        }

        // Add a taxonomy select instead.
        $form[$name]['select'] = _taxonomy_term_select($vocabulary->name, 'this-argument-is-not-used', array(), $vocabulary->vid, '', TRUE, '<' . t('none') . '>', array());

        // Finally, make it a Hierarchical Select!
        $form[$name]['select']['#type'] = 'hierarchical_select';
        $form[$name]['select']['#default_value'] = $selected_tids;
        $form[$name]['select']['#hierarchical_select_settings'] = array(
          'module' => 'taxonomy',
          'params' => array(
            'vid' => $vocabulary->vid,
          ),
          'save_linage' => FALSE,
          'enforce_deepest' => TRUE,
          'dropbox_title' => t('Subscribed to:'),
          'dropbox_limit' => 0,
        );
      }
    }

    // Since this obviously can't work with the default submit handler, alter
    // the form back to its original state in our validation handler.
    $form['#validate']['hierarchical_select_subscriptions_taxonomy_validate'] = array();
  }
  return $form;
}