Passed
Pull Request — master (#224)
by
unknown
02:40
created

ElementalAreaField   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B performReadonlyTransformation() 0 51 4
1
<?php
2
3
namespace DNADesign\Elemental\Forms;
4
5
use SilverStripe\Forms\GridField\GridField;
6
use SilverStripe\Forms\CompositeField;
7
use SilverStripe\Forms\FieldGroup;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\TabSet;
10
use SilverStripe\Forms\ReadonlyTransformation;
11
12
class ElementalAreaField extends GridField
13
{
14
    public function performReadonlyTransformation()
15
    {
16
        $readOnlyField = $this->castedCopy(CompositeField::class);
17
        
18
        $blockReducer = function ($element) {
19
            $parentName = 'Element' . $element->ID;
20
            $elementFields = $element->getCMSFields();
21
            // Obtain highest impact fields for a summary (e.g. Title & Content)
22
            foreach ($elementFields as $field) {
23
                if (is_object($field) && $field instanceof TabSet) {
24
                    // Assign the fields of the first Tab in the TabSet - most regularly 'Root.Main'
25
                    $elementFields = $field->FieldList()->first()->FieldList();
26
                    break;
27
                }
28
            }
29
            // Set values (before names don't match anymore)
30
            $elementFields->setValues($element->getQueriedDatabaseFields());
31
            // Ensure field names are unique between elements on parent form
32
            $elementFields->recursiveWalk(function ($field) use ($parentName) {
33
                $field->setName($parentName . '_' . $field->getName());
34
            });
35
            // Combine into an appropriately named group
36
            $elementGroup = FieldGroup::create($elementFields);
37
            $elementGroup->setName($parentName);
38
            $elementGroup->addExtraClass('elementalarea__element--historic');
39
            // Also set the important data for the rendering Component
40
            // $elementGroup->setSchemaComponent('HistoricElementView');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
            $elementGroup->setSchemaData([
42
                'data' => [
43
                    'ElementID' => $element->ID,
44
                    'ElementType' => $element->getType(),
45
                    'ElementIcon' => $element->config()->icon,
46
                    'ElementTitle' => $element->Title,
47
                    'ElementEditLink' => $element->CMSEditLink(),
48
                    'extraContext' => 'HistoricElementView'
49
                ]
50
            ]);
51
            
52
            return $elementGroup;
53
        };
54
        
55
        $readOnlyField->setChildren(
56
            FieldList::create(
57
                array_map($blockReducer, $this->getList()->toArray())
58
            )
59
        );
60
        $readOnlyField = $readOnlyField->transform(new ReadonlyTransformation());
61
        $readOnlyField->setReadOnly(true);
62
        $readOnlyField->setName($this->getName());
63
        $readOnlyField->addExtraClass('elementalarea--read-only');
64
        return $readOnlyField;
65
    }
66
}
67