|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\VersionedAdmin\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use SilverStripe\Forms\CompositeField; |
|
7
|
|
|
use SilverStripe\Forms\FormField; |
|
8
|
|
|
use SilverStripe\Forms\FormTransformation; |
|
9
|
|
|
use SilverStripe\Forms\LiteralField; |
|
10
|
|
|
|
|
11
|
|
|
class DiffTransformation extends FormTransformation |
|
12
|
|
|
{ |
|
13
|
|
|
public function transform(FormField $field) |
|
14
|
|
|
{ |
|
15
|
|
|
if ($field->isComposite()) { |
|
16
|
|
|
/** @var CompositeField $field */ |
|
17
|
|
|
// There isn't an interface for child fields so this is unfortunately just a best guess. |
|
18
|
|
|
$newKids = $field->getChildren()->transform($this); |
|
19
|
|
|
$newField = clone $field; |
|
20
|
|
|
$newField->setChildren($newKids); |
|
21
|
|
|
return $newField; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
if (!$field->hasData()) { |
|
25
|
|
|
// No data; no value. |
|
26
|
|
|
return clone $field; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$name = $field->getName(); |
|
30
|
|
|
|
|
31
|
|
|
// Do not compare generated security data |
|
32
|
|
|
if ( |
|
33
|
|
|
($form = $field->getForm()) |
|
34
|
|
|
&& ($securityToken = $form->getSecurityToken()) |
|
35
|
|
|
&& ($securityTokenName = $securityToken->getName()) |
|
36
|
|
|
&& $securityTokenName === $name |
|
37
|
|
|
) { |
|
38
|
|
|
return LiteralField::create($name, ''); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
try { |
|
42
|
|
|
// First check if a field implements performDiffTransformation() |
|
43
|
|
|
$diffField = parent::transform($field); |
|
44
|
|
|
} catch (BadMethodCallException $e) { |
|
45
|
|
|
$diffField = $field->castedCopy(DiffField::class); |
|
46
|
|
|
$diffField->addExtraClass("history-viewer__version-detail-diff"); |
|
47
|
|
|
$diffField->setComparisonField($field); |
|
48
|
|
|
} |
|
49
|
|
|
return $diffField; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|