Reputation: 1569
I have a site which has 2 landing pages with the exact same layout (1 thumbnail, a summary paragraph, a title, and a link to the detail page). The landing pages are Webinars and News, for reference. Both are managed as separate data objects in admin models.
The easiest way is to make 2 landing page types and reference the corresponding data object in each (i.e. WebinarLandingPage.php and NewsLandingPage.php). But I know this is not the most extensible way. If possible I'd like to have just one landing page type that differentiates between which data object to render.
The problem is, I'm not sure how that's possible without relying on a page url or page title or you can check which section of the site tree your are in, things that can easily change and thus break code. Is here a better way to go about something like this?
Upvotes: 0
Views: 52
Reputation: 219
You could either add both DataObjects to a single LandingPage
class as relations.
E.g.
LandingPage-> has_many -> Webinar(Object)
LandingPage-> has_many -> News(Object)
However this restricts you to having to define the relationship in LandingPage
for each additional object you are adding.
An alternative method would be to have both Webinar
and News
Objects use the same abstract / parent class so you can define some default functions that are shared between both Objects.
Then using an DropDown field on the LandingPage
(CMS) to set the Object which is to be used. (E.g. Get all Objects which extend base class mentioned above).
You can then use custom functions within LandingPage
to get all objects from the class which has been set in the DropDown field via the CMS.
E.g.
class LandingObject {}
class Webinar extends LandingObject {}
class News extends LandingObject {}
class LandingPage extends Page
{
private static $db = array(
LandingObject => 'Varchar(19)'
); //Populated by list of DataObject ClassNames that extends LandingObject
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldsToTab(
DropdownField::create( 'LandingObject', 'Landing Object', ClassInfo::subclassesFor('LandingObject') );
);
}
public function getLandingObject() {
return DataObject::get($this->LandingObject);
}
}
Upvotes: 2