You are here

function hs_taxonomy_field_widget_form in Hierarchical Select 7.3

Implements hook_field_widget_form().

File

modules/hs_taxonomy.module, line 267
Implementation of the Hierarchical Select API for the Taxonomy module.

Code

function hs_taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
  if (!empty($field['settings']['allowed_values'][0]['vocabulary'])) {
    $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
  }

  // Build an array of existing term IDs.
  $tids = array();
  foreach ($items as $delta => $item) {
    if (!empty($item['tid']) && $item['tid'] != 'autocreate') {
      $tids[] = $item['tid'];
    }
  }
  $element += array(
    '#type' => 'hierarchical_select',
    '#config' => array(
      'module' => 'hs_taxonomy',
      'params' => array(
        'vid' => isset($vocabulary->vid) ? (int) $vocabulary->vid : NULL,
        'exclude_tid' => NULL,
        'root_term' => isset($field['settings']['allowed_values'][0]['parent']) ? (int) $field['settings']['allowed_values'][0]['parent'] : NULL,
      ),
    ),
    '#default_value' => $tids,
  );
  hierarchical_select_common_config_apply($element, hs_taxonomy_get_config_id($field['field_name']));

  // Set allowed max depth to value saved in config if available.
  $element['#config']['params']['allowed_max_depth'] = isset($element['#config']['allowed_max_depth']) ? (int) $element['#config']['allowed_max_depth'] : NULL;

  // Append another #process callback that transforms #return_value to the
  // format that Field API/Taxonomy Field expects.
  // However, HS' default #process callback has not yet been set, since this
  // typically happens automatically during FAPI processing. To ensure the
  // order is right, we already set HS' own #process callback here explicitly.
  $element_info = element_info('hierarchical_select');
  $element['#process'] = array_merge($element_info['#process'], array(
    'hs_taxonomy_widget_process',
  ));
  return $element;
}