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