I want to create a generic "slide" sub class of DataObject that can be extended for use on different page types. E.g. some page types' slides might/might not want a title field.
I want to avoid duplicating and renaming the code of a Slide dataobject and extend the base on instead as per best OO practice. However when I try and do this, I just get SilverStripe errors. I've put my base, extended and page classes below. Does anyone have any insight into this?
Much appreciated.
The base slide class
<?php
class Slide extends DataObject {
/**
*
* DB fields
* @var unknown_type
*/
static $db = array (
'Title' => 'Varchar(255)',
'Text' => 'Text',
'Link' => 'Text'
);
/**
*
* Relations
* @var array
*/
static $has_one = array (
'Image' => 'Image'
);
/**
*
* Fields to show in the DOM table
* @var array
*/
static $summary_fields = array(
'Thumb' => 'Image',
'Title' => 'Title',
'Text' => 'Text'
);
/**
* (non-PHPdoc)
* @see httpdocs/sapphire/core/model/DataObject::getCMSFields()
*/
public function getCMSFields()
{
return new FieldSet(
new TextField('Title'),
new TextField('Text'),
new TextField('Link'),
new ImageField('Image', 'Image', null, null, null, 'slides')
);
}
/**
*
* Generate our thumbnail for the DOM
*/
public function getThumb()
{
if($this->ImageID)
return $this->Image()->CMSThumbnail();
else
return '(No Image)';
}
}
The PageSlide sub class
<?php
class PageSlide extends Slide {
static $has_one = array (
'ProductPage' => 'ProductPage',
'Image' => 'Image'
);
}
Use on a page
<?php
class ProductPage extends ArticlePage {
public static $db = array(
'ShowOnProductHolder' => "Enum('Yes,No')",
'Introduction' => 'Text'
);
public static $has_one = array(
'PageSildes' => 'PageSlide'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new TextField('Introduction', "Introduction"), 'Content');
$manager = new DataObjectManager(
$this,
'PageSlides',
'PageSlide'
);
$fields->addFieldToTab("Root.Content.Slideshow", $manager);
// Add the dispaly on productholder page drop down
$fields->addFieldToTab('Root.Content.Main', new DropdownField('ShowOnProductHolder', 'Show product on Products and applications page?', $this->dbObject('ShowOnProductHolder')->enumValues()));
return $fields;
}
}