EditFormFactory::getForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 13
rs 10
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\HTMLEditor\HTMLEditorField;
10
11
class EditFormFactory extends DefaultFormFactory
12
{
13
    use Configurable;
14
15
    /**
16
     * This will be set the number of rows in HTML field
17
     *
18
     * @config
19
     * @var integer
20
     */
21
    private static $html_field_rows = 7;
0 ignored issues
show
introduced by
The private property $html_field_rows is not used, and could be removed.
Loading history...
22
23
    /**
24
     * @var string
25
     */
26
    const FIELD_NAMESPACE_TEMPLATE = 'PageElements_%d_%s';
27
28
    public function getForm(RequestHandler $controller = null, $name = self::DEFAULT_NAME, $context = [])
29
    {
30
        $form = parent::getForm($controller, $name, $context);
31
32
        // Remove divider lines between form fields
33
        $form->addExtraClass('form--no-dividers');
34
35
        // Namespace all fields - do this after getting getFormFields so they still get populated
36
        $formFields = $form->Fields();
37
        $this->namespaceFields($formFields, $context);
38
        $form->setFields($formFields);
39
40
        return $form;
41
    }
42
43
    protected function getFormFields(RequestHandler $controller = null, $name, $context = [])
44
    {
45
        $fields = parent::getFormFields($controller, $name, $context);
46
47
        // Configure a slimmed down HTML editor for use with blocks
48
        /** @var HTMLEditorField|null $editorField */
49
        $editorField = $fields->fieldByName('Root.Main.HTML');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $editorField is correct as $fields->fieldByName('Root.Main.HTML') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50
        if ($editorField) {
0 ignored issues
show
introduced by
$editorField is of type null, thus it always evaluated to false.
Loading history...
51
            $editorField->setRows($this->config()->get('html_field_rows'));
52
        }
53
54
        return $fields;
55
    }
56
57
    /**
58
     * Given a {@link FieldList}, give all fields a unique name so they can be used in the same context as
59
     * other elemental edit forms and the page (or other DataObject) that owns them.
60
     *
61
     * @param FieldList $fields
62
     * @param array $context
63
     */
64
    protected function namespaceFields(FieldList $fields, array $context)
65
    {
66
        $elementID = $context['Record']->ID;
67
68
        foreach ($fields->dataFields() as $field) {
69
            $namespacedName = sprintf(self::FIELD_NAMESPACE_TEMPLATE ?? '', $elementID, $field->getName());
70
            $field->setName($namespacedName);
71
        }
72
    }
73
}
74