Date & Time fields don't have labels for their individual elements.

This is an accessibility issue.

Comments

rooby’s picture

Status: Active » Needs review
StatusFileSize
new1.45 KB

Patch to add invisible labels, so screen readers can read the label.

rooby’s picture

StatusFileSize
new1.18 KB

New version that doesn't add a label for the time period because the individual radio button elements have their own labels.

quicksketch’s picture

Thanks this is a good suggestion. Minor problem here:

+    $element[$type]['#title'] = $none;
+    $element[$type]['#title_display'] = 'invisible';

$none doesn't exist right? This should probably just be NULL, and in that case you probably don't need to set #title_display immediately afterwards.

rooby’s picture

The purpose here is to set titles for screen readers so setting them to null would not help.

$none is the visible value of the empty/none option for each of the date parts, which is either t('Day'), t('Month'), or t('Year').

This is the code in question:

<?php
  // Set defaults according to existing #default_value (set by Form API)
  if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) {
    $default_values = array(
      'month' => $element['#default_value']['month'],
      'day' => $element['#default_value']['day'],
      'year' => $element['#default_value']['year'],
    );
  }
  else {
    $default_values = array(
      'day' => NULL,
      'month' => NULL,
      'year' => NULL,
    );
  }

  // Let Drupal do it's normal expansion.
  $element = form_process_date($element);

  // Set default values.
  foreach ($default_values as $type => $value) {
    switch ($type) {
      case 'month':
        $none = t('Month');
        break;
      case 'day':
        $none = t('Day');
        break;
      case 'year':
        $none = t('Year');
        break;
    }
    unset($element[$type]['#value']);
    $element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
    $element[$type]['#options'] = array('' => $none) + $element[$type]['#options'];
  }
?>
quicksketch’s picture

Ah, okay sorry I should have applied the patch to review it. I'll take another look at this today.

quicksketch’s picture

Status: Needs review » Fixed

Thanks, committed to D7 versions.

Automatically closed -- issue fixed for 2 weeks with no activity.