Passed
Pull Request — master (#14)
by Robbie
01:44
created

ElementFormExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 3 1
B moveTitleAndDisplayedCheckbox() 0 32 5
1
<?php
2
3
namespace DNADesign\ElementalUserForms\Extension;
4
5
use SilverStripe\Forms\FieldGroup;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\Forms\CheckboxField;
9
use SilverStripe\ORM\DataExtension;
10
11
class ElementFormExtension extends DataExtension
12
{
13
    public function updateCMSFields(FieldList $fields)
14
    {
15
        $this->moveTitleAndDisplayedCheckbox($fields);
16
    }
17
18
    /**
19
     * Move the Title and Displayed checkbox from the Content to Form Fields tab
20
     *
21
     * @param FieldList $fields
22
     * @return $this
23
     */
24
    protected function moveTitleAndDisplayedCheckbox(FieldList $fields)
25
    {
26
        foreach ($fields->fieldByName('Root.Main')->Fields() as $field) {
27
            if (!$field instanceof FieldGroup) {
28
                continue;
29
            }
30
31
            $children = $field->getChildren();
32
            if (!$children->fieldByName('Title') || !$children->fieldByName('ShowTitle')) {
33
                continue;
34
            }
35
36
            /** @see DNADesign\Elemental\Models\BaseElement */
37
            $fields->removeByName('Titlenotdisplayedunlessspecified');
38
39
            $fields->findOrMakeTab('Root.FormFields')->unshift(
40
                FieldGroup::create(
41
                    TextField::create('Title', ''),
0 ignored issues
show
Bug introduced by
'Title' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

41
                    TextField::create(/** @scrutinizer ignore-type */ 'Title', ''),
Loading history...
42
                    CheckboxField::create(
43
                        'ShowTitle',
44
                        _t('DNADesign\\Elemental\\Models\\BaseElement\\.ShowTitleLabel', 'Displayed')
45
                    )
46
                )
47
                    ->setTemplate('DNADesign\\Elemental\\Models\\BaseElement\\FieldGroup')
48
                    ->setTitle(_t(
49
                        'DNADesign\\Elemental\\Models\\BaseElement\\.TitleLabel',
50
                        'Title (not displayed unless specified)'
51
                    ))
52
            );
53
        }
54
55
        return $this;
56
    }
57
}
58