Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » [solved] DateFormat in HasOneComplexTableField

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

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

Page: 1 2
Go to End
Author Topic: 1618 Views
  • quanto
    Avatar
    Community Member
    81 Posts

    [solved] DateFormat in HasOneComplexTableField Link to this post

    I'm trying to format the date into 'HasOneComplexTableField' and 'FieldSet'. I only try to put the date in the right format in the CMS.

    For the fieldset i've got this code:

    function getCMSFields_forPopup() {
    $fields = new FieldSet();
    $fields->push( new DateField( 'Date', 'Datum') ); //<-- This date has to be formatted into dd-MM-YYYY
    $fields->push( new TextareaField( 'ShortDesc', 'Korte beschrijving' ) );
    $fields->push( new TextareaField( 'LongDesc', 'Lange Beschrijving' ) );

    return $fields;
    }

    For the 'HasOneComplexTableField' i've got this code:

    $tablefield = new HasOneComplexTableField(
    $this,
    'MyCalendar',
    'Calendar',
    array(
    'Date' => 'Datum',//<-- This date has to be formatted into dd-MM-YYYY
    'ShortDesc' => 'Korte beschrijving',
    'LongDesc' => 'Lange beschrijving'
    ),
    'getCMSFields_forPopup'
    );

    In my __config.php i did setup the i18n settings:

    // Set the site locale
    i18n::enable();
    i18n::set_locale('nl_NL');
    i18n::set_date_format('dd-MM-YYYY');

    I tried several things, like $fields->setConfig, but it returns the method 'setconfig' does not exist on 'FieldSet'. Is there any regular way to format these fields right?

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: [solved] DateFormat in HasOneComplexTableField Link to this post

    this is one thing I keep in a Utils class...

    class Utils {
       public static function MakeCalendarPopup($strFieldName,$strLabel) {
          $df = new DateField($strFieldName,$strLabel);
          $df->setConfig('showcalendar', true);
          $df->setLocale('nl_NL');
          $df->setConfig('dateformat', 'dd-MM-YYYY');
          return $df;
       }
    }

    then in cmsfileds....

          $f->replaceField('Date', Utils::MakeCalendarPopup('Date', 'Date:'));

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: [solved] DateFormat in HasOneComplexTableField Link to this post

    Thanks for the calendarpopup! The Fieldset works fine by now. The HasOneComplexTableField doesn't change to the right date. See attached image. Any idea?

    Attached Files
  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: [solved] DateFormat in HasOneComplexTableField Link to this post

    yep, using casting of summary fields like this...

       public static $summary_fields = array (
    ...
          'DateText'         => 'Date',
    ...
       );

       static $casting = array(
          "DateText" => "Text",
       );

       public function DateText() {
          return <<add logic to format date top string here>>;
       }

  • martimiz
    Avatar
    Forum Moderator
    1091 Posts

    Re: [solved] DateFormat in HasOneComplexTableField Link to this post

    Also: using the ComplexTableField::setFieldFormatting() function will format the fields as they appear in the list (not the popup). Something like:

    function formattedDate() {
       //return formatted date
    }

    $tablefield->setFieldFormatting(array(
       'Date' => $this->formattedDate()
    ));

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: [solved] DateFormat in HasOneComplexTableField Link to this post

    Sorry, stupid question: but how do i push the date into the function? $this->Date doesn't work.

  • martimiz
    Avatar
    Forum Moderator
    1091 Posts

    Re: [solved] DateFormat in HasOneComplexTableField Link to this post

    Not a stupid question at all It has been a while, so I didn't quite remember how it went

    It's the Calender object that knows its own date, so this should go into your Calendar class:

    public function getFormattedDate() {
       return date('d-m-Y', strtotime($this->Date)); // replace with however you want to do it
    }

    Next for your ComplexTableField:

    $tablefield->setFieldFormatting(array(
       'Date' => '$FormattedDate')
    ));

    The code '$FormattedDate' will be eval-ed for the Calender object, hence the quotes... So now I remember

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: [solved] DateFormat in HasOneComplexTableField Link to this post

    Hmm. $this->Date still doesn't work . Is there an other option to get the date from the HasOneComplexTableField?

    1618 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.