You are here

function datex_date_select_process_alter in Datex 7.2

Same name and namespace in other branches
  1. 7.3 datex_date.inc \datex_date_select_process_alter()

Handles date field.

File

./datex_date.inc, line 105
Provides Jalali support for date module.

Code

function datex_date_select_process_alter(&$element, &$form_state, $context) {
  if (isset($element['#instance']['widget']['settings']['datex_schema'])) {
    $schema = $element['#instance']['widget']['settings']['datex_schema'];
  }
  else {
    $schema = 'default';
  }
  $tz = $element['#date_timezone'];
  if (!($calendar = _datex_field_calendar($element['#granularity'], $schema, $tz))) {
    return;
  }
  $calendar
    ->setTimestamp(strtotime($element['#default_value']));
  $year_now = $calendar
    ->format('Y');
  static $convert = array(
    'year' => 'Y',
    'month' => 'n',
    'day' => 'j',
  );
  foreach ($convert as $name => $format) {
    $v = $calendar
      ->format($format);
    if (isset($element[$name]) && !empty($element[$name]['#default_value'])) {
      $element[$name]['#default_value'] = $v;
    }
    if (isset($element[$name]) && !empty($element['#value'][$name])) {
      $element['#value'][$name] = $v;
    }
  }

  // Generating Year Option (if field has year granularity):
  if (in_array('year', $element['#granularity'])) {
    list($year_before, $year_after) = explode(':', $element['#date_year_range']);

    // If year field is not required, add an empty value as drupal does.
    if ($element['#required']) {
      $element['year']['#options'] = array();
    }
    else {
      $element['year']['#options'] = array(
        '' => t('-Year'),
      );
    }

    // Now add actual year options.
    for ($year = $year_now + $year_before; $year <= $year_now + $year_after; $year++) {
      $element['year']['#options'][$year] = $year;
    }
  }

  // Generating month Option (if field has month granularity).
  if (in_array('month', $element['#granularity'])) {
    $names = $calendar
      ->getInfo('names');

    // If field is not required, add an empty value as drupal does.
    if ($element['#required']) {
      $element['month']['#options'] = array();
    }
    else {
      $element['month']['#options'] = array(
        '' => t('-Month'),
      );
    }

    // Now add month options.
    for ($i = 0; $i < 12; $i++) {
      $element['month']['#options'][$i + 1] = $names['months'][$i];
    }
  }
}