GridFieldResourceTitle::getHTMLFragments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Forms;
4
5
use SilverStripe\CKANRegistry\Model\Resource;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
8
use SilverStripe\View\ArrayData;
9
10
/**
11
 * Presents a title for the {@link Resource} data set and selected resource, as well as a toggle to hide or show
12
 * the resource locator field in the CMS.
13
 *
14
 * This component is designed to be used on CKANRegistryPage instances.
15
 */
16
class GridFieldResourceTitle implements GridField_HTMLProvider
17
{
18
    use Injectable;
19
20
    /**
21
     * @var Resource
22
     */
23
    protected $resource;
24
25
    /**
26
     * @var string
27
     */
28
    protected $targetFragment = '';
29
30
    /**
31
     * @param Resource $resource
32
     * @param string $targetFragment
33
     */
34
    public function __construct(Resource $resource, $targetFragment = 'buttons-before-left')
35
    {
36
        $this->setResource($resource);
37
        $this->setTargetFragment($targetFragment);
38
    }
39
40
    public function getHTMLFragments($gridField)
41
    {
42
        $data = ArrayData::create([
43
            'Resource' => $this->resource,
44
            'EditLinkTitle' => _t(__CLASS__ . '.EDIT_LINK_TITLE', 'Edit resource'),
45
            'ReadOnly' => $gridField->isReadonly(),
46
        ]);
47
        $result = $data->renderWith(__CLASS__);
48
49
        return [
50
            $this->targetFragment => $result,
51
        ];
52
    }
53
54
    /**
55
     * @param Resource $resource
56
     * @return $this
57
     */
58
    public function setResource(Resource $resource)
59
    {
60
        $this->resource = $resource;
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $targetFragment
66
     * @return $this
67
     */
68
    public function setTargetFragment($targetFragment)
69
    {
70
        $this->targetFragment = (string) $targetFragment;
71
        return $this;
72
    }
73
}
74