SubsiteSafeDataObject::getCMSFields()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Just little class to make any SilverStripe dataobject subsite-safe.
5
 *
6
 * How to use this? Instead of this:
7
 *
8
 * ```php
9
 * class MyDataObject extends DataObject
10
 * ```
11
 *
12
 * simply do this:
13
 *
14
 * ```php
15
 * class MyDataObject extends SubsiteSafeDataObject
16
 * ```
17
 *
18
 * @author Peter Thaleikis
19
 */
20
21
class SubsiteSafeDataObject extends DataObject
22
{
23
    /**
24
     * @var array
25
     */
26
    private static $has_one = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
        'Subsite' => 'Subsite',
28
    ];
29
30
    /**
31
     * make the form a bit more usable.
32
     *
33
     * @return FieldList
34
     */
35
    public function getCMSFields()
36
    {
37
        $fields = parent::getCMSFields();
38
39
        // remove it and, if needed re-add it as a hidden field
40
        $fields->removeByName('SubsiteID');
41
        if (class_exists('Subsite')) {
42
            $fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
43
        }
44
45
        return $fields;
46
    }
47
}
48