I created a DataObjectManager for managing "help links". These are featured links to other pages which are shown on some of the pages of a client's website. The structure I want to achieve is the following:
<h2>I need help with...</h2>
<h3><a href="product-foo">Product Foo</a></h3>
<p>This is the description of product foo.</p>
<h3><a href="product-bar">Product Bar</a></h3>
<p>This is the description of product bar.</p>
I appended the DataObjectManager to the HomePage in the CMS, since there is only one homepage, and I use the homepage for all content which is not a page and not related to specific page types. I have the following code so far:
HomePage.php
<?php
class HomePage extends Page
{
public static $db = array(
"Introduction" => "HTMLText"
);
public static $has_one = array(
);
static $has_many = array(
"Helplinks" => "Helplink"
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Content.Main", "Content");
$fields->addFieldToTab("Root.Content.Main", new HTMLEditorField("Introduction"));
$fields->addFieldToTab("Root.Content.Helplinks", new DataObjectManager(
$this,
"Helplinks",
"Helplink",
array(
"Title" => "Title",
"Description" => "Description"
),
'getCMSFields_forPopup'
));
return $fields;
}
}
class HomePage_Controller extends Page_Controller
{
function LatestNewsArticles($num = 3)
{
$news_articles = DataObject::get_one("NewsPageHolder");
return ($news_articles) ? DataObject::get("NewsPage", "ParentID = $news_articles->ID", "Date DESC", "", $num) : false;
}
function PartnerSpotlight()
{
$partner = DataObject::get_one("PartnerPage", null, false, "RAND()");
return $partner;
}
function LatestCases($num = 3)
{
$cases = DataObject::get_one("CasePageHolder");
return ($cases) ? DataObject::get("CasePage", "ParentID = $cases->ID", "Created DESC", "", $num) : false;
}
}
?>
Helplink.php
<?php
class Helplink extends DataObject
{
static $db = array(
"Title" => "Varchar(255)",
"Description" => "Text"
);
static $has_one = array(
"LinkTo" => "SiteTree",
"HomePage" => "HomePage"
);
public function getCMSFields_forPopup()
{
$fields = new FieldSet();
$title = new TextField("Title");
$fields->push($title);
$description = new TextareaField("Description");
$fields->push($description);
$pages = new SimpleTreeDropdownField("LinkTo", "Choose a page:", "SiteTree");
$pages->setEmptyString("-- Select a page --");
$fields->push($pages);
return $fields;
}
}
?>
I use a SimpleTreeDropdownField for selecting a page from the SiteTree to link to.
The problem is that the title and description fields are being saved, but not the link. In the database the field LinkToID always contains "1", the ID of the HomePage in the SiteTree table.
When I use $pages = new SimpleTreeDropdownField("LinkToID", "Choose a page:", "SiteTree"); instead of $pages = new SimpleTreeDropdownField("LinkTo", "Choose a page:", "SiteTree");, the DataObjectManager doesn't show the SimpleTreeDropdownField.
What am I doing wrong?