-
Notifications
You must be signed in to change notification settings - Fork 5
/
BannerBlock.php
106 lines (86 loc) · 2.81 KB
/
BannerBlock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace SilverStripe\ElementalBannerBlock\Block;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ElementalFileBlock\Block\FileBlock;
use SilverStripe\Forms\FieldList;
use SilverStripe\View\ArrayData;
class BannerBlock extends FileBlock
{
private static $icon = 'font-icon-block-banner';
private static $db = [
'Content' => 'HTMLText',
'CallToActionLink' => 'Link',
];
private static $singular_name = 'banner';
private static $plural_name = 'banners';
private static $table_name = 'S_EB_BannerBlock';
public function getType()
{
return _t(__CLASS__ . '.BlockType', 'Banner');
}
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// Remove default scaffolded relationship fields
$fields->removeByName('CallToActionLinkID');
// Move the file upload field to be before the content
$upload = $fields->fieldByName('Root.Main.File');
$fields->insertBefore('Content', $upload);
// Set the height of the content fields
$fields->fieldByName('Root.Main.Content')->setRows(5);
$fields->fieldByName('Root.Main.CallToActionLink')
->setTitle(_t(__CLASS__ . '.CallToActionTitle', 'Call to action link'));
});
return parent::getCMSFields();
}
/**
* For the frontend, return a parsed set of data for use in templates
*
* @return ArrayData|null
*/
public function CallToActionLink()
{
return $this->decodeLinkData($this->getField('CallToActionLink'));
}
/**
* Add the banner content instead of the image title
*
* {@inheritDoc}
*/
public function getSummary()
{
if ($this->File() && $this->File()->exists()) {
return $this->getSummaryThumbnail() . $this->dbObject('Content')->Summary(20);
}
return '';
}
/**
* Return content summary for summary section of ElementEditor
*
* @return array
*/
protected function provideBlockSchema()
{
$blockSchema = parent::provideBlockSchema();
$blockSchema['content'] = $this->dbObject('Content')->Summary(20);
return $blockSchema;
}
/**
* Given a set of JSON data, decode it, attach the relevant Page object and return as ArrayData
*
* @param string $linkJson
* @return ArrayData|null
*/
protected function decodeLinkData($linkJson)
{
if (!$linkJson || $linkJson === 'null') {
return;
}
$data = ArrayData::create(json_decode($linkJson));
// Link page, if selected
if ($data->PageID) {
$data->setField('Page', self::get_by_id(SiteTree::class, $data->PageID));
}
return $data;
}
}