1 | <?php |
||
2 | |||
3 | namespace DNADesign\Elemental\Forms; |
||
4 | |||
5 | use SilverStripe\Control\RequestHandler; |
||
6 | use SilverStripe\Forms\DefaultFormFactory; |
||
7 | use SilverStripe\Forms\FieldList; |
||
8 | use SilverStripe\Forms\FormField; |
||
9 | use SilverStripe\Forms\HTMLEditor\HTMLEditorField; |
||
10 | |||
11 | class EditFormFactory extends DefaultFormFactory |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | const FIELD_NAMESPACE_TEMPLATE = 'PageElements_%d_%s'; |
||
17 | |||
18 | public function getForm(RequestHandler $controller = null, $name = self::DEFAULT_NAME, $context = []) |
||
19 | { |
||
20 | $form = parent::getForm($controller, $name, $context); |
||
21 | |||
22 | // Remove divider lines between form fields |
||
23 | $form->addExtraClass('form--no-dividers'); |
||
24 | |||
25 | // Namespace all fields - do this after getting getFormFields so they still get populated |
||
26 | $formFields = $form->Fields(); |
||
27 | $this->namespaceFields($formFields, $context); |
||
28 | $form->setFields($formFields); |
||
29 | |||
30 | return $form; |
||
31 | } |
||
32 | |||
33 | protected function getFormFields(RequestHandler $controller = null, $name, $context = []) |
||
34 | { |
||
35 | $fields = parent::getFormFields($controller, $name, $context); |
||
36 | |||
37 | /** @var HTMLEditorField $contentField */ |
||
38 | $contentField = $fields->fieldByName('Root.Main.HTML'); |
||
39 | if ($contentField) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
40 | $contentField->setRows(5); |
||
41 | } |
||
42 | |||
43 | return $fields; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Given a {@link FieldList}, give all fields a unique name so they can be used in the same context as |
||
48 | * other elemental edit forms and the page (or other DataObject) that owns them. |
||
49 | * |
||
50 | * @param FieldList $fields |
||
51 | * @param array $context |
||
52 | */ |
||
53 | protected function namespaceFields(FieldList $fields, array $context) |
||
54 | { |
||
55 | $elementID = $context['Record']->ID; |
||
56 | |||
57 | foreach ($fields->dataFields() as $field) { |
||
58 | $namespacedName = sprintf(self::FIELD_NAMESPACE_TEMPLATE, $elementID, $field->getName()); |
||
59 | $field->setName($namespacedName); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 |