D-L
D-L

Reputation: 255

SilverStripe 3.1.x: How can I show the calendar picker in a DatetimeField?

If I change the DatetimeField to DateField, it works - the calendar picker will show, I have not been able to get it to show in a DatetimeField. Is there a quick and easy way of doing this?

<?php
class Page extends SiteTree {

    private static $db = array(
        "StartDatetime" => "SS_Datetime"
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        //Add a Start Date/time field
        $datetimeField = new DatetimeField("StartDatetime", "Enter a Start Date for the Promotion");
        //Add a calendar picker (only works with DateField but not DatetimeField)
        $datetimeField->setConfig('dateformat', 'dd/MM/yyyy')->setConfig('showcalendar', true);
        $datetimeField->setDescription(sprintf(
            _t('FormField.Example', 'e.g. %s', 'Example format'),
            Convert::raw2xml(Zend_Date::now()->toString($datetimeField->getConfig('dateformat')))
        ));
        $fields->addFieldToTab("Root.Main", $datetimeField);

        return $fields;
    }

Upvotes: 3

Views: 1595

Answers (1)

Stevie
Stevie

Reputation: 96

You need to extract the DateField out from the DatetimeField to do this.

eg.

$datetimeField->getDateField()->setConfig('showcalendar', true);

The same can be done with the TimeField using getTimeField()

Upvotes: 8

Related Questions