You are here

function content_taxonomy_options_widget in Content Taxonomy 5

Same name and namespace in other branches
  1. 6.2 content_taxonomy_options.module \content_taxonomy_options_widget()
  2. 6 content_taxonomy_options.module \content_taxonomy_options_widget()

Implementation of hook_widget().

File

./content_taxonomy_options.module, line 77
Defines a widget type for content_taxonomy for options

Code

function content_taxonomy_options_widget($op, &$node, $field, &$node_field) {
  $vid = $field['vid'];
  $tid = $field['tid'];
  $depth = !empty($field['depth']) ? $field['depth'] : NULL;

  //default values
  $default_values = array();
  if (is_array($node_field[$field['tid']])) {
    $default_values = array_keys($node_field[$field['tid']]);
  }
  else {
    if (is_array($field['widget']['default_value']['tids'])) {
      $default_values = $field['widget']['default_value']['tids'];
    }
    else {
      if (is_array($field['widget']['default_value'])) {
        $default_values = array_keys($field['widget']['default_value']);
      }
    }
  }
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
        '#weight' => $field['widget']['weight'],
      );
      switch ($field['widget']['type']) {
        case 'content_taxonomy_options':
          $options = content_taxonomy_options_array($vid, $field['tid'], $depth, FALSE, TRUE);
          $form[$field['field_name']]['tids'] = array(
            '#type' => $field['multiple'] ? 'checkboxes' : 'radios',
            '#title' => t($field['widget']['label']),
            '#default_value' => $field['multiple'] ? $default_values : reset($default_values),
            '#multiple' => $field['multiple'] ? TRUE : FALSE,
            '#options' => $options,
            '#required' => $field['required'],
            '#description' => t($field['widget']['description']),
          );
          break;
        case 'content_taxonomy_select':
          $options = array();
          if ($field['widget']['group_tid']) {
            $options = content_taxonomy_options_groups($field);
          }
          else {
            $options = content_taxonomy_options_array($vid, $tid, $depth, !$field['multiple'], TRUE);
          }
          $form[$field['field_name']]['tids'] = array(
            '#type' => 'select',
            '#title' => t($field['widget']['label']),
            '#default_value' => $default_values,
            '#multiple' => $field['multiple'] ? TRUE : FALSE,
            '#options' => $options,
            '#required' => $field['required'],
            '#description' => t($field['widget']['description']),
          );
          break;
      }
      return $form;
    case 'validate':
      if ($field['required'] && $node_field['tids'] == 0) {
        form_set_error($field['field_name'], t('The field %d is required', array(
          '%d' => $field['widget']['label'],
        )));
      }
      break;
    case 'process form values':
      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[$key] = $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;
        }
      }
      break;
  }
}