-1

I'm using the following to format field elements in my theme:

function theme_form_element($element, $value) {
  $t = get_t();

   if ($element['#type'] == 'checkbox') {
     $output = '<div class="checkbox">';
   } elseif ($element['#type'] == 'radio') {
      $output = '<div class="radio">';
   } else {
      $output = '<div class="form-group">';
   };

   $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";
     }
   }

  $output .= " $value\n";

  if (!empty($element['#description'])) {
    $output .= ' <div class="description">' . $element['#description'] . 
  }

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

This gives me the following markup:

<div class="form-group">
  <label for="edit-field-phone-0-value">Phone Number:<span class="form-required" title="This field is required.">*</span></label>
</div>
<input type="text" name="field_phone[0][value]" id="edit-field-phone-0-value" value="" class="phone-field-textfield form-control">

I need <div class="form-group"></div> to wrap both the form label and input. I would also like the label to appear beneath the form element. Below is an example of the markup that I'm looking for:

<div class="form-group">
  <input type="text" name="field_phone[0][value]" id="edit-field-phone-0-value" value="" class="phone-field-textfield form-control">
  <label for="edit-field-phone-0-value">Phone Number:<span class="form-required" title="This field is required.">*</span></label>
</div>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.