API Moved "SideBar" widget relation to "widgets" module

Removed BlogTreeExtension in favour of the new WidgetPageExtension
in the "widgets module". Use the following code to update:

Object::add_extension('BlogTree', 'WidgetPageExtension');
Object::add_extension('BlogEntry', 'WidgetPageExtension');

The template placeholder has been renamed from $SideBar to
$SideBarView. In case you're overriding blog templates,
please adjust accordingly.

This change means that widgets can theoretically be applied
to individual blog entries as well, since they need the
"SideBar" relationship as well. In practice, this defaults
to "InheritSideBar" though, which pulls them from the BlogHolder as usual.
This commit is contained in:
Ingo Schommer 2013-04-16 15:28:45 +02:00
parent 1815eea00a
commit d1d9452757
8 changed files with 48 additions and 80 deletions

View File

@ -76,6 +76,22 @@ or: mysite/blog/2007 would show blog entries for 2007
See [:pagecomment](/pagecomment) for creating Askimet-protected comments for every page. See [:pagecomment](/pagecomment) for creating Askimet-protected comments for every page.
## Widgets
The module comes with a couple of default widgets, which rely on the "silverstripe/widgets"
module being installed. Since widgets are based on database records and relations
to pages, they need to be enabled through an `Extension` class in your `config.yml`:
:::yml
BlogTree:
extensions:
- WidgetPageExtension
BlogEntry:
extensions:
- WidgetPageExtension
Alternatively, you can simply enable the extension on your `Page` records
to have it available globally.
## Working with the theme ## Working with the theme

View File

@ -1,3 +0,0 @@
<?php
if(class_exists('WidgetArea')) Object::add_extension('BlogTree','BlogTreeExtension');

View File

