Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Customizing the SS_Datetime with setConfig [solved]


Go to End


4 Posts   2507 Views

Avatar
Adesso

Community Member, 10 Posts

17 September 2014 at 9:54pm

Edited: 18/09/2014 8:18pm

For the setup code, please reffer to Articles that only have relations to Other Pages

When creating a Holding Page and then a Article page the edit mask displays the SS_Datetime. This field will be used later to sort the articles with and I would like to use the setConfig function 'showcalander' as described in the Tutorial.

Now I am sure that the Date could not be all that difficult, but I have no clue yet how to do it, and the next obvious question would be, is there a time picker :?

Greetings from Germany

Avatar
Adesso

Community Member, 10 Posts

18 September 2014 at 12:39am

Edited: 18/09/2014 7:49pm

I could not find a way to get a DateTime-Picker, so I ended up using 2 Fields.

First add the extra field CreatTime in the $db (rebuild!)
and then Create a getCMSFields override function
create, config and add the fields, and it works.

Updated Article.php

<?php
class Article extends DataObject {
	private static $db = array(
		'Heading' => 'Varchar',
		'CreateDate' => 'Date',
		'CreateTime' => 'Time',
		'Author' => 'Varchar',
		'Content' => 'HTMLText'
	);
	private static $belongs_many_many = array(
		'Article' => 'ArticleHolder'
	);
	public static $summary_fields = array(
		'Heading' => 'Heading',
		'CreateDate' => 'Date',
		'Author' => 'Author'
	);
	static $default_sort = "CreateDate ASC";
	private static $searchable_fields = array(
		'Heading' => 'PartialMatchFilter'
	);
	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$dateField = new DateField('CreateDate','Date','Creation Date?');
		$timeField = new TimeField('CreateTime','Time','now');
		$dateField->setConfig('showcalendar', true);
		$timeField->setConfig('use_strtotime', true);
		$fields->addFieldToTab('Root.Main',$dateField,'Author');
		$fields->addFieldToTab('Root.Main',$timeField,'Author');
		return $fields;
    }
}

Any improvements are always welcome.

Avatar
apiening

Community Member, 60 Posts

18 September 2014 at 5:26am

Edited: 18/09/2014 9:28am

Hi Adesso,

you probably missed the fact that you are dealing with a DatetimeField not a DateField. You cannot set the showcalendar option on a DatetimeField. The DatetimeField consists of three fields, a DateField, a TimeField and a HiddenField for the timezone. So you have to get hold of the DateField. Here is what you do:

class MyClass extends DataObject
{
    private static $db = array(
        'MyDatetimeFiled' => 'SS_Datetime',
    );

    public function getCMSFields()
        $fields = parent::getCMSFields();
        // You don't have to create the field since it's already been created by the scaffolder. You just set the option.
        $fields->dataFieldByName('MyDatetimeField')->getDateField()->setConfig('showcalendar', true);
        return $fields;
    }
}

Afaik there is no SS-timepicker.

Grüße aus HH

andy

Avatar
Adesso

Community Member, 10 Posts

18 September 2014 at 8:13pm

Edited: 18/09/2014 8:31pm

Great stuff,

with your suggestions I managed to get back to the original idea of a DateTime object with picker.
http://api.silverstripe.org/3.1/class-FormField.html#$containerFieldList

I added another setting for the time field getTimeField.
Here is the resulting code that creates a DateTime Object with Date-Picker and the current time on creation.

class MyClass extends DataObject { 
	private static $db = array( 
		'MyDatetimeFiled' => 'SS_Datetime', 
	);

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->dataFieldByName('MyDatetimeField')->getDateField()->setConfig('showcalendar', true);
		$fields->dataFieldByName('MyDatetimeField')->getTimeField()->setConfig('use_strtotime', true);
		$fields->dataFieldByName('MyDatetimeField')->getTimeField()->setValue('now');
		return $fields;
	} 
}

Thanks for the help ;)
Grüße aus Frankfurt