Completed
Pull Request — master (#323)
by Damian
01:55
created

HistoryListField::getSchemaStateDefaults()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
4
namespace SilverStripe\AssetAdmin\Forms;
5
6
use SilverStripe\Forms\FormField;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\Versioning\Versioned;
9
10
/**
11
 * History view for file editor form
12
 */
13
class HistoryListField extends FormField
14
{
15
    /**
16
     * @var DataObject
17
     */
18
    protected $record = null;
19
20
    protected $schemaComponent = 'HistoryList';
21
22
    protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
23
24
    public function getSchemaStateDefaults()
25
    {
26
        $state = parent::getSchemaStateDefaults();
27
        if ($record = $this->getRecord()) {
28
            $latest = Versioned::get_latest_version($record->baseClass(), $record->ID);
29
            if ($latest) {
30
                $state['data']['fileId'] = $latest->ID;
31
                $state['data']['latestVersionId'] = $latest->Version;
32
            }
33
        }
34
35
        return $state;
36
    }
37
38
    /**
39
     * @return DataObject
40
     */
41
    public function getRecord()
42
    {
43
        return $this->record;
44
    }
45
46
    /**
47
     * @param DataObject $record
48
     * @return $this
49
     */
50
    public function setRecord(DataObject $record)
51
    {
52
        $this->record = $record;
53
        return $this;
54
    }
55
}
56