1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CWP\AgencyExtensions\Extensions; |
4
|
|
|
|
5
|
|
|
use CWP\AgencyExtensions\Model\CarouselItem; |
6
|
|
|
use SilverStripe\ORM\DataExtension; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldSortableHeader; |
14
|
|
|
use SilverStripe\ORM\DataList; |
15
|
|
|
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldVersionedState; |
17
|
|
|
use SilverStripe\Forms\LiteralField; |
18
|
|
|
|
19
|
|
|
class CarouselPageExtension extends DataExtension |
20
|
|
|
{ |
21
|
|
|
private static $has_many = [ |
22
|
|
|
'CarouselItems' => CarouselItem::class |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private static $owns = [ |
26
|
|
|
'CarouselItems' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return DataList |
31
|
|
|
*/ |
32
|
|
|
public function getCarouselItems() |
33
|
|
|
{ |
34
|
|
|
return $this->owner->getComponents('CarouselItems')->sort('SortOrder'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add the carousel management GridField to the Page's CMS fields |
39
|
|
|
* |
40
|
|
|
* @param FieldList $fields |
41
|
|
|
*/ |
42
|
|
|
public function updateCMSFields(FieldList $fields) |
43
|
|
|
{ |
44
|
|
|
$gridField = GridField::create( |
45
|
|
|
'CarouselItems', |
46
|
|
|
_t(__CLASS__ . 'TITLE', 'Hero/Carousel'), |
47
|
|
|
$this->getCarouselItems(), |
48
|
|
|
GridFieldConfig_RelationEditor::create() |
49
|
|
|
); |
50
|
|
|
$gridField->setDescription(_t( |
51
|
|
|
__CLASS__ . 'NOTE', |
52
|
|
|
'NOTE: Carousel functionality will automatically be loaded when 2 or more items are added below' |
53
|
|
|
)); |
54
|
|
|
$gridConfig = $gridField->getConfig(); |
55
|
|
|
$gridConfig->getComponentByType(GridFieldAddNewButton::class)->setButtonName( |
56
|
|
|
_t(__CLASS__ . 'ADDNEW', 'Add new') |
57
|
|
|
); |
58
|
|
|
$gridConfig->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
59
|
|
|
$gridConfig->removeComponentsByType(GridFieldDeleteAction::class); |
60
|
|
|
$gridConfig->addComponent(new GridFieldDeleteAction()); |
61
|
|
|
$gridConfig->addComponent(new GridFieldVersionedState()); |
62
|
|
|
if (class_exists(GridFieldSortableRows::class)) { |
63
|
|
|
$gridConfig->addComponent(new GridFieldSortableRows('SortOrder')); |
64
|
|
|
} |
65
|
|
|
$gridConfig->removeComponentsByType(GridFieldSortableHeader::class); |
66
|
|
|
$gridField->setModelClass(CarouselItem::class); |
67
|
|
|
|
68
|
|
|
$fields->findOrMakeTab( |
69
|
|
|
'Root.Carousel', |
70
|
|
|
_t(__CLASS__ . 'TITLE', 'Hero/Carousel') |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$fields->addFieldToTab('Root.Carousel', $gridField); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|