heimrichhannot/contao-flatpickr-bundle

A bundle to provide flatpickr support for contao.

2.9.4 2024-05-21 13:30 UTC

README

A bundle to provide frontend date- and timepicker using flatpickr.js.

Features

Usage

Install

Install with composer or contao manager and update the database afterwards

composer require heimrichhannot/contao-flatpickr-bundle

Form generator

Create a text field with input validation date, time or date and time and check 'Activate flatpickr'

Filter bundle

Create a filter element of type date, datetime, time or text and check 'Activate flatpickr'. You can also activate flatpickr and pass additional options from dca. See chapter dca configuration for more information. Flatpickr bundle checks the corresponding field dca configuration for filter elements.

DCA / Other frontend forms

Other frontend form systems may work (like formhybrid), if the form widgets are processed by the getAttributesFromDca hook. You need to activate flatpickr in your dca field configuration and activate flatpickr on page level (layout section). The configuration is inherited but can be overridden.

To actived flatpickr for a dca field, set field.eval.flatpickr.active to true.

$GLOBALS['TL_DCA']['tl_content']['fields']['date']['eval']['flatpickr']['active'] = true;

You can customize the picker type with rgxp eval option and pass additional flatpickr configuration:

$GLOBALS['TL_DCA']['tl_content']['fields']['date'] = [
    'eval' => [
        'rgxp' => 'datim' # one of 'date', 'time' or 'datim'
        'flatpickr' => [
            'active' => true,
            'options' => [], // Pass addtional flatpickr option, see configuration chapter
            'plugins' => [], // Use and configure flatpickr plugins, see configuration chapter
          ],
    ]   
];

Configuration

If flatpickr is activated for a field, it already works "out-of-the-box". Sometime additional customization are needed.

Flatpickr picker type

Three picker types are supported: date, time and datetime. The types are automatically selected by setting the rgxp value to one of 'date', 'time' or 'datim'

$field['eval']['rgxp'] = 'time'

Custom configuration

The most config options from flatpickr should work with this bundle.

These options can be set from:

  • DCA (works for filter bundle and other frontend forms): Add the configuration to $field['eval']['flatpickr']['options']
  • php event (work with all types): See developers chapter
  • javascript event (work with all type): see developers chapter

Typical configuration options:

Following additional configuration are available from this bundle:

Plugins

Flatpickr can be extended with plugins. Plugins supported by this bundle can by activated and configured from $field['eval']['flatpickr']['plugins'].

monthSelectPlugin

monthSelectPlugin shows a month-only calendar view. Additional to the default configuration options of the plugin there is an addition disabledMonths option.

Example:

$GLOBALS['TL_DCA']['tl_content']['fields']['date']['eval']['flatpickr']['plugins']['monthSelectPlugin'] = [
    'shorthand' => true,
    'dateFormat' => "M Y",
    'disabledMonths' => [2021 => [0,3,6]]
];

rangePlugin

rangePlugin add a range selection using two inputs. It can be enabled in form generator or from dca:

$GLOBALS['TL_DCA']['tl_content']['fields']['startDate']['eval']['flatpickr']['plugins']['rangePlugin'] = [
    'input' => '#stopDate',
];

Developers

Events

JavaScript events

Update from version 1.x to 2.x

  • add flatpickr => [ active => true ] to the eval array of the dca configuration
  • move the configuration in dca from eval to eval => [ flatpickr => [ options => [ option => value ] ] ]