You are here

function _tss_branch in Taxonomy Super Select (TSS) 6

Same name and namespace in other branches
  1. 5 taxonomy_super_select.module \_tss_branch()
2 calls to _tss_branch()
taxonomy_super_select_form_alter in ./taxonomy_super_select.module
@file Changes the default taxonomy select box into checkbox or radio buttons.
_tss_next_nested in ./taxonomy_super_select.module
Recursive function to allow infinite depth vocabularies.

File

./taxonomy_super_select.module, line 331
Changes the default taxonomy select box into checkbox or radio buttons.

Code

function _tss_branch($vid, $term, $value = NULL, $type = 'fieldset', $fieldweight = -1) {
  static $tss = array();
  if (!isset($tss[$vid])) {
    $tss[$vid] = variable_get('taxonomy_super_select_vid_' . $vid, 0);
  }
  $required = empty($term->required) ? '' : '<span class="form-required" title="' . t('This field is required.') . '">*</span>';

  // i18ntaxonomy module integration
  $localize = FALSE;
  if (module_exists('i18ntaxonomy')) {
    $localize = is_numeric($vid) && i18ntaxonomy_vocabulary($vid) == I18N_TAXONOMY_LOCALIZE;
  }
  switch ($type) {
    case 'fieldset':

      // In this section, $term is sometimes actually the vocabulary.
      $is_vocabulary = !isset($term->tid);

      // Automatically expand required vocabs or if the parent term is selected
      $collapsed = $required || !empty($term->parent_value) || $tss[$vid]['terms_expanded'] ? FALSE : TRUE;
      $classes = 'taxonomy-super-select-' . (empty($term->multiple) ? 'radios' : 'checkboxes');
      if ($tss[$vid]['compact'] && !empty($term->is_parent)) {
        $classes .= ' taxonomy-super-select-compact';
      }
      $id = 'taxonomy-' . $vid . (empty($term->is_parent) ? '' : '-' . $term->tid) . '-container';
      $name = $localize ? tt($is_vocabulary ? "taxonomy:vocabulary:{$vid}:name" : "taxonomy:term:{$term->tid}:name", $term->name) : $term->name;
      $form = array(
        '#type' => 'fieldset',
        '#title' => check_plain($name) . $required,
        '#collapsible' => $tss[$vid]['collapsible'],
        '#collapsed' => $collapsed,
        '#weight' => $fieldweight >= 0 ? $fieldweight : $term->weight,
        '#parents' => array(
          'taxonomy',
          $vid,
        ),
        '#description' => $is_vocabulary && !empty($term->help) ? $localize ? tt("taxonomy:vocabulary:{$vid}:help", $term->help) : $term->help : '',
        '#prefix' => '<div class="' . $classes . '" id="' . form_clean_id($id) . '">',
        '#suffix' => '</div>',
      );

      // If we have vocabulary that is single select and not required or is freetagging we need a way to unselect the term
      if ((!$required || !empty($term->tags)) && empty($term->multiple) && isset($term->module) && $term->module === 'taxonomy') {
        $form['none'] = array(
          '#type' => 'radio',
          '#title' => '<em>' . t('Select None') . '</em>',
          '#return_value' => 0,
          '#default_value' => '',
          '#weight' => -12,
          '#parents' => array(
            'taxonomy',
            $term->vid,
          ),
        );
      }
      if (!empty($term->is_parent)) {
        $term->weight = -11;
        $form[$term->tid] = _tss_branch($term->vid, $term, $term->parent_value, $term->parent_type);
      }
      break;
    case 'radio':
    case 'checkbox':
      $name = $localize ? tt("taxonomy:term:{$term->tid}:name", $term->name) : $term->name;
      $description = $localize ? tt("taxonomy:term:{$term->tid}:description", $term->description) : $term->description;
      $form = array(
        '#type' => $type,
        '#title' => check_plain($name),
        '#return_value' => $term->tid,
        '#default_value' => $value,
        '#weight' => $fieldweight >= 0 ? $fieldweight : $term->weight,
        '#attributes' => array(
          'title' => $description,
        ),
      );
      if (!empty($term->is_parent)) {
        $form['#prefix'] = '<div class="taxonomy-super-select-term-parent">';
        $form['#suffix'] = '</div>';
      }
      if ($type == 'radio') {
        $form['#parents'] = array(
          'taxonomy',
          $vid,
        );
      }
      break;
  }
  return $form;
}