Issues (23)

src/Extensions/CarouselPageExtension.php (4 issues)

1
<?php
2
3
namespace CWP\AgencyExtensions\Extensions;
4
5
use CWP\AgencyExtensions\Model\CarouselItem;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
12
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
13
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
14
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
15
use SilverStripe\ORM\DataList;
16
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
17
use SilverStripe\Forms\GridField\GridFieldVersionedState;
18
use SilverStripe\Forms\LiteralField;
19
20
class CarouselPageExtension extends DataExtension
21
{
22
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
23
        'CarouselTitle' => 'Text',
24
    ];
25
26
    private static $has_many = [
0 ignored issues
show
The private property $has_many is not used, and could be removed.
Loading history...
27
        'CarouselItems' => CarouselItem::class,
28
    ];
29
30
    private static $owns = [
0 ignored issues
show
The private property $owns is not used, and could be removed.
Loading history...
31
        'CarouselItems',
32
    ];
33
34
    /**
35
     * @return DataList
36
     */
37
    public function getCarouselItems()
38
    {
39
        return $this->owner->getComponents('CarouselItems')->sort('SortOrder');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->owner->get...ms')->sort('SortOrder') also could return the type SilverStripe\ORM\UnsavedRelationList which is incompatible with the documented return type SilverStripe\ORM\DataList.
Loading history...
40
    }
41
42
    /**
43
     * Add the carousel management GridField to the Page's CMS fields
44
     *
45
     * @param FieldList $fields
46
     */
47
    public function updateCMSFields(FieldList $fields)
48
    {
49
        $gridField = GridField::create(
50
            'CarouselItems',
51
            _t(__CLASS__ . 'TITLE', 'Hero/Carousel'),
52
            $this->getCarouselItems(),
53
            GridFieldConfig_RelationEditor::create()
54
        );
55
        $gridField->setDescription(_t(
56
            __CLASS__ . 'NOTE',
57
            'Carousel functionality will automatically be loaded when 2 or more items are added below'
58
        ));
59
        $gridConfig = $gridField->getConfig();
60
        $gridConfig->getComponentByType(GridFieldAddNewButton::class)->setButtonName(
61
            _t(__CLASS__ . 'ADDNEW', 'Add new')
62
        );
63
        $gridConfig->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
64
        $gridConfig->removeComponentsByType(GridFieldDeleteAction::class);
65
        $gridConfig->addComponent(new GridFieldDeleteAction());
66
        $gridConfig->addComponent(new GridFieldOrderableRows('SortOrder'));
67
        $gridConfig->removeComponentsByType(GridFieldSortableHeader::class);
68
        $gridField->setModelClass(CarouselItem::class);
69
70
        $fields->findOrMakeTab(
71
            'Root.Carousel',
72
            _t(__CLASS__ . 'TITLE', 'Hero/Carousel')
73
        );
74
75
        $titleField = TextField::create('CarouselTitle', 'Carousel Title');
76
        $titleField->setDescription(_t(
77
            __CLASS__ . 'TITLE_NOTE',
78
            'Used by screen readers'
79
        ));
80
81
        $fields->addFieldToTab('Root.Carousel', $titleField);
82
        $fields->addFieldToTab('Root.Carousel', $gridField);
83
    }
84
}
85