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