Hi,
I am struggling with the following - I am trying to get a list of Related Links onto my page. I have created a RelatedLink object and used the DOM to add it to my admin page. This has worked exactly as expected and I am able to add data objects as i would expect. The issue comes when trying to view them on the page. It appears the RelatedLinks object is empty for some reason?! If i try to access a count on the page (see snippet below) it fails with the error Fatal error: Call to a member function Count() on a non-object in...
Any help very gratefully received!
Richard
function RelatedLinks() {
return $this->RelatedLinks->Count();
}
Page.php
class Page extends SiteTree {
static $db = array(
'HighlightPage' => 'Boolean',
'FeaturedText' => 'HTMLText'
);
static $has_one = array(
"WidgetBar" => "WidgetArea"
);
static $has_many = array(
"RelatedLinks" => "RelatedLink"
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.FeaturedText", new CheckboxField(
$name = "HighlightPage",
$title = "Highlight this page"));
$fields->addFieldToTab("Root.Content.FeaturedText", new HtmlEditorField(
$name = "FeaturedText",
$title = "Add content to the Featured Box on this page"));
$fields->addFieldToTab("Root.Content.RelatedLinks", new DataObjectManager(
$this,
'RelatedLinks',
'RelatedLink',
array('LinkTitle' => 'LinkTitle', 'LinkURL' => 'LinkURL')
));
if (Permission::check('ADMIN')) {
$fields->addFieldToTab("Root.Content.WidgetBar", new WidgetAreaEditor("WidgetBar"));
} else {
$fields->removeFieldFromTab("Root","Access");
$fields->removeFieldFromTab("Root","Behaviour");
$fields->removeFieldFromTab("Root.Content","Google Sitemap");
$fields->removeFieldFromTab("Root.Content","Metadata");
$fields->removeFieldFromTab("Root.Content","Widgets");
}
return $fields;
}
function highlightPage() {
return $this->HighlightPage ? "highlight" : null;
}
function RelatedLinks() {
return $this->RelatedLinks->Count();
}
}
class Page_Controller extends ContentController {
public function init() {
parent::init();
}
}
RelatedLink.php
class RelatedLink extends DataObject {
static $db = array(
'LinkTitle' => 'Varchar',
'LinkURL' => 'Text'
);
static $has_one = array(
'Page' => 'Page'
);
function getCMSFields_forPopup() {
$fields = new Fieldset();
$fields->push(new TextField('LinkTitle', 'Link title'));
$fields->push(new TextField('LinkURL', 'Link URL'));
return $fields;
}
}