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