function hs_subscriptions_taxonomy_form_alter in Hierarchical Select 5.3
Implementation of hook_form_alter().
File
- modules/
hs_subscriptions_taxonomy.module, line 16 - Makes the Taxonomy Subscriptions module use Hierarchical Select form items at user/<uid>/subscriptions/taxa.
Code
function hs_subscriptions_taxonomy_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']['#config'] = array(
'module' => 'hs_taxonomy',
'params' => array(
'vid' => $vocabulary->vid,
),
'save_lineage' => 0,
'enforce_deepest' => 1,
'resizable' => 1,
'level_labels' => array(
'status' => 0,
),
'dropbox' => array(
'status' => $form[$name]['select']['#multiple'],
'title' => t('Subscribed to:'),
'limit' => 0,
'reset_hs' => 0,
),
'editability' => array(
'status' => 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']['hs_subscriptions_taxonomy_validate'] = array();
}
return $form;
}