@ -111,16 +111,6 @@ class BlogEntry extends Page {
} }
} }
/**
* Get the sidebar from the BlogHolder.
*/
function SideBar() {
if(method_exists($this->Parent(), 'SideBar')) {
return $this->getParent()->SideBar();
}
}
function Content() { function Content() {
if(self::$allow_wysiwyg_editing) { if(self::$allow_wysiwyg_editing) {
return $this->getField('Content'); return $this->getField('Content');

View File

@ -129,39 +129,45 @@ class BlogHolder extends BlogTree implements PermissionProvider {
$blogholder->Title = "Blog"; $blogholder->Title = "Blog";
$blogholder->URLSegment = "blog"; $blogholder->URLSegment = "blog";
$blogholder->Status = "Published"; $blogholder->Status = "Published";
$blogholder->write();
$blogholder->publish("Stage", "Live");
// Add default widgets to first found WidgetArea relationship
if(class_exists('WidgetArea')) { if(class_exists('WidgetArea')) {
$widgetarea = new WidgetArea(); foreach($this->has_one() as $name => $class) {
$widgetarea->write(); if(is_a($class, 'WidgetArea', true)) {
$relationName = "{$name}ID";
$widgetarea = new WidgetArea();
$widgetarea->write();
$blogholder->SideBarID = $widgetarea->ID; $blogholder->$relationName = $widgetarea->ID;
$blogholder->write(); $blogholder->write();
$blogholder->publish("Stage", "Live"); $blogholder->publish("Stage", "Live");
$managementwidget = new BlogManagementWidget(); $managementwidget = new BlogManagementWidget();
$managementwidget->ParentID = $widgetarea->ID; $managementwidget->ParentID = $widgetarea->ID;
$managementwidget->write(); $managementwidget->write();
$tagcloudwidget = new TagCloudWidget(); $tagcloudwidget = new TagCloudWidget();
$tagcloudwidget->ParentID = $widgetarea->ID; $tagcloudwidget->ParentID = $widgetarea->ID;
$tagcloudwidget->write(); $tagcloudwidget->write();
$archivewidget = new ArchiveWidget(); $archivewidget = new ArchiveWidget();
$archivewidget->ParentID = $widgetarea->ID; $archivewidget->ParentID = $widgetarea->ID;
$archivewidget->write(); $archivewidget->write();
$widgetarea->write(); $widgetarea->write();
} else {
$blogholder->write(); break; // only apply to one
$blogholder->publish("Stage", "Live"); }
}
} }
$blog = new BlogEntry(); $blog = new BlogEntry();
$blog->Title = _t('BlogHolder.SUCTITLE', "SilverStripe blog module successfully installed"); $blog->Title = _t('BlogHolder.SUCTITLE', "SilverStripe blog module successfully installed");
$blog->URLSegment = 'sample-blog-entry'; $blog->URLSegment = 'sample-blog-entry';
$blog->Tags = _t('BlogHolder.SUCTAGS',"silverstripe, blog"); $blog->Tags = _t('BlogHolder.SUCTAGS',"silverstripe, blog");
$blog->Content = _t('BlogHolder.SUCCONTENT',"<p>Congratulations, the SilverStripe blog module has been successfully installed. This blog entry can be safely deleted. You can configure aspects of your blog (such as the widgets displayed in the sidebar) in <a href=\"admin\">the CMS</a>.</p>"); $blog->Content = _t('BlogHolder.SUCCONTENT',"<p>Congratulations, the SilverStripe blog module has been successfully installed. This blog entry can be safely deleted. You can configure aspects of your blog in <a href=\"admin\">the CMS</a>.</p>");
$blog->Status = "Published"; $blog->Status = "Published";
$blog->ParentID = $blogholder->ID; $blog->ParentID = $blogholder->ID;
$blog->write(); $blog->write();

View File

@ -23,24 +23,13 @@ class BlogTree extends Page {
static $default_entries_limit = 10; static $default_entries_limit = 10;
static $db = array( static $db = array(
'InheritSideBar' => 'Boolean',
'LandingPageFreshness' => 'Varchar', 'LandingPageFreshness' => 'Varchar',
); );
static $defaults = array(
'InheritSideBar' => True
);
static $has_one = array();
static $has_many = array();
static $allowed_children = array( static $allowed_children = array(
'BlogTree', 'BlogHolder' 'BlogTree', 'BlogHolder'
); );
/* /*
* Finds the BlogTree object most related to the current page. * Finds the BlogTree object most related to the current page.
* - If this page is a BlogTree, use that * - If this page is a BlogTree, use that
@ -90,30 +79,8 @@ class BlogTree extends Page {
return $freshness; return $freshness;
} }
function SideBar() {
if($this->InheritSideBar && $this->getParent()) {
if (method_exists($this->getParent(), 'SideBar')) return $this->getParent()->SideBar();
}
if($this->SideBarID){
return DataObject::get_by_id('WidgetArea', $this->SideBarID);
// @todo: This segfaults - investigate why then fix: return $this->getComponent('SideBar');
}
}
/* ----------- CMS CONTROL -------------- */ /* ----------- CMS CONTROL -------------- */
function getCMSFields() {
$fields = parent::getCMSFields();
if(class_exists('WidgetArea')) {
$fields->addFieldToTab("Root.Widgets", new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent'));
$fields->addFieldToTab("Root.Widgets", new WidgetAreaEditor("SideBar"));
}
return $fields;
}
function getSettingsFields() { function getSettingsFields() {
$fields = parent::getSettingsFields(); $fields = parent::getSettingsFields();

View File

@ -1,7 +0,0 @@
<?php
class BlogTreeExtension extends DataExtension {
static $has_one = array("SideBar" => "WidgetArea");
}

View File

@ -1,3 +0,0 @@
div.flickrwidget {
text-align: center;
}

View File

@ -1,3 +1,5 @@
<div id="Sidebar" class="typography"> <% if SideBarView %>
$SideBar <div id="Sidebar" class="typography">
</div> $SideBarView
</div>
<% end_if %>