Answers.Body: <p>When you save a draft, all of the fields’ POST data is saved in the craft_entrydrafts table, exactly as it showed up in POST. When the draft is loaded, it’s that raw POST data that is passed to your field type’s <a href="http://buildwithcraft.com/classreference/fieldtypes/IFieldType#getInputHtml-detail" rel="nofollow">getInputHtml()</a> method - not whatever the field type would have returned in <a href="http://buildwithcraft.com/classreference/fieldtypes/IFieldType#prepValueFromPost-detail" rel="nofollow">prepValueFromPost()</a>.</p>
  
  <p>So your getInputHtml() method just needs to be aware that there’s a chance that the $value passed to it may be raw POST data, and react accordingly in that case.</p>
  
  <p>If your field type is storing dates and you’re using Craft’s built-in date/time templates (<a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/templates/_includes/forms/date.html" rel="nofollow">_includes/forms/date.html</a> and <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/templates/_includes/forms/time.html" rel="nofollow">_includes/forms/time.html</a>, which are rendered by that <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/templates/_includes/forms.html#L132" rel="nofollow">dateTimeField()</a> macro in _includes/forms.html), then the POST data will look like this, for whatever input name you passed in.</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>array(
      'date' =&gt; '[localized date string]',
      'time' =&gt; '[localized time string]'
  )
  </code></pre>
  
  <p>That looks like it would be tricky to deal with. Thankfully, Craft’s DateTime class has a handy-yet-awkwardly-named <a href="http://buildwithcraft.com/classreference/etc/dates/DateTime#createFromString-detail" rel="nofollow">createFromString()</a> static method which will check to see if a passed-in value follows that array format, and normalize it into an actual DateTime object for you, taking the localized date/time formats and timezones into account for you.</p>
  
  <p>So all you need to do, from your get field type’s getInputHtml() and prepValueFromPost() methods, is check to see if $value['startDate'] and $value['endDate'] are arrays, and if so, run them through DateTime::createFromString().</p>
  
  <p>We happen to have an existing field type that does the same thing: <a href="https://github.com/pixelandtonic/StoreHours" rel="nofollow">Store Hours</a>. Rather than dealing with just two dates per field, it's actually dealing with with 14 (an Open and Close time for each day of the week), but it should be easy enough to adapt its code for your needs. Here’s its field type file: <a href="https://github.com/pixelandtonic/StoreHours/blob/master/storehours/fieldtypes/StoreHoursFieldType.php" rel="nofollow">https://github.com/pixelandtonic/StoreHours/blob/master/storehours/fieldtypes/StoreHoursFieldType.php</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-22T05:19:10.450Z
Answers.Text: When getInputHtml() returns raw POST data through $value, an array is returned for DateTime fields.  However when passing that array back through $fieldData to templates->render it throws an error when trying to output to the date & time fields.  Is there a specific way to accomplish this?
Answers.UserDisplayName: a-am
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-22T14:29:18.977Z
Answers.Text: @aran I just modified the answer to fully explain how to handle this when dealing with dates.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-16T20:19:19.937Z
Answers.LastActivityDate: 2014-10-22T14:28:58.230Z
Answers.LastEditDate: 2014-10-22T14:28:58.230Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>When entries with plugins custom fields are saved as a draft, the data previously entered in the custom field is not visible. It is saved in the database but doesn't show in the edit screen.  The data will show in the fields when the entry is published. </p>
  
  <pre><code>  {{ forms.dateTimeField({
      label: "Start Date"|t,
      instructions: "When should the event start?"|t,
      id: 'eventStartDate',
      name: name~'[startDate]',
      value: (fieldData ? fieldData.startDate : null),
      errors:''
    }) }}
    {{ forms.dateTimeField({
      label: "End Date"|t,
      instructions: "When should the event end?"|t,
      id: 'eventEndDate',
      name: name~'[endDate]',
      value: (fieldData ? fieldData.endDate : null),
      errors: ''
    }) }}
  </code></pre>
  
  <p>Is there a different way to output the data to the template so it shows in both?</p>
  
