You are here

function datex_form_alter in Datex 7.3

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

Implements hook_form_alter().

Modules to support:

  • Scheduler
  • translation date
  • views exposed forms

s orm_id

Parameters

$f:

File

./datex.module, line 449
Datex main module file, Datex adds php-intl support to drupal.

Code

function datex_form_alter(&$f, &$fs, $form_id) {
  if ($form_id === 'node_admin_content' && isset($f['admin']['nodes']['#options'])) {

    // Contents list (/admin/content/).
    $calendar = datex_factory();
    if (!$calendar) {
      return;
    }
    $format = variable_get('date_format_short', '');
    if (!$format) {
      return;
    }
    foreach ($f['admin']['nodes']['#options'] as &$node) {
      $date =& $node['changed'];
      $date_obj = date_create_from_format($format, $date);
      $calendar
        ->setTimestamp($date_obj
        ->format('U'));
      $date = $calendar
        ->format($format);
    }
    return;
  }
  $calendar = datex_factory(NULL, NULL, 'en');
  if (!$calendar) {
    return;
  }
  if (isset($f['#node_edit_form']) && !module_exists('datex_popup')) {
    $fs['#datexified'] = TRUE;
    $calendar
      ->xSetDate(2017, 03, 22);
    $calendar
      ->setTimestamp($f['created']['#value']);
    if (isset($f['author']['date'])) {
      $f['author']['date']['#element_validate'][] = '_datex_node_edit_form_date_validate';
      $f['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.', [
        '%date' => $calendar
          ->format('Y-m-d H:i:s'),
      ]);
    }
    if (!empty($f['author']['date']['#default_value'])) {
      $f['author']['date']['#default_value'] = $calendar
        ->format('Y-m-d H:i:s O');
    }
  }
  if (isset($f['scheduler_settings']) && !module_exists('date_popup')) {
    foreach ([
      'publish_on',
      'unpublish_on',
    ] as $name) {
      if (isset($f['scheduler_settings'][$name])) {
        $f['scheduler_settings'][$name]['#element_validate'][] = '_datex_node_edit_form_date_validate';
      }
      if (isset($f['#node']->scheduler[$name]) && !empty($f['scheduler_settings'][$name]['#default_value'])) {
        $calendar
          ->setTimestamp($f['#node']->scheduler[$name]);
        $f['scheduler_settings'][$name]['#default_value'] = $calendar
          ->format('Y-m-d H:i:s O');
      }
      $then = $calendar
        ->format('Y-m-d H:i:s O');
      $f['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.', [
        '%date' => $then,
      ]);
    }
  }
  if (isset($f['#id']) && $f['#id'] === 'comment-form' && $f['#entity'] && $f['#entity']->cid && !module_exists('datex_popup')) {
    if (!isset($f['#validate'])) {
      $f['#validate'] = [];
    }
    if (isset($f['author']['date'])) {
      $fmt = 'Y-m-d H:i O';
      $date_obj = date_create_from_format($fmt, $f['author']['date']['#default_value']);
      if ($date_obj) {
        $calendar
          ->setTimestamp($date_obj
          ->format('U'));
        $f['author']['date']['#default_value'] = $calendar
          ->format($fmt);
      }
    }
    array_unshift($f['#validate'], '_datex_comment_edit_form_date_validate');
  }
}