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

@ -362,7 +362,7 @@ class Blog extends Page implements PermissionProvider {
if($this->config()->grant_user_access) {
$list = Member::get();
$this->extend('updateCandidateUsers', $list);
return $list;
return $list;
} else {
return Permission::get_members_by_permission(
$this->config()->grant_user_permission
@ -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

@ -1,20 +1,20 @@
<div class="cms-content-tools east cms-panel cms-panel-layout blog-admin-sidebar<% if $isOpen %> open<% end_if %>"
data-expandOnClick="true"
data-layout-type="border"
id="blog-admin-sidebar">
<div class="cms-panel-content center">
<div class="cms-content-view cms-tree-view-sidebar" id="blog-admin-content">
<h3 class="cms-panel-header">$Title</h3>
data-expandOnClick="true"
data-layout-type="border"
id="blog-admin-sidebar">
<div class="cms-panel-content center">
<div class="cms-content-view cms-tree-view-sidebar" id="blog-admin-content">
<h3 class="cms-panel-header">$Title</h3>
<% loop $Children %>
$FieldHolder
<% end_loop %>
</div>
</div>
<div class="cms-panel-content-collapsed">
<h3 class="cms-panel-header">$Title</h3>
</div>
<div class="cms-panel-toggle south">
<a class="toggle-expand" href="#"><span>&laquo;</span></a>
<a class="toggle-collapse" href="#"><span>&raquo;</span></a>
</div>
</div>
</div>
<div class="cms-panel-content-collapsed">
<h3 class="cms-panel-header">$Title</h3>
</div>
<div class="cms-panel-toggle south">
<a class="toggle-expand" href="#"><span>&laquo;</span></a>
<a class="toggle-collapse" href="#"><span>&raquo;</span></a>
</div>
</div>

View File

@ -1,5 +1,5 @@
<% if $SideBarView %>
<div class="blog-sidebar typography unit size1of4 lastUnit">
$SideBarView
</div>
<div class="blog-sidebar typography unit size1of4 lastUnit">
$SideBarView
</div>
<% end_if %>

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

@ -1,13 +1,13 @@
<section>
<h1>$CurrentProfile.FirstName $CurrentProfile.Surname</h1>
<div>
<% if $CurrentProfile.BlogProfileImage %>
<div class="profile-image">
$CurrentProfile.BlogProfileImage.setWidth(180)
</div>
<% end_if %>
<div class="profile-summary">
<p>$CurrentProfile.BlogProfileSummary</p>
</div>
</div>
<h1>$CurrentProfile.FirstName $CurrentProfile.Surname</h1>
<div>
<% if $CurrentProfile.BlogProfileImage %>
<div class="profile-image">
$CurrentProfile.BlogProfileImage.setWidth(180)
</div>
<% end_if %>
<div class="profile-summary">
<p>$CurrentProfile.BlogProfileSummary</p>
</div>
</div>
</section>

View File

@ -1,25 +1,25 @@
<%-- NOTE: Before including this, you will need to wrap the include in a with block --%>
<% if $MoreThanOnePage %>
<p class="pagination">
<% if $NotFirstPage %>
<a class="prev" href="{$PrevLink}">&larr;</a>
<% end_if %>
<p class="pagination">
<% if $NotFirstPage %>
<a class="prev" href="{$PrevLink}">&larr;</a>
<% end_if %>
<% loop $Pages %>
<% if $CurrentBool %>
<span>$PageNum</span>
<% else %>
<% if $Link %>
<a href="$Link">$PageNum</a>
<% else %>
<span>...</span>
<% end_if %>
<% end_if %>
<% end_loop %>
<% loop $Pages %>
<% if $CurrentBool %>
<span>$PageNum</span>
<% else %>
<% if $Link %>
<a href="$Link">$PageNum</a>
<% else %>
<span>...</span>
<% end_if %>
<% end_if %>
<% end_loop %>
<% if $NotLastPage %>
<a class="next" href="{$NextLink}">&rarr;</a>
<% end_if %>
</p>
<% if $NotLastPage %>
<a class="next" href="{$NextLink}">&rarr;</a>
<% end_if %>
</p>
<% end_if %>

View File

@ -2,41 +2,50 @@
<div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>">
<article>
<h1>
<% if $ArchiveYear %>
<%t Blog.Archive "Archive" %>:
<% if $ArchiveDay %>
$ArchiveDate.Nice
<% else_if $ArchiveMonth %>
$ArchiveDate.format("F, Y")
<% else %>
$ArchiveDate.format("Y")
<% end_if %>
<% else_if $CurrentTag %>
<%t Blog.Tag "Tag" %>: $CurrentTag.Title
<% else_if $CurrentCategory %>
<%t Blog.Category "Category" %>: $CurrentCategory.Title
<% else %>
$Title
<% end_if %>
</h1>
<article>
<h1>
<% if $ArchiveYear %>
<%t Blog.Archive 'Archive' %>:
<% if $ArchiveDay %>
$ArchiveDate.Nice
<% else_if $ArchiveMonth %>
$ArchiveDate.format('F, Y')
<% else %>
$ArchiveDate.format('Y')
<% end_if %>
<% else_if $CurrentTag %>
<%t Blog.Tag 'Tag' %>: $CurrentTag.Title
<% else_if $CurrentCategory %>
<%t Blog.Category 'Category' %>: $CurrentCategory.Title
<% else %>
$Title
<% end_if %>
</h1>
<div class="content">$Content</div>
<div class="content">$Content</div>
<% if $PaginatedList.Exists %>
<% loop $PaginatedList %>
<% include PostSummary %>
<% end_loop %>
<% else %>
<p><%t Blog.NoPosts "There are no posts" %></p>
<% end_if %>
</article>
<% if $FeaturedBlogPosts.Exists && $FeaturedBlogPosts.First %>
<% with $FeaturedBlogPosts.First %>
<% include FeaturedPostSummary %>
<% end_with %>
<% end_if %>
<% if $PaginatedList.Exists %>
<% loop $PaginatedList %>
<% include PostSummary %>
<% end_loop %>
<% else %>
<% 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>
$Form
$CommentsForm
$Form
$CommentsForm
<% with $PaginatedList %>
<% with $PaginatedList %>
<% include Pagination %>
<% end_with %>
</div>

View File

@ -4,7 +4,7 @@
<article>
<h1>$Title</h1>
<% if $FeaturedImage %>
<% if $FeaturedImage %>
<p class="post-image">$FeaturedImage.setWidth(795)</p>
<% end_if %>
@ -13,7 +13,7 @@
<% include EntryMeta %>
</article>
$Form
$Form
$CommentsForm
</div>

View File

@ -2,21 +2,21 @@
<div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>">
<% include MemberDetails %>
<% include MemberDetails %>
<% if $PaginatedList.Exists %>
<h2>Posts by $CurrentProfile.FirstName $CurrentProfile.Surname for $Title:</h2>
<% loop $PaginatedList %>
<% include PostSummary %>
<% end_loop %>
<% end_if %>
<% if $PaginatedList.Exists %>
<h2>Posts by $CurrentProfile.FirstName $CurrentProfile.Surname for $Title:</h2>
<% loop $PaginatedList %>
<% include PostSummary %>
<% end_loop %>
<% end_if %>
$Form
$CommentsForm
$Form
$CommentsForm
<% with $PaginatedList %>
<% include Pagination %>
<% end_with %>
<% with $PaginatedList %>
<% include Pagination %>
<% end_with %>
</div>