Completed
Push — master ( 11b587...c342a0 )
by
unknown
16:08
created

LinkButton::setHref()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Backend\Template\Components\Buttons;
17
18
/**
19
 * LinkButton
20
 *
21
 * This button type renders a regular anchor tag with TYPO3s way to render a
22
 * button control.
23
 *
24
 * EXAMPLE USAGE TO ADD A BUTTON TO THE FIRST BUTTON GROUP IN THE LEFT BAR:
25
 *
26
 * $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
27
 * $saveButton = $buttonBar->makeLinkButton()
28
 *      ->setHref('#')
29
 *      ->setDataAttributes([
30
 *          'foo' => 'bar'
31
 *      ])
32
 *      ->setIcon($this->iconFactory->getIcon('actions-document-save', Icon::SIZE_SMALL))
33
 *      ->setTitle('Save');
34
 * $buttonBar->addButton($saveButton, ButtonBar::BUTTON_POSITION_LEFT, 1);
35
 */
36
class LinkButton extends AbstractButton
37
{
38
    /**
39
     * HREF attribute of the link
40
     *
41
     * @var string
42
     */
43
    protected $href = '';
44
45
    /**
46
     * Get href
47
     *
48
     * @return string
49
     */
50
    public function getHref()
51
    {
52
        return $this->href;
53
    }
54
55
    /**
56
     * Set href
57
     *
58
     * @param string $href HREF attribute
59
     *
60
     * @return LinkButton
61
     */
62
    public function setHref($href)
63
    {
64
        $this->href = $href;
65
        return $this;
66
    }
67
68
    /**
69
     * Validates the current button
70
     *
71
     * @return bool
72
     */
73
    public function isValid()
74
    {
75
        if (
76
            trim($this->getHref()) !== ''
77
            && trim($this->getTitle()) !== ''
78
            && $this->getType() === self::class
79
            && $this->getIcon() !== null
80
        ) {
81
            return true;
82
        }
83
        return false;
84
    }
85
86
    /**
87
     * Renders the markup for the button
88
     *
89
     * @return string
90
     */
91
    public function render()
92
    {
93
        $attributes = [
94
            'href' => $this->getHref(),
95
            'class' => 'btn btn-default btn-sm ' . $this->getClasses(),
96
            'title' => $this->getTitle()
97
        ];
98
        $labelText = '';
99
        if ($this->showLabelText) {
100
            $labelText = ' ' . $this->title;
101
        }
102
        foreach ($this->dataAttributes as $attributeName => $attributeValue) {
103
            $attributes['data-' . $attributeName] = $attributeValue;
104
        }
105
        if ($this->onClick !== '') {
106
            $attributes['onclick'] = $this->onClick;
107
        }
108
        if ($this->isDisabled()) {
109
            $attributes['disabled'] = 'disabled';
110
            $attributes['class'] .= ' disabled';
111
        }
112
        $attributesString = '';
113
        foreach ($attributes as $key => $value) {
114
            $attributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
115
        }
116
117
        return '<a ' . $attributesString . '>'
118
            . $this->getIcon()->render() . htmlspecialchars($labelText)
119
        . '</a>';
120
    }
121
122
    /**
123
     * Magic method so Fluid can access a button via {button}
124
     *
125
     * @return string
126
     */
127
    public function __toString()
128
    {
129
        return $this->render();
130
    }
131
}
132