GridFieldLinkButton::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Forms;
4
5
use SilverStripe\Forms\GridField\GridField;
6
use SilverStripe\View\ArrayData;
7
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
8
9
/**
10
 * A button that contains a link to an URL.
11
 *
12
 * @package forms
13
 * @subpackage fields-gridfield
14
 */
15
class GridFieldLinkButton implements GridField_HTMLProvider
16
{
17
    /**
18
     * Fragment to write the button to.
19
     * @var string
20
     */
21
    protected $targetFragment;
22
23
    /**
24
     * URL link the button links out to.
25
     * @var string
26
     */
27
    protected $link;
28
29
    /**
30
     * Caption text for the button to show
31
     * @var string
32
     */
33
    protected $caption;
34
35
    /**
36
     * @param string $link The URL link the button links out to.
37
     * @param string $targetFragment The HTML fragment to write the button into
38
     */
39
    public function __construct($link, $caption, $targetFragment)
40
    {
41
        $this->link = $link;
42
        $this->caption = $caption;
43
        $this->targetFragment = $targetFragment;
44
    }
45
46
    /**
47
     * Place the link button in a <p> tag above the field
48
     *
49
     * @param GridField $gridField
50
     *
51
     * @return array
52
     */
53
    public function getHTMLFragments($gridField)
54
    {
55
        $fragment = ArrayData::create([
56
            'Link' => $this->link,
57
            'Caption' => $this->caption,
58
        ])->renderWith(__CLASS__);
59
60
        return [$this->targetFragment => $fragment];
61
    }
62
}
63