mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
Merge pull request #578 from SpeksForks/featured-blog-posts-widget
Featured Blog posts widget
This commit is contained in:
commit
ad320d7d88
@ -4,3 +4,12 @@ Name: blogconfig
|
|||||||
SilverStripe\Security\Member:
|
SilverStripe\Security\Member:
|
||||||
extensions:
|
extensions:
|
||||||
- SilverStripe\Blog\Model\BlogMemberExtension
|
- SilverStripe\Blog\Model\BlogMemberExtension
|
||||||
|
|
||||||
|
---
|
||||||
|
Name: featuredpostswidget
|
||||||
|
Only:
|
||||||
|
moduleexists: silverstripe/widgets
|
||||||
|
---
|
||||||
|
SilverStripe\Blog\Model\BlogPost:
|
||||||
|
extensions:
|
||||||
|
- SilverStripe\Blog\Model\BlogPostFeaturedExtension
|
||||||
|
32
src/Model/BlogPostFeaturedExtension.php
Normal file
32
src/Model/BlogPostFeaturedExtension.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Blog\Model;
|
||||||
|
|
||||||
|
use SilverStripe\ORM\DataExtension;
|
||||||
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\CheckboxField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a checkbox field for featured blog posts widget.
|
||||||
|
*/
|
||||||
|
class BlogPostFeaturedExtension extends DataExtension
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $db = [
|
||||||
|
'FeaturedInWidget' => 'Boolean',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function updateCMSFields(FieldList $fields)
|
||||||
|
{
|
||||||
|
// Add the checkbox in.
|
||||||
|
$fields->addFieldToTab(
|
||||||
|
'Root.PostOptions',
|
||||||
|
CheckboxField::create('FeaturedInWidget', _t(__CLASS__ . '.FEATURED', 'Include Post in Feature Widget'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
93
src/Widgets/BlogFeaturedPostsWidget.php
Normal file
93
src/Widgets/BlogFeaturedPostsWidget.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Blog\Widgets;
|
||||||
|
|
||||||
|
use SilverStripe\Blog\Model\Blog;
|
||||||
|
use SilverStripe\Control\Director;
|
||||||
|
use SilverStripe\Forms\DropdownField;
|
||||||
|
use SilverStripe\Forms\FieldList;
|
||||||
|
use SilverStripe\Forms\NumericField;
|
||||||
|
use SilverStripe\ORM\DataList;
|
||||||
|
use SilverStripe\Widgets\Model\Widget;
|
||||||
|
|
||||||
|
if (!class_exists(Widget::class)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @method Blog Blog()
|
||||||
|
*
|
||||||
|
* @property int $NumberOfPosts
|
||||||
|
*/
|
||||||
|
class BlogFeaturedPostsWidget extends Widget
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $title = 'Featured Posts';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $cmsTitle = 'Featured Posts';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $description = 'Displays a list of featured blog posts.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $db = [
|
||||||
|
'NumberOfPosts' => 'Int',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $has_one = [
|
||||||
|
'Blog' => Blog::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private static $table_name = 'BlogFeaturedPostsWidget';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getCMSFields()
|
||||||
|
{
|
||||||
|
$this->beforeUpdateCMSFields(function ($fields) {
|
||||||
|
/**
|
||||||
|
* @var FieldList $fields
|
||||||
|
*/
|
||||||
|
$fields->merge([
|
||||||
|
DropdownField::create('BlogID', _t(__CLASS__ . '.Blog', 'Blog'), Blog::get()->map()),
|
||||||
|
NumericField::create('NumberOfPosts', _t(__CLASS__ . '.NumberOfPosts', 'Number of Posts'))
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
return parent::getCMSFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array|DataList
|
||||||
|
*/
|
||||||
|
public function getPosts()
|
||||||
|
{
|
||||||
|
$blog = $this->Blog();
|
||||||
|
|
||||||
|
if ($blog) {
|
||||||
|
return $blog->getBlogPosts()
|
||||||
|
->filter('ID:not', Director::get_current_page()->ID)
|
||||||
|
->filter('FeaturedInWidget', true)
|
||||||
|
->sort('RAND()')
|
||||||
|
->limit($this->NumberOfPosts);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
<% if $Posts %>
|
||||||
|
<ul>
|
||||||
|
<% loop $Posts %>
|
||||||
|
<li>
|
||||||
|
<a href="$Link" title="$Title">
|
||||||
|
<span class="arrow">→</span>
|
||||||
|
<span class="text">$Title</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<% end_loop %>
|
||||||
|
</ul>
|
||||||
|
<% end_if %>
|
Loading…
Reference in New Issue
Block a user