You are here

function form_process_creditfield_date in Creditfield Form Element 7

Element process callback; adds support for credit expiration date entry.

Parameters

$element: An associative array containing the properties of the element.

Return value

The processed element.

1 string reference to 'form_process_creditfield_date'
creditfield_element_info in ./creditfield.module
Implements hook_element_info().

File

./creditfield.module, line 129

Code

function form_process_creditfield_date($element) {

  // Default to current date
  if (empty($element['#value'])) {
    $element['#value'] = array(
      'month' => format_date(REQUEST_TIME, 'custom', 'n'),
      'year' => format_date(REQUEST_TIME, 'custom', 'Y'),
    );
  }
  $element['#tree'] = TRUE;

  // Determine the order of month & year in the site's chosen date format.
  $format = variable_get('date_format_short', 'm/Y');
  $sort = array();
  $sort['month'] = max(strpos($format, 'm'), strpos($format, 'M'));
  $sort['year'] = strpos($format, 'Y');
  asort($sort);
  $order = array_keys($sort);

  // Output multi-selector for date.
  foreach ($order as $type) {
    switch ($type) {
      case 'month':
        $options = drupal_map_assoc(range(1, 12), 'map_month');
        $title = t('Month');
        break;
      case 'year':
        $options = drupal_map_assoc(range(date('Y', time()), date('Y', time()) + 10));
        $title = t('Year');
        break;
    }
    $element[$type] = array(
      '#type' => 'select',
      '#title' => $title,
      '#title_display' => 'invisible',
      '#value' => $element['#value'][$type],
      '#attributes' => $element['#attributes'],
      '#options' => $options,
    );
  }
  return $element;
}