From 7c76b39b98bb65e2fbd3344e954e92cdedf6aeda Mon Sep 17 00:00:00 2001
From: Chris Pliakas <cpliakas@266779.no-reply.drupal.org>
Date: Thu, 17 Mar 2011 14:17:31 -0400
Subject: [PATCH] Issue #600236 by cpliakas, anrikun: Added 508 compliance fix for date popup form.

---
 date_popup/date_popup.module |   57 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/date_popup/date_popup.module b/date_popup/date_popup.module
index f219a72..58c5dcb 100644
--- a/date_popup/date_popup.module
+++ b/date_popup/date_popup.module
@@ -158,7 +158,12 @@ function date_popup_js_settings_id($id, $func, $settings) {
 
 function date_popup_theme() {
   return array(
-    'date_popup' => array('arguments' => array('element' => NULL)),
+    'date_popup' => array(
+      'arguments' => array('element' => NULL)
+    ),
+    'date_popup_wrapper_element' => array(
+      'arguments' => array('element' => NULL, 'value' => NULL),
+    ),
     );
 }
 
@@ -241,6 +246,11 @@ function date_popup_process($element, $edit, $form_state, $form) {
   $element['date'] = date_popup_process_date($element, $edit, $date);
   $element['time'] = date_popup_process_time($element, $edit, $date);
 
+  // Re-sets the "id" attribute for 508 compliance. This will make sure that the
+  // "for" attribute in the label element matches the "id" attribute in the
+  // input element.
+  $element['#id'] = $element['date']['#id'];
+
   if (isset($element['#element_validate'])) {
     array_push($element['#element_validate'], 'date_popup_validate');
   }
@@ -594,7 +604,50 @@ function theme_date_popup($element) {
   if (isset($element['#children'])) {
     $output = $element['#children'];
   }
-  return '<div class="'. $class .'">'. theme('form_element', $element, $output) .'</div>';
+  return '<div class="'. $class .'">'. theme('date_popup_wrapper_element', $element, $output) .'</div>';
+}
+
+/**
+ * Return a themed wrapper element for the date popup form.
+ *
+ * @param element
+ *   An associative array containing the properties of the element.
+ *   Properties used: title, description, id, required
+ * @param $value
+ *   The form element's data.
+ * @return
+ *   A string representing the form element.
+ *
+ * @ingroup themeable
+ * @see theme_form_element()
+ */
+function theme_date_popup_wrapper_element($element, $value) {
+  $output = '<div class="form-item"';
+  if (!empty($element['#id'])) {
+    $output .= ' id="'. $element['#id'] .'-outer-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";
+    }
+  }
+
+  $output .= " $value\n";
+
+  if (!empty($element['#description'])) {
+    $output .= ' <div class="description">'. $element['#description'] ."</div>\n";
+  }
+
+  $output .= "</div>\n";
+
+  return $output;
 }
 
 /**
-- 
1.7.4.1

