From d37f6a50460baa07209a437acc1b7ca6e719ccb4 Mon Sep 17 00:00:00 2001 From: Michael van Schaik Date: Tue, 2 Jul 2019 12:44:19 +0200 Subject: [PATCH 1/4] Load flatpickr locale file for current locale --- src/Extensions/ControllerExtension.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Extensions/ControllerExtension.php b/src/Extensions/ControllerExtension.php index 01e140a..8663232 100644 --- a/src/Extensions/ControllerExtension.php +++ b/src/Extensions/ControllerExtension.php @@ -18,6 +18,7 @@ namespace SilverWare\Calendar\Extensions; use SilverStripe\Core\Extension; +use SilverStripe\i18n\i18n; use SilverStripe\View\Requirements; /** @@ -43,6 +44,19 @@ public function onBeforeInit() } } + /** + * Event handler method triggered after the extended controller has initialised. + * + * @return void + */ + public function onAfterInit() + { + // (try &) load locale file + set default locale for flatpickr to current users' i18n locale + $userLangIso = i18n::getData()->langFromLocale(i18n::get_locale()); + Requirements::javascript("//npmcdn.com/flatpickr/dist/l10n/$userLangIso.js"); + Requirements::customScript("flatpickr.localize(flatpickr.l10ns.$userLangIso);"); + } + /** * Answers the calendar highlight color from configuration. * From 4546e0e570d8f268a948e4c96f0594fa2b531b88 Mon Sep 17 00:00:00 2001 From: Michael van Schaik Date: Tue, 2 Jul 2019 12:47:12 +0200 Subject: [PATCH 2/4] Merge instance config with default (Yaml-based) config --- src/Extensions/FormFieldExtension.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Extensions/FormFieldExtension.php b/src/Extensions/FormFieldExtension.php index d5c2d9b..ebf3245 100644 --- a/src/Extensions/FormFieldExtension.php +++ b/src/Extensions/FormFieldExtension.php @@ -17,6 +17,7 @@ namespace SilverWare\Calendar\Extensions; +use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Convert; use SilverStripe\Core\Extension; use SilverStripe\Forms\FormField; @@ -32,6 +33,13 @@ */ class FormFieldExtension extends Extension { + use Configurable; + + /** + * (Standard/default) config object for static flatpickr config (merged with instance $calendarConfig) + */ + private static $flatpickr_base_config = []; + /** * Holds the calendar picker config for the extended object. * @@ -74,11 +82,16 @@ public function setCalendarConfig($arg1, $arg2 = null) */ public function getCalendarConfig($name = null) { + $mergedConfig = array_merge( + $this->config()->get('flatpickr_base_config'), + $this->calendarConfig + ); + if (!is_null($name)) { - return isset($this->calendarConfig[$name]) ? $this->calendarConfig[$name] : null; + return isset($mergedConfig[$name]) ? $mergedConfig[$name] : null; } - return $this->calendarConfig; + return $mergedConfig; } /** From 30e2997af7ef512534be3839078c762000a2e413 Mon Sep 17 00:00:00 2001 From: Michael van Schaik Date: Tue, 2 Jul 2019 12:52:03 +0200 Subject: [PATCH 3/4] add static config options & locale info --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d504aef..3b32e51 100644 --- a/README.md +++ b/README.md @@ -90,10 +90,18 @@ SilverStripe\Forms\DateField: Out of the box, the module will automatically add a [flatpickr][flatpickr] to all `DateField`, `DatetimeField` and `TimeField` instances. Each field will -be configured automatically with default settings for each use case. +be configured automatically with default settings for each use case. A flatpickr +language file will be loaded and used (if available) based on the current i18n locale. If you need to apply additional options supported by [flatpickr][flatpickr], you -can do so by using the `setCalendarConfig()` method: +can do so by using YAML config (static), or the `setCalendarConfig()` method (instance): + +```yml +SilverWare\Calendar\Extensions\FormFieldExtension: + flatpickr_base_config: + time_24hr: true + altFormat: "l j F Y \\o\\m H:i" +``` ```php use SilverStripe\Forms\DateField; From 6da27cee29ec47dedabfd4ff637ae755ea0baefd Mon Sep 17 00:00:00 2001 From: Michael van Schaik Date: Sun, 15 Sep 2019 07:17:33 +0200 Subject: [PATCH 4/4] Add a uniqueness id to the customscript --- src/Extensions/ControllerExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extensions/ControllerExtension.php b/src/Extensions/ControllerExtension.php index 8663232..8341929 100644 --- a/src/Extensions/ControllerExtension.php +++ b/src/Extensions/ControllerExtension.php @@ -54,7 +54,7 @@ public function onAfterInit() // (try &) load locale file + set default locale for flatpickr to current users' i18n locale $userLangIso = i18n::getData()->langFromLocale(i18n::get_locale()); Requirements::javascript("//npmcdn.com/flatpickr/dist/l10n/$userLangIso.js"); - Requirements::customScript("flatpickr.localize(flatpickr.l10ns.$userLangIso);"); + Requirements::customScript("flatpickr.localize(flatpickr.l10ns.$userLangIso);", "FlatpickrLocalization"); } /**