Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding i18n/localization and static config option #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/Extensions/ControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace SilverWare\Calendar\Extensions;

use SilverStripe\Core\Extension;
use SilverStripe\i18n\i18n;
use SilverStripe\View\Requirements;

/**
Expand All @@ -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.
*
Expand Down
17 changes: 15 additions & 2 deletions src/Extensions/FormFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down