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