I've used this thread to successfully get my dateTime input fields to repopulate.
The problem I'm seeing now is that while values in the DB are correct, the field input values are always a day behind.
For example, the data in my DB looks like:
....
"startDate":{"date":"5\/4\/2015"}
....
But, the value that is getting rendered in the filed (from my settings page) is this:
5/3/2015
If I save the settings (no errors). The values in the DB will be 5/3/2015
- but then the field on my settings page will show: 5/2/2015
EDIT
Thank you @carlcs, I am sure you are right - I'm just not doing it...right.
Here is what I have tried:
MyPlugin.php
// this returns nothing when logged in error_log
$settings['myStartDate'] = DateTime::createFromString($settings['seasonStartDate']['date'] );
// this returns the date, but one day in the past
$settings['myStartDate'] = new DateTime( $settings['myStartDate']['date'] );
In my defineSettings()
method, I am setting myStartDate
to a Mixed
type as there is a bug with dateTime
.
....
'myStartDate' => array(
AttributeType::Mixed
),
....
I'm not exactly sure where prepSettings()
should live. Here is the whole file:
MyPlugin.php
public function defineSettings()
{
return array(
'myOpenings' => array(
AttributeType::Number,
'required' => true
),
'myStartDate' => array(
AttributeType::Mixed
),
'myEndDate' => array(
AttributeType::Mixed
)
);
}
public function getSettingsHtml()
{
$settings = $this->getSettings();
if ( isset( $settings['myStartDate']['date'] ) )
{
$settings['myStartDate'] = DateTime::createFromString( $settings['myStartDate']['date'] );
// $settings['myStartDate'] = new DateTime( $settings['myStartDate']['date'] );
error_log($settings['myStartDate']);
}
if ( isset( $settings['myEndDate']['date'] ) )
{
$settings['myEndDate'] = new DateTime( $settings['myEndDate']['date'] );
}
return craft()->templates->render( 'plugin/_settings', array(
'settings' => $settings,
) );
}
EDIT/SOLUTION
@carlcs and @Mats Mikkel Rummelhoff for their help. For anyone coming to this post, here is what my "final" main plugin class file looks like hope it helps.
MyPlugin.php
....
public function defineSettings() {
return array(
'myOpenings' => array(AttributeType::Number, 'required'=>true),
'myStartDate' => array(AttributeType::Mixed, 'required'=>true),
'myEndDate' => array(AttributeType::Mixed, 'required'=>true)
);
}
public function getSettingsHtml() {
$settings = $this->getSettings();
if(isset($settings['myStartDate']['date']))
{
$settings['myStartDate'] = DateTime::CreateFromString($settings['myStartDate']);
}
if(isset($settings['myEndDate']['date']))
{
$settings['myEndDate'] = DateTime::CreateFromString($settings['myEndDate']);
}
return craft()->templates->render('myplugin/_settings', array(
'settings' => $settings
));
}
prepSettings()
goes in your main plugin class, see buildwithcraft.com/docs/plugins/plugin-settings