This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

jkhamler53742
jkhamler53742

Greetings all,

This is my first forum post and I'm looking forward to receiving your golden nuggets of valuable wisdom.

I'm trying to figure out a way to adjust the format of the datepicker elements globally throughout the backend, primarily from d/m/Y (UK style) to m/d/Y (US style), based on the locale settings, or alternatively a new 'preferred date format' property added to the backend user class.

I have extended the UserPreference model and controller to allow this to be saved, which is working fine. I have been looking into a few approaches, including the following:

  1. Intercept the parsing of the YAML file and dynamically set the date format based on the contents of the above.
  2. Override the DatePicker class somehow and add my logic in there.

Before I get stuck down a rabbit hole of trying to extend/override core October CMS functionality, can anybody point me in the right direction? Perhaps there is a much easier way to do this.

Thanks

Jon

Last updated

mjauvin
mjauvin

Hi Jon, you can definitely use the backend.form.extendFields event to do this. Just make sure the controller/widget model are the ones you want before making the changes in the event handler.

ref. https://octobercms.com/docs/api/backend/form/extendfields

You can use the following code in the handler:

$datepicker = $widget->getField('datepickerFieldName');
$datepicker->config['format'] = $yourFormatFromUserPreference;

Last updated

jkhamler53742
jkhamler53742

Great, thanks for getting back to me so quickly Marc!

mjauvin
mjauvin

Also note: the backend defined locale should define this automatically... if the Backend Locale is set to "french", the datepicker format should be d/m/Y automatically.

mjauvin
mjauvin

The code should actually be:

$datepicker->config['format'] = ...

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.