silverstripe /
silverstripe-elemental-bannerblock
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace SilverStripe\ElementalBannerBlock\Block; |
||||
| 4 | |||||
| 5 | use SilverStripe\CMS\Model\SiteTree; |
||||
| 6 | use SilverStripe\ElementalFileBlock\Block\FileBlock; |
||||
| 7 | use SilverStripe\Forms\FieldList; |
||||
| 8 | use SilverStripe\View\ArrayData; |
||||
| 9 | |||||
| 10 | class BannerBlock extends FileBlock |
||||
| 11 | { |
||||
| 12 | private static $icon = 'font-icon-block-banner'; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 13 | |||||
| 14 | private static $db = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 15 | 'Content' => 'HTMLText', |
||||
| 16 | 'CallToActionLink' => 'Link', |
||||
| 17 | ]; |
||||
| 18 | |||||
| 19 | private static $singular_name = 'banner'; |
||||
|
0 ignored issues
–
show
|
|||||
| 20 | |||||
| 21 | private static $plural_name = 'banners'; |
||||
|
0 ignored issues
–
show
|
|||||
| 22 | |||||
| 23 | private static $table_name = 'S_EB_BannerBlock'; |
||||
|
0 ignored issues
–
show
|
|||||
| 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 | $fields->fieldByName('Root.Main.CallToActionLink') |
||||
| 44 | ->setTitle(_t(__CLASS__ . '.CallToActionTitle', 'Call to action link')); |
||||
| 45 | }); |
||||
| 46 | |||||
| 47 | return parent::getCMSFields(); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * For the frontend, return a parsed set of data for use in templates |
||||
| 52 | * |
||||
| 53 | * @return ArrayData|null |
||||
| 54 | */ |
||||
| 55 | public function CallToActionLink() |
||||
| 56 | { |
||||
| 57 | return $this->decodeLinkData($this->getField('CallToActionLink')); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * Add the banner content instead of the image title |
||||
| 62 | * |
||||
| 63 | * {@inheritDoc} |
||||
| 64 | */ |
||||
| 65 | public function getSummary() |
||||
| 66 | { |
||||
| 67 | if ($this->File() && $this->File()->exists()) { |
||||
|
0 ignored issues
–
show
The method
File() does not exist on SilverStripe\ElementalBa...Block\Block\BannerBlock. Since you implemented __call, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 68 | return $this->getSummaryThumbnail() . $this->dbObject('Content')->Summary(20); |
||||
| 69 | } |
||||
| 70 | return ''; |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | /** |
||||
| 74 | * Return content summary for summary section of ElementEditor |
||||
| 75 | * |
||||
| 76 | * @return array |
||||
| 77 | */ |
||||
| 78 | protected function provideBlockSchema() |
||||
| 79 | { |
||||
| 80 | $blockSchema = parent::provideBlockSchema(); |
||||
| 81 | $blockSchema['content'] = $this->dbObject('Content')->Summary(20); |
||||
| 82 | return $blockSchema; |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * Given a set of JSON data, decode it, attach the relevant Page object and return as ArrayData |
||||
| 87 | * |
||||
| 88 | * @param string $linkJson |
||||
| 89 | * @return ArrayData|null |
||||
| 90 | */ |
||||
| 91 | protected function decodeLinkData($linkJson) |
||||
| 92 | { |
||||
| 93 | if (!$linkJson || $linkJson === 'null') { |
||||
| 94 | return; |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | $data = ArrayData::create(json_decode($linkJson)); |
||||
| 98 | |||||
| 99 | // Link page, if selected |
||||
| 100 | if ($data->PageID) { |
||||
| 101 | $data->setField('Page', self::get_by_id(SiteTree::class, $data->PageID)); |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | return $data; |
||||
| 105 | } |
||||
| 106 | } |
||||
| 107 |