Added featured posts to CMS

This commit is contained in:
Christopher Pitt 2015-11-12 12:20:11 +13:00
parent ee9cca0788
commit 6d059806ea
15 changed files with 251 additions and 111 deletions

View File

@ -0,0 +1,41 @@
<?php
class BlogFeatureExtension extends DataExtension {
/**
* @config
*
* @var int
*/
private static $excluded_feature_posts = 1;
/**
* @return DataList
*/
public function getFeaturedBlogPosts() {
return BlogPost::get()
->filter('ParentID', $this->owner->ID)
->filter('IsFeatured', true);
}
/**
* @param DataList $posts
* @param null|string $context Context for these blog posts (e.g 'rss')
*
* @return DataList
*/
public function updateGetBlogPosts(DataList &$posts, $context = null) {
if($context === 'rss') {
return;
}
$excluded = (int) Config::inst()->get('BlogFeatureExtension', 'excluded_feature_posts');
if($excluded > 0) {
$taken = $this->getFeaturedBlogPosts()->limit($excluded);
if ($taken->count()) {
$posts = $posts->exclude(array('ID' => $taken->getIDList()));
}
}
}
}

View File

@ -0,0 +1,21 @@
<?php
class BlogPostFeatureExtension extends DataExtension {
/**
* @var array
*/
private static $db = array(
'IsFeatured' => 'Boolean',
);
/**
* @inheritdoc
*
* @param FieldList
*/
public function updateCMSFields(FieldList $fields) {
$sidebar = $fields->fieldByName('blog-admin-sidebar');
$sidebar->insertBefore('PublishDate', new CheckboxField('IsFeatured', 'Mark as featured post'));
}
}

View File

@ -471,12 +471,14 @@ class Blog extends Page implements PermissionProvider {
/**
* Return blog posts.
*
* @param null|string $context Context for these blog posts (e.g 'rss')
*
* @return DataList of BlogPost objects
*/
public function getBlogPosts() {
public function getBlogPosts($context = null) {
$blogPosts = BlogPost::get()->filter('ParentID', $this->ID);
$this->extend('updateGetBlogPosts', $blogPosts);
$this->extend('updateGetBlogPosts', $blogPosts, $context);
return $blogPosts;
}
@ -964,6 +966,7 @@ class Blog_Controller extends Page_Controller {
*/
public function PaginatedList() {
$allPosts = $this->blogPosts ?: new ArrayList();
$posts = new PaginatedList($allPosts);
// Set appropriate page size
@ -994,7 +997,7 @@ class Blog_Controller extends Page_Controller {
*/
$dataRecord = $this->dataRecord;
$this->blogPosts = $dataRecord->getBlogPosts();
$this->blogPosts = $dataRecord->getBlogPosts('rss');
$rss = new RSSFeed($this->blogPosts, $this->Link(), $this->MetaTitle, $this->MetaDescription);

View File

@ -173,7 +173,7 @@ class BlogPost extends Page {
$self =& $this;
$this->beforeUpdateCMSFields(function ($fields) use ($self) {
$uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Featured Image'));
$uploadField = UploadField::create('FeaturedImage', _t('BlogPost.FeaturedImage', 'Banner Image'));
$uploadField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
/**
@ -272,6 +272,8 @@ class BlogPost extends Page {
$authorNames
)->setTitle('Post Options');
$options->setName('blog-admin-sidebar');
$fields->insertBefore($options, 'Root');
});
@ -658,7 +660,7 @@ class BlogPost extends Page {
public function fieldLabels($includeRelations = true) {
$labels = parent::fieldLabels($includeRelations);
$labels['Title'] = _t('BlogPost.PageTitleLabel', "Post Title");
$labels['Title'] = _t('BlogPost.PageTitleLabel', 'Post Title');
return $labels;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

38
docs/en/featured-posts.md Normal file
View File

@ -0,0 +1,38 @@
# Featured posts
You can enable featured posts with the following config:
```yaml
---
Name: featured-posts-blog-config
---
Blog:
extensions:
- BlogFeatureExtension
BlogPost:
extensions:
- BlogPostFeatureExtension
```
This will enable a checkbox in the CMS, with which you can feature blog posts:
![](_images/featured-posts-cms.png)
By default, the template will show the most recent featured post at the top of the
list of posts in a blog. This post will be removed from the normal list of blog posts.
You can increase the number of specially-displayed feature posts by modifying the
template to show more, and by changing the following config setting:
```
<% if $CanHaveFeaturedBlogPosts && $FeaturedBlogPosts %>
<% loop $FeaturedBlogPosts.Limit(10) %>
<% include FeaturedPostSummary %>
<% end_loop %>
<% end_if %>
```
```yaml
BlogFeatureExtension:
excluded_feature_posts: 10
```

View File

@ -69,7 +69,7 @@ en:
CUSTOMSUMMARY: 'Add A Custom Summary'
Categories: Categories
DESCRIPTION: 'Generic content page'
FeaturedImage: 'Featured Image'
FeaturedImage: 'Banner Image'
PLURALNAME: 'Base Pages'
PageTitleLabel: 'Post Title'
PublishDate: 'Publish Date'

View File

@ -0,0 +1,26 @@
<div class="post-summary featured">
<h2>
<a href="$Link" title="<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>">
<% if $MenuTitle %>$MenuTitle
<% else %>$Title<% end_if %>
</a>
</h2>
<p class="post-image">
<a href="$Link" <%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>>
$FeaturedImage.setWidth(795)
</a>
</p>
<% if $Summary %>
<p>$Summary
<% else %>
<p>$Excerpt
<% end_if %>
<a href="$Link">
<%t Blog.ReadMoreAbout "Read more about '{title}'..." title=$Title %>
</a>
</p>
<% include EntryMeta %>
</div>

View File

@ -5,18 +5,18 @@
<article>
<h1>
<% if $ArchiveYear %>
<%t Blog.Archive "Archive" %>:
<%t Blog.Archive 'Archive' %>:
<% if $ArchiveDay %>
$ArchiveDate.Nice
<% else_if $ArchiveMonth %>
$ArchiveDate.format("F, Y")
$ArchiveDate.format('F, Y')
<% else %>
$ArchiveDate.format("Y")
$ArchiveDate.format('Y')
<% end_if %>
<% else_if $CurrentTag %>
<%t Blog.Tag "Tag" %>: $CurrentTag.Title
<%t Blog.Tag 'Tag' %>: $CurrentTag.Title
<% else_if $CurrentCategory %>
<%t Blog.Category "Category" %>: $CurrentCategory.Title
<%t Blog.Category 'Category' %>: $CurrentCategory.Title
<% else %>
$Title
<% end_if %>
@ -24,12 +24,21 @@
<div class="content">$Content</div>
<% if $FeaturedBlogPosts.Exists && $FeaturedBlogPosts.First %>
<% with $FeaturedBlogPosts.First %>
<% include FeaturedPostSummary %>
<% end_with %>
<% end_if %>
<% if $PaginatedList.Exists %>
<% loop $PaginatedList %>
<% include PostSummary %>
<% end_loop %>
<% else %>
<p><%t Blog.NoPosts "There are no posts" %></p>
<% if $FeaturedBlogPosts.Exists %>
<p><%t Blog.NoUnfeaturedPosts 'There are no non-featured posts' %></p>
<% else %>
<p><%t Blog.NoPosts 'There are no posts' %></p>
<% end_if %>
<% end_if %>
</article>