|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental; |
|
4
|
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Models\ElementalArea; |
|
6
|
|
|
use DNADesign\Elemental\Forms\ElementalAreaConfig; |
|
7
|
|
|
use DNADesign\Elemental\Forms\ElementalAreaField; |
|
8
|
|
|
use SilverStripe\Core\Extensible; |
|
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
10
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
11
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
12
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; |
|
13
|
|
|
|
|
14
|
|
|
class ElementalEditor |
|
15
|
|
|
{ |
|
16
|
|
|
use Extensible; |
|
17
|
|
|
use Injectable; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var ElementalArea $area |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $area; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string $name |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $name; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* By default, no need for a title on the editor. If there is more than one |
|
31
|
|
|
* area then use `setTitle` to describe. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string $title |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $title = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array $type |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $types = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
* @param ElementalArea $area |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($name, ElementalArea $area) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->name = $name; |
|
49
|
|
|
$this->area = $area; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param array $types |
|
54
|
|
|
* |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setTypes($types) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->types = $types; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getTypes() |
|
68
|
|
|
{ |
|
69
|
|
|
$types = $this->types; |
|
70
|
|
|
|
|
71
|
|
|
$this->extend('updateGetTypes', $types); |
|
72
|
|
|
|
|
73
|
|
|
return $types; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return ElementalArea |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getArea() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->area; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $title |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setTitle($title) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->title = $title; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return GridField |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getField() |
|
100
|
|
|
{ |
|
101
|
|
|
$gridField = ElementalAreaField::create( |
|
102
|
|
|
$this->name, |
|
103
|
|
|
$this->title, |
|
104
|
|
|
$this->getArea()->Elements(), |
|
105
|
|
|
$config = ElementalAreaConfig::create() |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$gridField->addExtraClass('elemental-editor'); |
|
109
|
|
|
|
|
110
|
|
|
if ($this->types) { |
|
|
|
|
|
|
111
|
|
|
$adder = Injector::inst()->create(GridFieldAddNewMultiClass::class, 'toolbar-header-left'); |
|
112
|
|
|
$adder->setClasses($this->getTypes()); |
|
113
|
|
|
|
|
114
|
|
|
$config->addComponent($adder); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->extend('updateField', $gridField); |
|
118
|
|
|
|
|
119
|
|
|
return $gridField; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.