Chris
Chris

Reputation: 262

Reusable summary_fields getter Silverstripe

I have a couple of Time datatypes in my summary_fields array that I'd like to modify with the same getter method, but it doesn't seem possible to pass properties to them. My original thought was:

class BusinessHour extends DataObject {

    private static $db = array(
        'Title' => 'Varchar(9)',
        'Day' => 'Enum("Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday", "Monday")',
        'Open' => 'Time',
        'Close' => 'Time',
        'Closed' => 'Boolean'
    );

    private static $summary_fields = array(
        'Day' => 'Day',
        'OpenClose(Open)' => 'Open',
        'OpenClose(Close)' => 'Close'
    );

    public function getOpenClose($val) {
        if($val == FALSE) {
             return "Closed";
        }else {
             return $val;
        }
    }
}

I'm assuming I can extend the Time datatype and use it like this:

private static $summary_fields = array(
    'Open.OpenClose' => 'Open',
    'Close.OpenClose' => 'Close'
);

But is this the right way to about this?

Upvotes: 1

Views: 74

Answers (1)

bummzack
bummzack

Reputation: 5875

I suggest you add the desired functionality via Extension to the Time DBField. Your extension could be something like:

class TimeExtension extends Extension
{
    public function OpenClose(){
        $val = $this->owner->getValue();
        return $val ? $val : 'Closed';
    }
}

Then you add the extension via YAML (eg. in _config.yml)

Time
  extensions:
    - TimeExtension

You should then be able to use something like this as your summary_fields:

private static $summary_fields = [
    'Open.OpenClose' => 'Open',
    'Close.OpenClose' => 'Close'
];

Upvotes: 4

Related Questions