0

I'm new to Drupal 8. In the user registration form, i've added a Date field via manage fields in user account settings. However, I need to customize this field and change its format. I tried 'manage display' but it's not taking effect. Is there something I'm missing. (Ideally, it should look and behave just like field datetime in Drupal 8 webform)

Appreciate any help given.

Thanks

0

You can implement template_preprocess_field() and alter the markup of the date field.

function MY_THEME_preprocess_field(&$vars) {
  if ($vars['field_name'] === 'field_custom_date_field') {
    $date_format = 'short'; // You need to provide the machine name of the format you want.
    $timestamp = strtotime($vars['element']['#items']->value);
    $markup = \Drupal::service('date.formatter')->format($timestamp, $date_format);
    $vars['items'][0]['content']['#text'] = $markup;
  }
}
| improve this answer | |
  • Hi, sorry i've been extremely busy, i'll test it in the coming two days and let you know, thanks – ram_griever Jan 21 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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