I've gone through the cck form editing handbook info and am hoping there's an easier solution than trying to retheme all my cck add/edit forms. I'd like to have help text display above input fields.

Comments

Anonymous’s picture

Just override theme_form_element in your theme's template.php. Just move the output of the description to the point you want.

seanberto’s picture

Thanks to bangpound, I was able to overwrite where description fields display on form elements in my template.php file as show below. Note for my purposes, descriptions should default to appearing above the input field. However, there are a few instances where it does make sense in my application for the descriptions to appear below, so I have an if statement that catches specific form element ids for which drupal's standard layout should be respected.

Oh, and there's also a bit of code in there that checks field titles to see if they end with a question mark. If so, it replaces the colon delimiter after the field title. (I'm too lazy to take it out for this example. ;) )

function phptemplate_form_element($element, $value) {
  $output  = '<div class="form-item">'."\n";
  $required = !empty($element['#required']) ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>' : '';

  if (!empty($element['#title'])) {
    $title = $element['#title'];
    $delimiter = ":&nbsp;"; // We catch the last character in the title, if it's a "?", we don't use a ":" after the field title.
    if (substr($title, -1) == "?") {
      $delimiter = "&nbsp;";
    }
    if (!empty($element['#id'])) {
      $output .= ' <label for="'. $element['#id'] .'">'. t('!title' . $delimiter . '!required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
    }
    else {
      $output .= ' <label>'. t('!title' . $delimiter . '!required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
    }
  }

  /**
   *
   *  Catch element id, if the form element's id is not in the list below, descriptions comes before input field, otherwise descriptions come above input field.
   *
   */
  $specific_id = $element['#id'];
  $caught_ids = "
                gmap-loc1-locpick_longitude0
                edit-field-before-image-upload
                ";
  if (strstr($caught_ids, $specific_id)) { /* if element id is in the list above, put description below input field */
    $output .= " $value\n";
    if (!empty($element['#description'])) {
      $output .= ' <div class="description">'. $element['#description'] ."</div>\n";
    }
  } else { /* Default formatting */
    if (!empty($element['#description'])) {
      $output .= ' <div class="description">'. $element['#description'] ."</div>\n";
    }
    $output .= " $value\n";    
  }
  
  $output .= "</div>\n";
  
  return $output;
}

Sean Larkin
OpenSourcery.com

Sean Larkin

adshill’s picture

Just for the record - this appears to work fine in D6 also. :) Thanks Sean.

adshill’s picture

There is a slight issue with this in that the formating guidelines appear all messed up with the help above the radio button - and since they all have an individual id based on the field they are being shown on, it is a bit of a minefield... or close to impossible do "en-mass"... is there a possible catch all solution for this problem?

davedg629’s picture

I copied this into my template.php file and it has no effect. I am using D6. Is there anything else I need to do to make this work?

mmilo’s picture

davedg629, that snippet is quite old.

The following (done by modifying http://api.drupal.org/api/function/theme_form_element/6) should work:

function yourthemename_form_element($element, $value) {
  $output  = '<div class="form-item"';
  if (!empty($element['#id'])) {
    $output .= ' id="'. $element['#id'] .'-wrapper"';
  }
  $output .= ">\n";
  $required = !empty($element['#required']) ? '<span class="form-required" title="'. t('This field is required.') .'">*</span>' : '';

  if (!empty($element['#title'])) {
    $title = $element['#title'];
    if (!empty($element['#id'])) {
      $output .= ' <label for="'. $element['#id'] .'">'. t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
    }
    else {
      $output .= ' <label>'. t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
    }
  }
	
  if (!empty($element['#description'])) {
    $output .= ' <div class="description">'. $element['#description'] ."</div>\n";
  }

  $output .= " $value\n";

  $output .= "</div>\n";

  return $output;
}

Don't forget to substitute your theme name in the function name.

davedg629’s picture

Thanks for the quick reply.

I tried the snippet you suggested with no success. I copied it into the bottom of my template.php file and changed the function name to acquia_marina_form_element (I'm using the acquia_marina theme). Sorry, I don't know much about doing theme overrides.

EDIT: I didn't know I had to clear the theme cache. I cleared it and it worked, thanks!

aleagi’s picture

The solution works great for regular fields (one single value) but does not work in multi-value fields.

I need it to adjust the position also with these kind of fields.

Any help would be nice!

Thanks in advance,
aleagi
.

Luiz Gustavo Aleagi Nunes `°º¤ø,¸¸,
http://aleagi.com | http://eopen.com.br
Desenvolvedor Drupal | Padrões W3C | Acessibilidade
¸,ø¤º°`°º¤ø,¸¸,ø¤º°` Nosce te ipsum...¸,ø¤º°``°º¤ø,¸¸,

jamesmcd’s picture

Hello,

Did you manage to find a solution for multi value fields?

Many thanks

JamesWA’s picture

Is there a way to isolate it to just one content type?

Thanks

James

chinita7’s picture

Somebody has worked out for Multi Value Fields already?

chinita7’s picture

This code worked for me.
Only the thing is that the description for password in "account information" on user/%/edit is above the title but not under the title.
How can I fix this?

sheldon rampton’s picture

I've written a module for Drupal 7 that offers a solution to this problem:

http://drupal.org/project/label_help

----------------
Customer Support Engineer, Granicus
https://granicus.com