Source of file BlogCategoriesWidget.php
Size: 3,249 Bytes - Last Modified: 2021-12-23T10:28:05+00:00
/var/www/docs.ssmods.com/process/src/src/Widgets/BlogCategoriesWidget.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 | <?php namespace SilverStripe\Blog\Widgets; use SilverStripe\Blog\Model\Blog; use SilverStripe\Core\Convert; 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() */ class BlogCategoriesWidget extends Widget { /** * @var string */ private static $title = 'Categories'; /** * @var string */ private static $cmsTitle = 'Blog Categories'; /** * @var string */ private static $description = 'Displays a list of blog categories.'; /** * @var array */ private static $db = [ 'Limit' => 'Int', 'Order' => 'Varchar', 'Direction' => 'Varchar', ]; /** * @var array */ private static $has_one = [ 'Blog' => Blog::class, ]; /** * @var string */ private static $table_name = 'BlogCategoriesWidget'; /** * {@inheritdoc} */ public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { $fields[] = DropdownField::create( 'BlogID', _t(__CLASS__ . '.Blog', 'Blog'), Blog::get()->map() ); $fields[] = NumericField::create( 'Limit', _t(__CLASS__ . '.Limit', 'Limit'), 0 ) ->setDescription( _t( __CLASS__ . '.Limit_Description', 'Limit the number of categories shown by this widget (set to 0 to show all categories).' ) ) ->setMaxLength(3); $fields[] = DropdownField::create( 'Order', _t(__CLASS__ . '.Sort', 'Sort'), ['Title' => 'Title', 'Created' => 'Created', 'LastEdited' => 'Updated'] ) ->setDescription( _t(__CLASS__ . '.Sort_Description', 'Change the order of categories shown by this widget.') ); $fields[] = DropdownField::create( 'Direction', _t(__CLASS__ . '.Direction', 'Direction'), ['ASC' => 'Ascending', 'DESC' => 'Descending'] ) ->setDescription( _t( __CLASS__ . '.Direction_Description', 'Change the direction of ordering of categories shown by this widget.' ) ); }); return parent::getCMSFields(); } /** * @return DataList */ public function getCategories() { $blog = $this->Blog(); if (!$blog) { return []; } $query = $blog->Categories(); if ($this->Limit) { $query = $query->limit(Convert::raw2sql($this->Limit)); } if ($this->Order && $this->Direction) { $query = $query->sort(Convert::raw2sql($this->Order), Convert::raw2sql($this->Direction)); } return $query; } } |