Tags: <plugin-development>
Title: My plugin's custom fields don't show values when entry saved as draft
ThreadText: # My plugin's custom fields don't show values when entry saved as draft
  
  (Asked by: username_0 on 2014-10-16)
  
  When entries with plugins custom fields are saved as a draft, the data previously entered in the custom field is not visible. It is saved in the database but doesn't show in the edit screen. The data will show in the fields when the entry is published.
  
  ```
    {{ forms.dateTimeField({
      label: "Start Date"|t,
      instructions: "When should the event start?"|t,
      id: 'eventStartDate',
      name: name~'[startDate]',
      value: (fieldData ? fieldData.startDate : null),
      errors:''
    }) }}
    {{ forms.dateTimeField({
      label: "End Date"|t,
      instructions: "When should the event end?"|t,
      id: 'eventEndDate',
      name: name~'[endDate]',
      value: (fieldData ? fieldData.endDate : null),
      errors: ''
    }) }}
  
  ```
  
  Is there a different way to output the data to the template so it shows in both?
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-16. Score: 3 | Accepted answer
  
  When you save a draft, all of the fields’ POST data is saved in the craft\_entrydrafts table, exactly as it showed up in POST. When the draft is loaded, it’s that raw POST data that is passed to your field type’s [getInputHtml()](http://buildwithcraft.com/classreference/fieldtypes/IFieldType#getInputHtml-detail) method - not whatever the field type would have returned in [prepValueFromPost()](http://buildwithcraft.com/classreference/fieldtypes/IFieldType#prepValueFromPost-detail).
  
  So your getInputHtml() method just needs to be aware that there’s a chance that the $value passed to it may be raw POST data, and react accordingly in that case.
  
  If your field type is storing dates and you’re using Craft’s built-in date/time templates ([\_includes/forms/date.html](https://github.com/pixelandtonic/Craft-Release/blob/master/app/templates/_includes/forms/date.html) and [\_includes/forms/time.html](https://github.com/pixelandtonic/Craft-Release/blob/master/app/templates/_includes/forms/time.html), which are rendered by that [dateTimeField()](https://github.com/pixelandtonic/Craft-Release/blob/master/app/templates/_includes/forms.html#L132) macro in \_includes/forms.html), then the POST data will look like this, for whatever input name you passed in.
  
  ```php
  array(
      'date' => '[localized date string]',
      'time' => '[localized time string]'
  )
  
  ```
  
  That looks like it would be tricky to deal with. Thankfully, Craft’s DateTime class has a handy-yet-awkwardly-named [createFromString()](http://buildwithcraft.com/classreference/etc/dates/DateTime#createFromString-detail) static method which will check to see if a passed-in value follows that array format, and normalize it into an actual DateTime object for you, taking the localized date/time formats and timezones into account for you.
  
  So all you need to do, from your get field type’s getInputHtml() and prepValueFromPost() methods, is check to see if $value['startDate'] and $value['endDate'] are arrays, and if so, run them through DateTime::createFromString().
  
  We happen to have an existing field type that does the same thing: [Store Hours](https://github.com/pixelandtonic/StoreHours). Rather than dealing with just two dates per field, it's actually dealing with with 14 (an Open and Close time for each day of the week), but it should be easy enough to adapt its code for your needs. Here’s its field type file: <https://github.com/pixelandtonic/StoreHours/blob/master/storehours/fieldtypes/StoreHoursFieldType.php>
  
  > **Comments:**
  > **username_0**: When getInputHtml() returns raw POST data through $value, an array is returned for DateTime fields.  However when passing that array back through $fieldData to templates->render it throws an error when trying to output to the date & time fields.  Is there a specific way to accomplish this?  
  > **username_1**: @aran I just modified the answer to fully explain how to handle this when dealing with dates.  
  
  ---
  
  