You are here

function datex_form_alter in Datex 7.2

Same name and namespace in other branches
  1. 7.3 datex.module \datex_form_alter()
  2. 7 datex.module \datex_form_alter()

Implements hook_form_alter().

Looks for node editing forms and adds a Jalali calendar to author information editing field in vertical tabs.

File

./datex.module, line 225
Convert output of date_format() to Jalali in a patched drupal installation.

Code

function datex_form_alter(&$form, &$form_state, $form_id) {
  $calendar = _datex_factory_with_defaults();

  // Make sure this is a node edit form and date element is present.
  if (!$calendar || !isset($form['#node_edit_form'])) {
    return;
  }
  $calendar
    ->setFormat('o-m-d H:i:s O');
  $now = $calendar
    ->format();
  $validator = array(
    '_datex_node_edit_form_date_validate',
  );
  if (isset($form['author']['date'])) {
    $t_args = array(
      '%date' => $now,
    );
    $form['author']['date']['#description'] = t('Format: %date The date format is YYYY-MM-DD and time is H:i:s. Leave blank to use the time of form submission.', $t_args);
    $form['author']['date']['#element_validate'] = $validator;
  }
  $nonpatching_mode = _datex_cfg('mode') == DATEX_NONPATCHING_MODE;
  if (isset($form['scheduler_settings'])) {
    foreach (array(
      'publish_on',
      'unpublish_on',
    ) as $name) {
      if (isset($form['scheduler_settings'][$name])) {
        $form['scheduler_settings'][$name]['#element_validate'] = $validator;
      }
      if ($nonpatching_mode) {
        if (isset($form['#node']->scheduler[$name]) && !empty($form['scheduler_settings'][$name]['#default_value'])) {
          $calendar
            ->setTimestamp($form['#node']->scheduler[$name]);
          $form['scheduler_settings'][$name]['#default_value'] = $calendar
            ->format('Y-m-d H:i:s O');
        }
        $form['scheduler_settings'][$name]['#description'] = t('Format: %date The date format is YYYY-MM-DD and time is H:i:s. Leave blank to disable scheduled.', array(
          '%date' => $now,
        ));
      }
    }
  }
  if ($nonpatching_mode && !empty($form['author']['date']['#default_value'])) {
    $calendar
      ->setTimestamp($form['created']['#value']);
    $form['author']['date']['#default_value'] = $calendar
      ->format('Y-m-d H:i:s O');
  }
}