Completed
Pull Request — master (#7)
by Robbie
02:15
created

BannerBlock::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ElementalBlocks\Block;
4
5
use SilverStripe\ElementalBlocks\Form\BlockLinkField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\HiddenField;
8
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
9
use SilverStripe\View\Requirements;
10
11
class BannerBlock extends FileBlock
12
{
13
    private static $db = [
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
        'Content' => 'HTMLText',
15
        'ImageLink' => 'Link',
16
        'CallToActionLink' => 'Link',
17
    ];
18
19
    private static $singular_name = 'banner';
0 ignored issues
show
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
20
21
    private static $plural_name = 'banners';
0 ignored issues
show
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
    public function getType()
24
    {
25
        return _t(__CLASS__ . '.BlockType', 'Banner');
26
    }
27
28
    public function getCMSFields()
29
    {
30
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
31
            // Remove default scaffolded relationship fields
32
            $fields->removeByName('ImageLinkID');
33
            $fields->removeByName('CallToActionLinkID');
34
35
            // Create hidden inputs for JSON input from the "insert link" modal
36
            $fields->addFieldsToTab('Root.Main', [
37
                HiddenField::create('ImageLinkData'),
0 ignored issues
show
Bug introduced by
'ImageLinkData' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
                HiddenField::create(/** @scrutinizer ignore-type */ 'ImageLinkData'),
Loading history...
38
                HiddenField::create('CallToActionLinkData'),
39
            ]);
40
41
            // Move the file upload field to be before the content
42
            $upload = $fields->fieldByName('Root.Main.File');
43
            $fields->insertBefore('Content', $upload);
44
45
            // Move the Image Link to underneath the file upload field
46
            $imageLink = $fields->fieldByName('Root.Main.ImageLink');
47
            $fields->insertBefore('Content', $imageLink);
48
49
            // Set the height of the content fields
50
            $fields->fieldByName('Root.Main.Content')->setRows(5);
51
        });
52
53
        // Ensure TinyMCE's javascript is loaded before the blocks overrides
54
        Requirements::javascript(TinyMCEConfig::get('cms')->getScriptURL());
55
        Requirements::javascript('silverstripe/elemental-blocks:client/dist/js/bundle.js');
56
        Requirements::css('silverstripe/elemental-blocks:client/dist/styles/bundle.css');
57
58
        return parent::getCMSFields();
59
    }
60
}
61