User login

Multiselect forms with Drupal 5's FAPI

I always have to look this up, so here it is. Two examples for the price of one. Oh, and a link to the official Drupal 5 form documentation.

<?php
/**
 * Implementation of hook_form_alter
 */
function term_message_form_alter($form_id, &$form) {
  switch ($form_id) {
    case 'taxonomy_form_term':
      $tid = $form['tid']['#value'];
      $vid = $form['vid']['#value'];
      $form_add = array();
      $form_add['term_message'] = array(
        '#type' => 'fieldset',
        '#title' => t('Term message settings'),
        '#collapsible' => TRUE,
        '#collapsed' => variable_get('term_message_collapse_form', FALSE),
      );
      $term_messages = variable_get('term_message', array());
      $form_add['term_message']['term_message_data'] = array(     
        '#type' => 'value',
        '#value' => $term_messages,
      );
      $form_add['term_message']['term_message'] = array(
        '#type' => 'textarea',
        '#title' => t('Term message'),
        '#default_value' => (isset($term_messages[$tid])) ? $term_messages[$tid] : '',
        '#description' => t('Provide a message to be displayed on pages of content tagged with this taxonomy term.'),
        '#rows' => 3,
      );
      $vocabulary = taxonomy_get_vocabulary($vid);
      if ($vocabulary->multiple) {
        $options = term_message_terms($vid, $tid);
        $form_add['term_message']['term_message_skip'] = array(
          '#type' => 'select',
          '#title' => t('Suppress message for content with selected terms'),
          '#options' => $options,
          '#default_value' => variable_get('term_message_skip_'. $tid, array()),
          '#multiple' => TRUE,
          '#size' => min(12, count($options)),
          '#description' => t('If one of these terms is also present, do not show the message.'),
        );
      }
      // handled by hook_taxonomy, no custom submit handler needed
      // put the form additions before the buttons
      $pos = array_search('submit', array_keys($form));
      $form = array_merge(array_slice($form, 0, $pos), $form_add, array_slice($form, $pos));       
      break;
    case 'taxonomy_form_vocabulary':
      $vid = $form['vid']['#value'];
      $form_add = array();
      $form_add['term_message'] = array(
        '#type' => 'fieldset',
        '#title' => t('Term message options'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      $show_form = variable_get('term_message_show_form', array($vid => 'all'));
      $form['term_message']['Display term message'] = array(
        '#type' => 'radios',
        '#title' => t('Display term message'),
        '#default_value' => $show_form[$vid],
        '#options' => array(
          'all' => t('For all users (with <em>administer taxonomy</em> permission)'),
          'admin' => t('Only for users with <em>administer term message</em> permission'),
          'none' => t('For no users'),
        ),
        '#description' => t('Please note that this only removes the term message textfield from taxonomy term add and edit forms.  Existing term messages are not removed.'),
      );
      // handled by hook_taxonomy, no custom submit handler needed
      // put the form additions before the buttons
      $pos = array_search('submit', array_keys($form));
      $form = array_merge(array_slice($form, 0, $pos), $form_add, array_slice($form, $pos));       
      break;
  }
}

function term_message_terms($vid, $tid) {
  $options = array();
  $terms = term_message_get_terms_by_vid($vid);
  foreach ($terms as $term) {
    if ($term->tid != $tid) {
      $options[$term->tid] = $term->name;
// if we do use depth for taxonomy tree result:
//    $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
  }
  return $options;
}
?>

Resolution

Searched words: 
FAPI multiple option select form multiselect radio checkboxes dropdown select

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.