cms-niceties/src/Widgets/BannerWidget.php

75 lines
1.7 KiB
PHP
Raw Normal View History

2021-06-19 21:30:03 +02:00
<?php
namespace A2nt\CMSNiceties\Widgets;
use Sheadawson\Linkable\Forms\LinkField;
use Sheadawson\Linkable\Models\Link;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\Widgets\Model\Widget;
if (!class_exists(Widget::class)) {
return;
}
2022-05-10 13:09:06 +02:00
/**
* Class \A2nt\CMSNiceties\Widgets\BannerWidget
*
* @property int $ImageID
* @property int $LinkID
* @method \SilverStripe\Assets\Image Image()
* @method \Sheadawson\Linkable\Models\Link Link()
*/
2021-06-19 21:30:03 +02:00
class BannerWidget extends Widget
{
private static $title = 'Banner';
private static $cmsTitle = 'Banner';
private static $description = 'Shows banner with image and link.';
private static $icon = '<i class="icon font-icon-block-banner"></i>';
private static $table_name = 'BannerWidget';
private static $has_one = [
'Image' => Image::class,
'Link' => Link::class,
];
private static $owns = [
'Image',
'Link',
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->push(UploadField::create('Image', 'Image (minimal width 301px)')
->setAllowedFileCategories(['image/supported']));
$fields->push(LinkField::create('LinkID', 'Link'));
return $fields;
}
private $_random;
public function Random()
{
if (!$this->_random) {
$this->_random = self::get()->filter('Enabled', true)->sort('RAND()')->first();
}
return $this->_random;
}
public function onBeforeWrite()
{
$title = $this->getField('Title');
$img = $this->Image();
if(!$title && $img) {
$this->setField('Title', $img->getTitle());
}
parent::onBeforeWrite();
}
}