You are here

function social_event_date_validate in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/social_event.module \social_event_date_validate()
  2. 8.3 modules/social_features/social_event/social_event.module \social_event_date_validate()
  3. 8.4 modules/social_features/social_event/social_event.module \social_event_date_validate()
  4. 8.6 modules/social_features/social_event/social_event.module \social_event_date_validate()
  5. 8.7 modules/social_features/social_event/social_event.module \social_event_date_validate()
  6. 8.8 modules/social_features/social_event/social_event.module \social_event_date_validate()
  7. 10.3.x modules/social_features/social_event/social_event.module \social_event_date_validate()
  8. 10.0.x modules/social_features/social_event/social_event.module \social_event_date_validate()
  9. 10.1.x modules/social_features/social_event/social_event.module \social_event_date_validate()
  10. 10.2.x modules/social_features/social_event/social_event.module \social_event_date_validate()

Set default time to the date field if time was not set.

1 string reference to 'social_event_date_validate'
social_event_date_after_build in modules/social_features/social_event/social_event.module
Add custom validation to event date fields.

File

modules/social_features/social_event/social_event.module, line 512
The Social event module.

Code

function social_event_date_validate(&$element, FormStateInterface $form_state, &$complete_form) {
  $input = NestedArray::getValue($form_state
    ->getValues(), $element['#parents']);
  $form_input = $form_state
    ->getUserInput();

  // Skip default validation for required time when date is required.
  if (!empty($input['date']) && (empty($input['time']) || !empty($form_input['event_all_day']))) {
    $input['time'] = '00:01:00';
    $storage_format = DrupalDateTime::FORMAT;
    $datetime = trim($input['date'] . ' ' . $input['time']);
    $storage_timezone = new DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE);
    $system_timezone = date_default_timezone_get();
    $zone = !empty($system_timezone) ? new DateTimeZone($system_timezone) : $storage_timezone;
    $input['object'] = DrupalDateTime::createFromFormat($storage_format, $datetime, $zone);
    if ($input['object'] instanceof DrupalDateTime) {
      $form_state
        ->setValueForElement($element, $input);
    }
  }
}