18/03/2012 10:22pm

PHP | Problem - How to override a method?


// We have core code which defines the location options for images in the CMS in Page.php

// Other page types can over-write these location options


// Page.php
class Page extends SiteTree{

function getLocationOptions()
{
return array(
"none" => "Do not display",
"default" => "Default"
);
}

}


// CustomPage.php
class CustomPage extends Page{

function getLocationOptions()
{
return array(
"belowtitle" => "Below page title",
"belowcontent" => "Below page content",
"default" => "Default"
);
}

}

//How do I override getLocationOptions across all Pages without editing Page.php (which is part of our core code and shared by many projects)?


1 Comments 1 Solutions

18/03/2012 10:30pm

PHP | Solution - Anonymous

// We have core code which defines the location options for images in the CMS in Page.php
// Other page types can over-write these location options


// Page.php
class Page extends SiteTree{

function getLocationOptions()
{
$locations = array(
"none" => "Do not display",
"default" => "Default"
);
$this->extend('LocationOptions', $locations);
return $locations);
}

}


// CustomPage.php
class CustomPage extends DataObjectDecorator{

function LocationOptions(&$locations)
{
$locations += array(
"belowtitle" => "Below page title",
"belowcontent" => "Below page content",
"default" => "Default"
);
unset($locations['none']);
}

}

// mysite/_config.php

Object::add_extension('Page', 'CustomPage');

//How do I override getLocationOptions across all Pages without editing Page.php (which is part of our core code and shared by many projects)?

Post Comment