1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\DynamicBlocks\Block; |
4
|
|
|
|
5
|
|
|
use Dynamic\DynamicBlocks\Model\AccordionPanel; |
6
|
|
|
use League\Flysystem\Config; |
7
|
|
|
use SheaDawson\Blocks\Controllers\BlockController; |
8
|
12 |
|
use SheaDawson\Blocks\Model\Block; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
12 |
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
12
|
|
|
use SilverStripe\View\Requirements; |
13
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
14
|
|
|
|
15
|
|
|
class AccordionBlock extends Block |
16
|
1 |
|
{ |
17
|
|
|
/** |
18
|
1 |
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public function singular_name() |
21
|
|
|
{ |
22
|
|
|
return _t('AccordionBlock.SINGULARNAME', 'Accordion Block'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function plural_name() |
29
|
|
|
{ |
30
|
|
|
return _t('AccordionBlock.PLURALNAME', 'Accordion Blocks'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private static $db = array( |
37
|
|
|
'Content' => 'HTMLText', |
38
|
|
|
'SortOrder' => 'Int', |
39
|
1 |
|
); |
40
|
|
|
|
41
|
1 |
|
/** |
42
|
|
|
* @var array |
43
|
1 |
|
*/ |
44
|
1 |
|
private static $has_many = array( |
45
|
1 |
|
'Panels' => AccordionPanel::class, |
46
|
1 |
|
); |
47
|
|
|
|
48
|
1 |
|
/** |
49
|
1 |
|
* @return FieldList |
50
|
1 |
|
*/ |
51
|
1 |
|
public function getCMSFields() |
52
|
1 |
|
{ |
53
|
1 |
|
$fields = parent::getCMSFields(); |
54
|
1 |
|
|
55
|
|
|
$fields->removeByName(array( |
56
|
1 |
|
'SortOrder', |
57
|
1 |
|
'Panels', |
58
|
1 |
|
)); |
59
|
1 |
|
|
60
|
1 |
|
$config = GridFieldConfig_RecordEditor::create(); |
61
|
|
|
if (class_exists(GridFieldOrderableRows::class)) { |
62
|
1 |
|
$config->addComponent(new GridFieldOrderableRows()); |
63
|
|
|
} |
64
|
|
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
65
|
|
|
$config->removeComponentsByType(GridFieldDeleteAction::class); |
66
|
|
|
$config->addComponent(new GridFieldDeleteAction(false)); |
67
|
|
|
|
68
|
1 |
|
if ($this->ID) { |
69
|
|
|
$fields->addFieldsToTab('Root.Panels', array( |
70
|
1 |
|
GridField::create('Panels', 'Accordion Panels', $this->Panels()->sort('Sort'), $config), |
|
|
|
|
71
|
1 |
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $fields; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return DataList |
79
|
|
|
*/ |
80
|
|
|
public function getPanelList() |
81
|
|
|
{ |
82
|
|
|
if ($this->Panels()->exists()) { |
|
|
|
|
83
|
|
|
return $this->Panels()->sort('Sort'); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
return $this->Panels(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
class AccordionBlockController extends BlockController |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
/** |
92
|
|
|
* @var string |
93
|
|
|
*/ |
94
|
|
|
private static $accordion_class = 'accordion-block'; |
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
|
|
public function init() |
100
|
|
|
{ |
101
|
|
|
$class = $this->AccordionClass(); |
102
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery-ui/jquery-ui.js'); |
103
|
|
|
Requirements::customScript(' |
104
|
|
|
$(function() { |
105
|
|
|
$( ".' . $class . '" ).accordion({ |
106
|
|
|
header: ".accord-header", |
107
|
|
|
collapsible: true, heightStyle: "content" |
108
|
|
|
}); |
109
|
|
|
}); |
110
|
|
|
'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function AccordionClass() |
117
|
|
|
{ |
118
|
|
|
return Config::inst()->get('AccordionBlockController', 'accordion_class'); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: