Completed
Pull Request — master (#78)
by
unknown
01:54
created

GridFieldLinkButton::getHTMLFragments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
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
16
class GridFieldLinkButton implements GridField_HTMLProvider
17
{
18
    /**
19
     * Fragment to write the button to.
20
     * @var string
21
     */
22
    protected $targetFragment;
23
24
    /**
25
     * URL link the button links out to.
26
     * @var string
27
     */
28
    protected $link;
29
30
    /**
31
     * Caption text for the button to show
32
     * @var string
33
     */
34
    protected $caption;
35
36
    /**
37
     * @param string $link The URL link the button links out to.
38
     * @param string $targetFragment The HTML fragment to write the button into
39
     */
40
    public function __construct($link, $caption, $targetFragment)
41
    {
42
        $this->link = $link;
43
        $this->caption = $caption;
44
        $this->targetFragment = $targetFragment;
45
    }
46
47
    /**
48
     * Place the link button in a <p> tag above the field
49
     *
50
     * @param GridField $gridField
51
     *
52
     * @return array
53
     */
54
    public function getHTMLFragments($gridField)
55
    {
56
        $fragment = ArrayData::create([
57
            'Link' => $this->link,
58
            'Caption' => $this->caption,
59
        ])->renderWith(__CLASS__);
60
61
        return [$this->targetFragment => $fragment];
62
    }
63
}
64