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; diff --git a/src/Extensions/ControllerExtension.php b/src/Extensions/ControllerExtension.php index 01e140a..8341929 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);", "FlatpickrLocalization"); + } + /** * Answers the calendar highlight color from configuration. * 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; } /**