0

I have some fields that are "Date and time". I want to ignore the time portion and only solicit date portion from the user. However, since there is already have data in these fields, Drupal does not allow me to easily change the field to "Date". I feel like there must be a simple trick to hide the time portion from the form, but I can't figure out how to do this (without going the install script route of changing a data type). Any ideas? Maybe something in a form_alter? Drupal 9. Thank you

Adding: I am trying to use the hook element_info_alter to hide the time portion, which works well. However, I run into the problem where upon submit, it fails the verification for datetime, so I am trying to duplicate the datetime verification routine, except it will ignores the time portion. BUt I haven't got that working. There must be a Is there a simpler way to do this, or no?

function modulename_element_info_alter(array &$types) {

  $types['datetime']['#process'][] = 'our_datetime_set_format';
}

function our_datetime_set_format($element) {

  $fname = $element['#parents'][0]; //field name
  if (want_dateonly($fname)) {
    $element['time']['#access'] = FALSE;  //hides the time portion
    $element['#date_time_format'] = '';   
    $element['#date_time_element'] = 'none';
    $element['#element_validate'][0] = 'validateDateSkipTime';    
  }
  return $element;
}
2
  • If it possible you can use CSS to hide the time portion
    – Vanish
    Sep 13, 2023 at 4:56
  • Thank you, but that doesn't work because it doesn't pass the verification. I am editing my question to add what I am currently trying.
    – VardeVim
    Sep 14, 2023 at 15:14

1 Answer 1

0

The following seems to work to hide the time portion of a datetime field on a form. The code also prevents the verification from failing when the user submits.

in your modname_form_alter hook function:

    $form['field_date1']['widget'][0]['value']['#date_time_element'] = 'none';
    $form['field_date1']['widget'][0]['value']['#date_time_format'] = '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.