You are here

function hs_content_taxonomy_widget in Hierarchical Select 6.3

Same name and namespace in other branches
  1. 5.3 modules/hs_content_taxonomy.module \hs_content_taxonomy_widget()

Implementation of hook_widget().

File

modules/hs_content_taxonomy.module, line 153
Implementation of the Hierarchical Select API for the Content Taxonomy module.

Code

function hs_content_taxonomy_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $field_name = $field['field_name'];
  $vid = $field['vid'];
  $tid = content_taxonomy_field_get_parent($field);
  $depth = empty($field['depth']) ? 0 : $field['depth'];
  require_once drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
  $node =& $form['#node'];

  // Extremely ugly checks because CCK/Content Taxonomy is a big failure.
  $selected_items = array();
  if (isset($items[$field_name])) {

    // New node: "default value" is the default value from field settings.
    if (isset($items[$field_name]['tids'])) {

      // Multiple default values as a field setting.
      if (is_array($items[$field_name]['tids'])) {
        foreach ($items[$field_name]['tids'] as $item) {
          $selected_items[] = $item['value'];
        }
      }
      else {
        $selected_items[] = $items[$field_name]['tids'];
      }
    }
  }
  else {

    // Existing node: "default value" are the previously selected terms.
    foreach ($items as $item) {
      $selected_items[] = $item['value'];
    }
  }
  $node_field =& $node->{$field_name};
  $form[$field_name]['#tree'] = TRUE;
  $form[$field_name]['#weight'] = $field['widget']['weight'];
  $form[$field_name]['tids'] = array(
    '#title' => t($field['widget']['label']),
    '#type' => 'hierarchical_select',
    '#weight' => $field['widget']['weight'],
    '#config' => array(
      'module' => 'hs_content_taxonomy',
      'params' => array(
        'vid' => $vid,
        'tid' => $tid,
        'depth' => $depth,
      ),
    ),
    '#required' => $field['required'],
    '#description' => t($field['widget']['description']),
    '#default_value' => !empty($selected_items) ? array_values($selected_items) : array(),
  );
  unset($form[$field_name]['#options']);

  // Unset to prevent passing around of possibly huge HTML.
  unset($form[$field_name]['#theme']);

  // Unset to prevent theme_taxonomy_term_select() from running.
  hierarchical_select_common_config_apply($form[$field_name]['tids'], "content-taxonomy-{$field_name}");
  return $form;
}