User login

Fieldsets in Drupal forms: FAPI 5 syntax

Drupal fieldsets can also have a description.

<?php
    $form['placement_imagecache'] = array(
      '#type' => 'fieldset',
      '#title' => t('Imagecache settings per content type'),
      '#description' => t('Please note that if you have just selected new content types with imagefields above, you must submit this form before the imagecache presets available can be set below.  If no presets are selected all will be available to that content type.'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
?>

From this function for providing the admin form for Agaric's field placement module, which has some other interesting things going on that you should not look at:

<?php
function placement_admin_form() {
  $form['#tree'] = TRUE;
  $options = array();
  $node_types = array();  // used to get the node type name later
  $result = db_query('SELECT type_name AS type, nt.name AS name, nf.field_name AS field_name, nfi.label AS label from {node_field} nf LEFT JOIN {node_field_instance} nfi ON (nf.field_name = nfi.field_name) LEFT JOIN {node_type} nt ON nt.type = type_name WHERE nf.type = "%s"', "image");
  while ($imagefield = db_fetch_object($result) )
  {
    $option_value = $imagefield->type . "+" . $imagefield->field_name;
    $option_name = $imagefield->name . ": " . $imagefield->label;
    $options[$option_value] = $option_name;
    $node_types[$imagefield->type] = $imagefield->name;
  }
  if (empty($options)) {
    $form['placement_imagefield'] = array(
      '#type' => 'markup',
      '#value' => t('No content types with imagefields are available.  You can add an image field to a <a href="/admin/content/types">content type</a>.'),
    );
    if (!module_exists('imagefield')) {
      $form['placement_imagefield_but'] = array(
        '#type' => 'markup',       
        '#value' => t('But you will have to install the <a href="http://drupal.org/project/imagefield">imagefield module</a> first.'),
      );
    }
  }
  else {
    $available_node_types = variable_get('placement_imagefield', array());
    $form['placement_imagefield'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#options' => $options,
      '#title' => t('Select image fields'),
      '#default_value' => $available_node_types,
      '#description' => t("These image fields from these content types are available to be placed in node edit forms."),
    );
  }
  if (function_exists('_imagecache_get_presets')) {   // as opposed to module_exists('imagecache')  - just cut to the chase
    // comes in the form: array (  1 => 'exampleset', )
    $imagecache_presets = _imagecache_get_presets();
    $form['placement_imagecache'] = array(
      '#type' => 'fieldset',
      '#title' => t('Imagecache settings per content type'),
      '#description' => t('You must selected content types with imagefields above and <em>submit this form</em> before imagecache presets available to each can be set below.  Also, for each content type, if no presets are selected all will be available.'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['placement_imagecache']['placement_imagecache_presets'] = array();
    $types = placement_available_node_types_and_fields();  // placement_imagefields is default
    foreach (array_keys($types) AS $node_type) {
      $type_presets = placement_available_imagecache_presets_get($node_type);
      $form['placement_imagecache']['placement_imagecache_presets'][$node_type] = array(
        '#type' => 'select',
        '#title' => $node_types[$node_type], // $nt_name,
        '#multiple' => TRUE,
        '#options' => $imagecache_presets,
        '#default_value' => array_keys($type_presets),
      );
    }
  }
  return system_settings_form($form);
}
?>

Reference

http://api.drupal.org/api/file/developer/topics/forms_api.html/5

Resolution

Searched words: 
fieldset in Drupal forms field set

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.