1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Core\Extensible; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Default form builder class. |
11
|
|
|
* |
12
|
|
|
* @internal WARNING: Experimental and volatile API. |
13
|
|
|
* |
14
|
|
|
* Allows extension by either controller or object via the following methods: |
15
|
|
|
* - updateFormActions |
16
|
|
|
* - updateFormValidator |
17
|
|
|
* - updateFormFields |
18
|
|
|
* - updateForm |
19
|
|
|
*/ |
20
|
|
|
class DefaultFormFactory implements FormFactory { |
21
|
|
|
use Extensible; |
22
|
|
|
|
23
|
|
|
public function __construct() { |
24
|
|
|
$this->constructExtensions(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = []) |
28
|
|
|
{ |
29
|
|
|
// Validate context |
30
|
|
|
foreach($this->getRequiredContext() as $required) { |
31
|
|
|
if (!isset($context[$required])) { |
32
|
|
|
throw new InvalidArgumentException("Missing required context $required"); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$fields = $this->getFormFields($controller, $name, $context); |
37
|
|
|
$actions = $this->getFormActions($controller, $name, $context); |
38
|
|
|
$validator = $this->getFormValidator($controller, $name, $context); |
39
|
|
|
$form = Form::create($controller, $name, $fields, $actions, $validator); |
40
|
|
|
|
41
|
|
|
// Extend form |
42
|
|
|
$this->invokeWithExtensions('updateForm', $form, $controller, $name, $context); |
43
|
|
|
|
44
|
|
|
// Populate form from record |
45
|
|
|
$form->loadDataFrom($context['Record']); |
46
|
|
|
|
47
|
|
|
return $form; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Build field list for this form |
52
|
|
|
* |
53
|
|
|
* @param Controller $controller |
54
|
|
|
* @param string $name |
55
|
|
|
* @param array $context |
56
|
|
|
* @return FieldList |
57
|
|
|
*/ |
58
|
|
|
protected function getFormFields(Controller $controller, $name, $context = []) { |
59
|
|
|
// Fall back to standard "getCMSFields" which itself uses the FormScaffolder as a fallback |
60
|
|
|
// @todo Deprecate or formalise support for getCMSFields() |
61
|
|
|
$fields = $context['Record']->getCMSFields(); |
62
|
|
|
$this->invokeWithExtensions('updateFormFields', $fields, $controller, $name, $context); |
63
|
|
|
return $fields; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Build list of actions for this form |
68
|
|
|
* |
69
|
|
|
* @param Controller $controller |
70
|
|
|
* @param string $name |
71
|
|
|
* @param array $context |
72
|
|
|
* @return FieldList |
73
|
|
|
*/ |
74
|
|
|
protected function getFormActions(Controller $controller, $name, $context = []) { |
75
|
|
|
// @todo Deprecate or formalise support for getCMSActions() |
76
|
|
|
$actions = $context['Record']->getCMSActions(); |
77
|
|
|
$this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context); |
78
|
|
|
return $actions; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Controller $controller |
83
|
|
|
* @param string $name |
84
|
|
|
* @param array $context |
85
|
|
|
* @return null|Validator |
86
|
|
|
*/ |
87
|
|
|
protected function getFormValidator(Controller $controller, $name, $context = []) { |
88
|
|
|
$validator = null; |
89
|
|
|
if ($context['Record']->hasMethod('getCMSValidator')) { |
90
|
|
|
// @todo Deprecate or formalise support for getCMSValidator() |
91
|
|
|
$validator = $context['Record']->getCMSValidator(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Extend validator |
95
|
|
|
$this->invokeWithExtensions('updateFormValidator', $validator, $controller, $name, $context); |
96
|
|
|
return $validator; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return list of mandatory context keys |
101
|
|
|
* |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
public function getRequiredContext() |
105
|
|
|
{ |
106
|
|
|
return ['Record']; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|