1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\Elemental\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\GridField\GridField; |
6
|
|
|
use SilverStripe\Forms\CompositeField; |
7
|
|
|
use SilverStripe\Forms\FieldGroup; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\TabSet; |
10
|
|
|
use SilverStripe\Forms\ReadonlyTransformation; |
11
|
|
|
|
12
|
|
|
class ElementalAreaField extends GridField |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* A getter method that seems redundant in that it is a function that returns a function, |
16
|
|
|
* however the returned closure is used in an array map function to return a complete FieldList |
17
|
|
|
* representing a read only view of the element passed in (to the closure). |
18
|
|
|
* |
19
|
|
|
* @return callable |
20
|
|
|
*/ |
21
|
|
|
protected function getReadOnlyBlockReducer() |
22
|
|
|
{ |
23
|
|
|
return function ($element) { |
24
|
|
|
$parentName = 'Element' . $element->ID; |
25
|
|
|
$elementFields = $element->getCMSFields(); |
26
|
|
|
// Obtain highest impact fields for a summary (e.g. Title & Content) |
27
|
|
|
foreach ($elementFields as $field) { |
28
|
|
|
if (is_object($field) && $field instanceof TabSet) { |
29
|
|
|
// Assign the fields of the first Tab in the TabSet - most regularly 'Root.Main' |
30
|
|
|
$elementFields = $field->FieldList()->first()->FieldList(); |
31
|
|
|
break; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
// Set values (before names don't match anymore) |
35
|
|
|
$elementFields->setValues($element->getQueriedDatabaseFields()); |
36
|
|
|
// Ensure field names are unique between elements on parent form |
37
|
|
|
$elementFields->recursiveWalk(function ($field) use ($parentName) { |
38
|
|
|
$field->setName($parentName . '_' . $field->getName()); |
39
|
|
|
}); |
40
|
|
|
// Combine into an appropriately named group |
41
|
|
|
$elementGroup = FieldGroup::create($elementFields); |
42
|
|
|
$elementGroup->setName($parentName); |
43
|
|
|
$elementGroup->addExtraClass('elemental-area__element--historic'); |
44
|
|
|
// Also set the important data for the rendering Component |
45
|
|
|
$elementGroup->setSchemaData([ |
46
|
|
|
'data' => [ |
47
|
|
|
'ElementID' => $element->ID, |
48
|
|
|
'ElementType' => $element->getType(), |
49
|
|
|
'ElementIcon' => $element->config()->icon, |
50
|
|
|
'ElementTitle' => $element->Title, |
51
|
|
|
// @todo: Change this to block history permalink when that functionality becomes available. |
52
|
|
|
'ElementEditLink' => $element->CMSEditLink() |
53
|
|
|
] |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
return $elementGroup; |
57
|
|
|
}; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Provides a readonly representation of the GridField (superclass) Uses a reducer |
62
|
|
|
* {@see ElementalAreaField::getReadOnlyBlockReducer()} to fetch a read only representation of the listed class |
63
|
|
|
* {@see GridField::getModelClass()} |
64
|
|
|
* |
65
|
|
|
* @return CompositeField |
66
|
|
|
*/ |
67
|
|
|
public function performReadonlyTransformation() |
68
|
|
|
{ |
69
|
|
|
$readOnlyField = $this->castedCopy(CompositeField::class); |
70
|
|
|
$blockReducer = $this->getReadOnlyBlockReducer(); |
71
|
|
|
$readOnlyField->setChildren( |
72
|
|
|
FieldList::create( |
73
|
|
|
array_map($blockReducer, $this->getList()->toArray()) |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
$readOnlyField = $readOnlyField->transform(new ReadonlyTransformation()); |
77
|
|
|
$readOnlyField->setReadOnly(true); |
78
|
|
|
$readOnlyField->setName($this->getName()); |
79
|
|
|
$readOnlyField->addExtraClass('elemental-area--read-only'); |
80
|
|
|
return $readOnlyField; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|