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