You are here

function hs_content_taxonomy_widget in Hierarchical Select 5.3

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

Implementation of hook_widget().

File

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

Code

function hs_content_taxonomy_widget($op, &$node, $field, &$node_field) {
  if ($field['widget']['type'] == 'content_taxonomy_hs') {
    $field_name = $field['field_name'];
    $vid = $field['vid'];
    $tid = $field['tid'];
    $depth = empty($field['depth']) ? 0 : $field['depth'];
    switch ($op) {
      case 'form':
        require_once drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
        $form[$field_name]['#tree'] = TRUE;
        $form[$field_name]['tids'] = array(
          '#title' => $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']),
          // The default value comes from $node_field (thus from the CCK
          // storage), unless it's empty, then we check if $node->taxonomy
          // (thus 'normal' Taxonomy storage) contains a value, and use that
          // instead, unless that's empty too.
          // The latter will only work reliably if only one content_taxonomy
          // field is being used, because when you have multiple
          // content_taxonomy fields that use the same vocabulary, there's no
          // way to distinguish.
          '#default_value' => is_array($node_field[$tid]) ? array_keys($node_field[$tid]) : (is_array($node->taxonomy) ? array_keys($node->taxonomy) : array()),
        );
        hierarchical_select_common_config_apply($form[$field_name]['tids'], "content-taxonomy-{$field_name}");
        return $form;
      case 'process form values':

        // TRICKY: this piece of utterly ugly, crappy and dysfunctional code
        // is here thanks to the ugly internal works of the content_taxonomy
        // module that don't make any sense at all. It's necessary to support
        // the 'both' (and 'cck') "save option" of content_taxonomy.
        if (isset($field['save']) && $field['save'] != 'tag') {
          if ($field['multiple'] && is_array($node_field['tids'])) {
            foreach ($node_field['tids'] as $key => $tid) {
              if ($tid != 0) {
                $keys[$tid] = $tid;
              }
            }
          }
          else {
            $keys[$node_field['tids']] = $node_field['tids'];
          }
          $node_field = content_transpose_array_rows_cols(array(
            'value' => $keys,
          ));
        }
        else {
          if (!$field['multiple']) {
            $value = $node_field['tids'];
            $node_field['tids'] = array();
            $node_field['tids'][0] = $value;
          }
          else {
            $value = $node_field['tids'];
            if (!$value) {
              $node_field['tids'] = array();
              $node_field['tids'][0] = $value;
            }
          }
        }
        break;
    }
  }
}