mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
Added featured posts to CMS
This commit is contained in:
parent
ee9cca0788
commit
6d059806ea
41
code/extensions/BlogFeatureExtension.php
Normal file
41
code/extensions/BlogFeatureExtension.php
Normal 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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
21
code/extensions/BlogPostFeatureExtension.php
Normal file
21
code/extensions/BlogPostFeatureExtension.php
Normal 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'));
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
BIN
docs/en/_images/featured-posts-cms.png
Normal file
BIN
docs/en/_images/featured-posts-cms.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 66 KiB |
38
docs/en/featured-posts.md
Normal file
38
docs/en/featured-posts.md
Normal 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
|
||||
```
|
@ -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'
|
||||
|
@ -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>«</span></a>
|
||||
<a class="toggle-collapse" href="#"><span>»</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>«</span></a>
|
||||
<a class="toggle-collapse" href="#"><span>»</span></a>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -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 %>
|
||||
|
26
templates/Includes/FeaturedPostSummary.ss
Normal file
26
templates/Includes/FeaturedPostSummary.ss
Normal 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>
|
@ -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>
|
||||
|
@ -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}">←</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 %>
|
||||
|
||||
<% if $NotLastPage %>
|
||||
<a class="next" href="{$NextLink}">→</a>
|
||||
<% end_if %>
|
||||
</p>
|
||||
<p class="pagination">
|
||||
<% if $NotFirstPage %>
|
||||
<a class="prev" href="{$PrevLink}">←</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 %>
|
||||
|
||||
<% if $NotLastPage %>
|
||||
<a class="next" href="{$NextLink}">→</a>
|
||||
<% end_if %>
|
||||
</p>
|
||||
<% end_if %>
|
||||
|
@ -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>
|
||||
|
||||
<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>
|
||||
|
||||
$Form
|
||||
$CommentsForm
|
||||
|
||||
<% with $PaginatedList %>
|
||||
<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>
|
||||
|
||||
<% 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
|
||||
|
||||
<% with $PaginatedList %>
|
||||
<% include Pagination %>
|
||||
<% end_with %>
|
||||
</div>
|
||||
|
@ -3,17 +3,17 @@
|
||||
<div class="blog-entry content-container <% if $SideBarView %>unit size3of4<% end_if %>">
|
||||
<article>
|
||||
<h1>$Title</h1>
|
||||
|
||||
<% if $FeaturedImage %>
|
||||
|
||||
<% if $FeaturedImage %>
|
||||
<p class="post-image">$FeaturedImage.setWidth(795)</p>
|
||||
<% end_if %>
|
||||
|
||||
|
||||
<div class="content">$Content</div>
|
||||
|
||||
<% include EntryMeta %>
|
||||
</article>
|
||||
|
||||
$Form
|
||||
|
||||
$Form
|
||||
$CommentsForm
|
||||
</div>
|
||||
|
||||
|
@ -2,22 +2,22 @@
|
||||
|
||||
<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 %>
|
||||
|
||||
$Form
|
||||
$CommentsForm
|
||||
<% if $PaginatedList.Exists %>
|
||||
<h2>Posts by $CurrentProfile.FirstName $CurrentProfile.Surname for $Title:</h2>
|
||||
<% loop $PaginatedList %>
|
||||
<% include PostSummary %>
|
||||
<% end_loop %>
|
||||
<% end_if %>
|
||||
|
||||
<% with $PaginatedList %>
|
||||
<% include Pagination %>
|
||||
<% end_with %>
|
||||
$Form
|
||||
$CommentsForm
|
||||
|
||||
<% with $PaginatedList %>
|
||||
<% include Pagination %>
|
||||
<% end_with %>
|
||||
|
||||
</div>
|
||||
|
||||
<% include BlogSideBar %>
|
||||
<% include BlogSideBar %>
|
||||
|
Loading…
Reference in New Issue
Block a user