Page types are great building blocks
Support OOP AND MVC architectures
Great for beginners
class ArticleHolder extends BlogHolder {
private static $has_one = array('Author' => 'Member');
}
class TaggedArticleHolder extends ArticleHolder {
private static $many_many = array('Tags' => 'Tag');
}
// in a module
class BlogEntry extends Page {
public function getTagCloud() {
// lots of useful code here
}
}
// in your project code
class ArticlePage extends BlogEntry {
public function getTagCloudSorted() {
return $this->getTagCloud()->sort('Name');
}
}
class Tag extends DataObject {
private static $db = array('Label' => 'Varchar');
}
class Page extends SiteTree {
private static $many_many = array(
'Tags' => 'Tag'
);
}
class Page_Controller extends ContentController {
public function TagCloud() {
return new TagCloud(Page::get()->relation('Tags'));
}
}
class TagCloud extends ViewableData {
protected $tags;
public function __construct($tags) {
$this->tags = $tags;
}
public function TagsByCount() {
$results = new ArrayList();
$namesToCount = $this->tags->dataQuery()->query()
->setGroupBy('Name')
->setSelect('Name', 'COUNT(*)')
->setOrderBy('COUNT(*) DESC')
->execute()
->map();
foreach($namesToCount as $name => $count) {
$results->push(new ArrayData(array(
'Name' => $name,
'Frequency' => $freq
)));
}
return $results;
}
}
class Taggable extends DataExtension {
private static $many_many = array('Tags' => 'Tags');
}
// In mysite/_config.yml
BlogEntry:
extensions:
- Taggable
class ArticlePage extends Page { private static $db = array( 'Type' => 'Enum("News Entry,Blog Entry")'
); public function getCMSFields() { $fields = parent::getCMSFields(); if($this->Type == 'Blog Entry') { $fields->addFieldToTab( 'Root.Main', new TagField() ); } return $fields; } }
class InflectionViewHelper implements TemplateGlobalProvider {
public function Pluralize($str) {
return Inflector::pluralize($str);
}
public static function get_template_global_variables() {
return array('Pluralize')
}
}