26/08/2011 5:57pm

PHP | Problem - Silverstripe Content Block manager


// This all requires DataObjectManager

http://www.silverstripe.org/dataobjectmanager-module/

_config.php

<?php

Object::add_extension('Page', 'BlockExtension');
SortableDataObject::add_sortable_class('Block');

code/Block_DataObject.php

<?php

class Block extends DataObject
{
static $db = array(
'Title' => 'VarChar',
'Content' => 'HTMLText',
);

static $belongs_many_many = array(
'Pages' => 'Page',
);
}

code/BlockExtension_Extension.php

<?php

class BlockExtension extends DataObjectDecorator
{
public function extraStatics() {
return array(
'many_many' => array(
'Blocks' => 'Block',
)
);
}

public function updateCMSFields(FieldSet $fields)
{
$fields->addFieldToTab('Root.Content.Blocks', new ManyManyDataObjectManager(
$this->owner,
'Blocks',
'Block'
));
}
}

In any template that extends Page.. just add

<% if Blocks %>
<div id="blocks">
<% control Blocks %>
<div class="block">$Title<br>$Content</div>
<% end_control %>
</div>
<% end_if %>

Or thereabouts.. The HTML is up to you :)


Post Comment