silverstripe-blog/src/Widgets/BlogRecentPostsWidget.php

91 lines
1.8 KiB
PHP
Raw Normal View History

2015-11-21 07:17:29 +01:00
<?php
namespace SilverStripe\Blog\Widgets;
use SilverStripe\Blog\Model\Blog;
use SilverStripe\Forms\DropdownField;
2018-01-29 04:13:19 +01:00
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\DataList;
use SilverStripe\Widgets\Model\Widget;
if (!class_exists(Widget::class)) {
return;
}
2015-11-21 07:17:29 +01:00
/**
* @method Blog Blog()
*
* @property int $NumberOfPosts
*/
class BlogRecentPostsWidget extends Widget
{
/**
* @var string
*/
private static $title = 'Recent Posts';
/**
* @var string
*/
private static $cmsTitle = 'Recent Posts';
/**
* @var string
*/
private static $description = 'Displays a list of recent blog posts.';
/**
* @var array
*/
2017-09-14 00:59:01 +02:00
private static $db = [
2015-11-21 07:17:29 +01:00
'NumberOfPosts' => 'Int',
2017-09-14 00:59:01 +02:00
];
2015-11-21 07:17:29 +01:00
/**
* @var array
*/
2017-09-14 00:59:01 +02:00
private static $has_one = [
'Blog' => Blog::class,
2017-09-14 00:59:01 +02:00
];
2015-11-21 07:17:29 +01:00
/**
* @var string
*/
private static $table_name = 'BlogRecentPostsWidget';
2015-11-21 07:17:29 +01:00
/**
* {@inheritdoc}
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function ($fields) {
/**
* @var FieldList $fields
*/
2017-09-14 00:59:01 +02:00
$fields->merge([
2017-09-14 00:27:40 +02:00
DropdownField::create('BlogID', _t(__CLASS__ . '.Blog', 'Blog'), Blog::get()->map()),
NumericField::create('NumberOfPosts', _t(__CLASS__ . '.NumberOfPosts', 'Number of Posts'))
2017-09-14 00:59:01 +02:00
]);
2015-11-21 07:17:29 +01:00
});
return parent::getCMSFields();
}
/**
* @return array|DataList
2015-11-21 07:17:29 +01:00
*/
public function getPosts()
{
$blog = $this->Blog();
if ($blog) {
return $blog->getBlogPosts()
->sort('"PublishDate" DESC')
->limit($this->NumberOfPosts);
}
2017-09-14 00:59:01 +02:00
return [];
2015-11-21 07:17:29 +01:00
}
}