You are here

function content_taxonomy_activeselect_widget in Content Taxonomy 5

Implementation of hook_widget().

File

./content_taxonomy_activeselect.module, line 83
Defines a widget type for content_taxonomy with activeselects

Code

function content_taxonomy_activeselect_widget($op, &$node, $field, &$node_field) {
  $vid = $field['vid'];
  $tid = $field['tid'];

  //dprint_r($op);
  switch ($op) {
    case 'prepare form values':
      if (!isset($node_field['default_tid'])) {
        $node_field['default_tid'] = $_POST[$field['field_name']]['tid'];
        $node_field['default_children'] = $_POST[$field['field_name']]['children'];
        $node_field['default_grandchildren'] = $_POST[$field['field_name']]['grandchildren'];
      }
      break;
    case 'form':
      $form = array();
      $children = taxonomy_get_children($tid, $vid);
      foreach ($children as $term) {
        $options_terms[$term->tid] = $term->name;
      }
      if (is_array($node_field['default_children'])) {
        $default_children = implode(',', $node_field['default_children']);
      }
      else {
        $default_children = $node_field['default_children'];
      }
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      $form[$field['field_name']]['tid'] = array(
        '#title' => $field['widget']['label'],
        '#type' => 'activeselect',
        '#activeselect_path' => 'content_taxonomy/activeselect',
        '#activeselect_targets' => str_replace('_', '-', $field['field_name']) . '-children',
        '#activeselect_extra' => 'term,' . FALSE . ',' . $field['vid'] . ',' . str_replace('_', '-', $field['field_name']) . '-children@' . $default_children,
        '#default_value' => isset($node_field['default_tid']) ? $node_field['default_tid'] : array(),
        '#options' => $options_terms,
        '#required' => $field['required'],
        '#DANGEROUS_SKIP_CHECK' => TRUE,
      );
      $form[$field['field_name']]['children'] = array(
        '#title' => $field['widget']['label_children'],
        '#options' => array(),
        '#multiple' => FALSE,
        '#required' => $field['required'],
        '#default_value' => isset($node_field['default_children']) ? $node_field['default_children'] : "",
        '#DANGEROUS_SKIP_CHECK' => TRUE,
      );
      if (!$field['widget']['grandchildren']) {
        $form[$field['field_name']]['children']['#type'] = 'select';
      }
      else {
        if (is_array($node_field['default_grandchildren'])) {
          $default_grandchildren = implode(',', $node_field['default_grandchildren']);
        }
        else {
          $default_grandchildren = $node_field['default_grandchildren'];
        }
        $form[$field['field_name']]['children']['#type'] = 'activeselect';
        $form[$field['field_name']]['children']['#activeselect_path'] = 'content_taxonomy/activeselect';
        $form[$field['field_name']]['children']['#activeselect_targets'] = str_replace('_', '-', $field['field_name']) . '-grandchildren';
        $form[$field['field_name']]['children']['#activeselect_extra'] = 'term,' . FALSE . ',' . $field['vid'] . ',' . str_replace('_', '-', $field['field_name']) . '-grandchildren@' . $default_grandchildren;
        $form[$field['field_name']]['grandchildren'] = array(
          '#title' => $field['widget']['label_grandchildren'],
          '#type' => 'select',
          '#options' => array(),
          '#required' => $field['required'],
          '#default_value' => isset($node_field['default_grandchildren']) ? $node_field['default_grandchildren'] : "",
          '#DANGEROUS_SKIP_CHECK' => TRUE,
        );
      }
      return $form;
      break;
    case 'process form values':
      $tids = array();
      foreach ($node_field as $type => $value) {
        if (!($type == "tid" || $type == "children" || $type == "grandchildren")) {
          continue;
        }
        if (is_array($value)) {
          foreach ($value as $tid) {
            if ($tid != 0) {
              $tids[] = $tid;
            }
          }
        }
        else {
          if ($value != 0) {
            $tids[] = $value;
          }
        }
      }
      $node_field = array();
      $node_field['tids'] = $tids;
      break;
  }
}