You are here

function taxonomy_super_select_form_alter in Taxonomy Super Select (TSS) 6

Same name and namespace in other branches
  1. 5 taxonomy_super_select.module \taxonomy_super_select_form_alter()

@file Changes the default taxonomy select box into checkbox or radio buttons.

File

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

Code

function taxonomy_super_select_form_alter(&$form, $form_state, $form_id) {
  drupal_add_css(drupal_get_path('module', 'taxonomy_super_select') . '/taxonomy_super_select.css');

  // Taxonomy Edit Form
  if ($form_id == 'taxonomy_form_vocabulary') {
    $vid = $form['vid']['#value'];
    $vocab = taxonomy_vocabulary_load($vid);
    if ($vocab) {
      $tss = variable_get('taxonomy_super_select_vid_' . $vid, 0);

      // Position the name field higher
      // Add our own submit handler
      $form['#submit'][] = 'taxonomy_super_select_submit';

      // Create fieldset and form elements
      $form['settings']['tss'] = array(
        '#type' => 'fieldset',
        '#title' => t('Taxonomy Super Select'),
        '#collapsed' => !$tss,
        '#collapsible' => TRUE,
        '#tree' => TRUE,
        '#weight' => 3,
      );

      // Get list of all content types
      $types = node_get_types('names');

      // Loop through all types that are enabled for this vocab
      foreach ($vocab->nodes as $index => $type) {
        $options[$type] = $types[$type];
      }
      $options['-all-'] = t('All content types');
      $form['settings']['tss']['taxonomy_super_select_vid_' . $vid]['types'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Enable Taxonomy Select for the Vocabulary Content Types Below'),
        '#options' => $options,
        '#default_value' => $tss ? $tss['types'] : array(),
        '#weight' => -1,
      );
      $form['settings']['tss']['taxonomy_super_select_vid_' . $vid]['parents'] = array(
        '#type' => 'checkbox',
        '#title' => t('Display parent terms as form items'),
        '#default_value' => $tss ? $tss['parents'] : array(),
        '#return_value' => 1,
        '#weight' => 0,
        '#description' => t('Leaving this disabled forces users to select dangling child terms. Useful for grouping terms with descriptive parent terms that are not themselves needed for display.'),
      );
      $form['settings']['tss']['taxonomy_super_select_vid_' . $vid]['collapsible'] = array(
        '#type' => 'checkbox',
        '#title' => t('Make this vocabulary collapsible'),
        '#default_value' => $tss ? $tss['collapsible'] : TRUE,
        '#return_value' => 1,
        '#weight' => 0,
        '#description' => t('Display this vocabulary in a fieldset (collapsible).'),
      );
      $form['settings']['tss']['taxonomy_super_select_vid_' . $vid]['terms_expanded'] = array(
        '#type' => 'checkbox',
        '#title' => t('Make all terms expanded by default'),
        '#default_value' => $tss ? $tss['terms_expanded'] : FALSE,
        '#description' => t('Make all terms expanded by default. If unchecked, only parents having at least a term selected are expanded.'),
        '#weight' => 0,
      );
      $form['settings']['tss']['taxonomy_super_select_vid_' . $vid]['compact'] = array(
        '#type' => 'checkbox',
        '#title' => t('Compact presentation'),
        '#default_value' => $tss ? $tss['compact'] : FALSE,
        '#description' => t('Make the presentation compact (no padding space, no description...)'),
        '#weight' => 0,
      );
      if (module_exists('taxonomy_image')) {
        $form['settings']['tss']['taxonomy_super_select_vid_' . $vid]['image'] = array(
          '#type' => 'checkbox',
          '#title' => t('Allow Taxonomy Image to provide images with the terms'),
          '#default_value' => $tss ? $tss['image'] : array(),
          '#weight' => 2,
        );
      }
    }
  }

  // Node Edit Form
  if (strstr($form_id, '_node_form') && strstr($form['form_id']['#value'], '_node_form')) {
    $content_type = $form['type']['#value'];

    // Get all vocabs for this content type
    $vocabularies = taxonomy_get_vocabularies($content_type);
    $valid_vocabs = array();
    foreach ($vocabularies as $vid => $vocabulary) {
      $tss[$vid] = variable_get('taxonomy_super_select_vid_' . $vid, 0);

      // Only operate on types for a vocabulary that are enabled,
      // and that vocabulary is not removed by vocabperms or other role-based taxonomy
      // permission control module.
      if ((isset($tss[$vid]['types'][$content_type]) || isset($tss[$vid]['types']['-all-'])) && ($vocabulary->tags && $form['taxonomy']['tags'][$vid]['#type'] !== 'value' || !$vocabulary->tags && $form['taxonomy'][$vid]['#type'] !== 'value')) {

        // Show radio or checkbox based on the selection type
        $valid_vocabs[$vid] = $vocabulary->multiple ? 'checkbox' : 'radio';
        if ($vocabulary->tags) {

          // Remove any tags from the autocomplete form item (prevent duplicates)
          $tags[$vid] = $form['taxonomy']['tags'][$vid];
          $tags[$vid]['#required'] = FALSE;
          $tags[$vid]['#parents'] = array(
            'taxonomy',
            'tags',
            $vid,
          );
          $tags[$vid]['#weight'] = -12;
          $tags[$vid]['#title'] = t('Enter New Tags');
          $tags[$vid]['#default_value'] = @$form_state['values']['taxonomy']['tags'][$vid];
          unset($form['taxonomy']['tags'][$vid]);
        }
        else {

          // Remove the default form rendering except for freetagging vocabs
          unset($form['taxonomy'][$vid]);
        }
      }
    }
    $form['#validate'][] = 'taxonomy_super_select_node_form_validate';

    // Go through each enabled vocab and create taxonomy super select
    foreach ($valid_vocabs as $vid => $input) {

      // Get root terms for vocabulary only
      if (!($terms = taxonomy_get_tree($vid, 0, -1, 1))) {
        continue;
      }
      if (module_exists('i18ntaxonomy')) {
        $terms = i18ntaxonomy_localize_terms($terms);
      }
      $form['taxonomy'][$vid] = _tss_branch($vid, $vocabularies[$vid]);
      if (isset($tags[$vid])) {
        $form['taxonomy'][$vid]['tags'] = $tags[$vid];
      }
      _tss_next_nested($terms, $vid, $input, $tss, $form['#node'], $form['taxonomy'][$vid]);
    }
  }
}