Righty ho. Yes this would make a nice module and I'm sure it can be done and will have a go myself. However as I'm just getting into Silverstripe it will take a bit to sort out the injecting of methods, extra editor fields and tabs, peeking at how other modules do it, and so on that would need to be done to extend the SiteTree class nicely.
However it is all fine just using the code from the guy at the dio5 site, I did little more than wrap it for my own amusement. Other than the sort by Date issue - which is kinda crucial to getting the X Most Recent.
What follows is the extras added to my ArticleHolder to hide the ArticlePages it holds 'below'. Crude but works and I'm sure one of us will do a better job in time. Including dbfields in the owner object to have a nice CMS specifiable limit and your checkbox (so you can have a limit of zero mean show zero [=none] as opposed to disabled).
Inside the ArticleHolder.php class file in mysite/code directory
<?php
class ArticleHolder extends Page {
[... other stuff like $db and so on ...]
static $extensions = array('StopHierarchy'); // or could be LimitHierarchy
function getCMSFields() {
$fields = parent::getCMSFields();
// get the children to add to article list with View & Edit options (as Delete is in the Edit)
$children = DataObject::get("ArticlePage", "ParentID = $this->ID", "Date DESC");
if( !$children ) $childList = 'No articles found'; // no children, nothing to work with
else {
$childList = '<table width=100% border=0 cellpadding=0 cellspacing=0 background-color=#efefef>';
$bgcolor = '#ffe';
foreach ($children as $achild) {
$childList .= '<tr>';
$childList .= '<td style="border: 1px solid #ddd; background-color: '.$bgcolor.'; padding: 0.5em 1em 0.5em 1em; margin: 0; width: 4em; text-align: center;"><a href="'.$achild->URLSegment.'">View</a></td>';
$childList .= '<td style="border: 1px solid #ddd; background-color: '.$bgcolor.'; padding: 0.5em 1em 0.5em 1em; margin: 0; width: 4em; text-align: center;"><a href="admin/show/'.$achild->ID.'">Edit</a></td>';
$childList .= '<td style="border: 1px solid #ddd; background-color: '.$bgcolor.'; padding: 0.5em 1em 0.5em 1em; margin: 0; text-align: left;">'.$achild->Date.' to '.$achild->EndDate.': '.$achild->Title.'</td>';
$childList .= '</tr>';
$bgcolor = ($bgcolor == '#ffe')?'#eff':'#ffe';
}
$childList .= '</table>';
}
$fields->addFieldToTab('Root.Content.Articles', new LiteralField('Articles',$childList));
return $fields;
}
}
[.. possibly more stuff here too (O: ...]
?>
The LimitHierarchy.php, also in mysite/code at the moment.
<?php
class LimitHierarchy extends Hierarchy {
public function stageChildren($showAll = false)
{
$baseClass = ClassInfo::baseDataClass($this->owner->class);
$extraFilter = $showAll ? '' : " AND ShowInMenus = 1";
$filter = "`{$baseClass}`.`ParentID` = " . (int)$this->owner->ID;
$filter .= " AND `{$baseClass}`.ID != " . (int)$this->owner->ID;
$filter .= $extraFilter;
if(Director::urlParam("Action") == "getfilteredsubtree"
&& (!empty($_REQUEST['SiteTreeSearchTerm']) || !empty($_REQUEST['SiteTreeFilterDate'])))
{
// ** it no like the Date sort - ambiguous as is in all, need to find the right way to specify it just for one **
// $staged = DataObject::get($baseClass, $filter, "Date ASC");
$staged = DataObject::get($baseClass, $filter, "");
if(!$staged) $staged = new DataObjectSet();
$this->owner->extend("augmentStageChildren", $staged, $showAll);
return $staged;
}
else
{
// tiny limit so it is obvious it had an effect
$limit = 2;
//origcode $set = DataObject::get($baseClass, $filter, "Date DESC", "", $limit);
//origcode $set->sort("Date","ASC");
// ** it no like the Date sort - ambiguous as is in all, need to find the right way to specify it just for one **
// $set = DataObject::get($baseClass, $filter, "`SiteTree`.Date DESC", "", $limit);
$set = DataObject::get($baseClass, $filter, "", "", $limit);
return $set;
}
}
}
?>
The StopHierarchy.php, also in mysite/code at the moment. Note is the same as the LimitHierarchy class but just returns false on the stageChildren equivalent to stop CMS crawl in its tracks. Also quicker (O:
<?php
class StopHierarchy extends Hierarchy {
// unless we have special reason to hunt then (as per lower return null) stop showing children in the CMS hierarchy here
public function stageChildren($showAll = false)
{
$baseClass = ClassInfo::baseDataClass($this->owner->class);
$extraFilter = $showAll ? '' : " AND ShowInMenus = 1";
$filter = "`{$baseClass}`.`ParentID` = " . (int)$this->owner->ID;
$filter .= " AND `{$baseClass}`.ID != " . (int)$this->owner->ID;
$filter .= $extraFilter;
if(Director::urlParam("Action") == "getfilteredsubtree"
&& (!empty($_REQUEST['SiteTreeSearchTerm']) || !empty($_REQUEST['SiteTreeFilterDate'])))
{
// $staged = DataObject::get($baseClass, $filter, "Date ASC");
$staged = DataObject::get($baseClass, $filter, "");
if(!$staged) $staged = new DataObjectSet();
$this->owner->extend("augmentStageChildren", $staged, $showAll);
return $staged;
}
else return new DataObjectSet(); // just Stop
}
}
?>