Passed
Pull Request — 4 (#930)
by Steve
03:25
created

EditFormFactory::addClassNameField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            /** @scrutinizer ignore-type */ new HiddenField($fieldName, 'ClassName', get_class($record))
Loading history...
88
        );
89
    }
90
}
91