Question

I'm wanting to extend an existing Silverstripe module (Swipestripe) where Attribute has_many Options.

The following code successfully extends Option so a Cost is added to each Option.

class OptionCost extends DataExtension {

  private static $db = array(
      'Cost' => 'Decimal(19,4)'
  );

  public function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', new PriceField('Cost'));
    return $fields;
  }
}

However when viewing the options via the parent Attribute the Cost is not display. This is controlled via the $summary_fields static but I can't work how to add Cost as a new summary field.

I've tried adding the following code to OptionCost, and to an extension of Attribute - but neither method worked.

private static $summary_fields = array(
  'Cost' => 'Cost'
);

What is the correct approach to add Cost to the summary_fields table?

Thank you in advance for any advice.

Was it helpful?

Solution

In Silverstripe 3.1 adding fields to $summary_fields in the extension is the correct way to do this.

The following code worked for me:

class OptionCost extends DataExtension  {

    private static $db = array(
        'Cost' => 'Decimal(19,4)'
    );

    private static $summary_fields = array(
        'Cost'
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', new PriceField('Cost'));
        return $fields;
    }

}

Declare the extension in your config or in a config yaml file.

config.yml

...
Attribute:
  extensions:
    - OptionCost
...

Run a dev/build?flush=all.

Also make sure you call ?flush=all on your admin page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top