‪TYPO3CMS  ‪main
GenericButton.php
Go to the documentation of this file.
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 
17 
19 use TYPO3\CMS\Core\Imaging\IconSize;
21 
34 {
35  protected string ‪$tag = 'button';
36  protected ?‪Icon ‪$icon = null;
37  protected string ‪$label = '';
38  protected ?string ‪$title = null;
39  protected ?string ‪$href = null;
40  protected string ‪$classes = '';
41  protected array ‪$attributes = [];
42  protected bool ‪$showLabelText = false;
43 
44  public function ‪setTag(string ‪$tag): self
45  {
46  $this->tag = htmlspecialchars(trim(‪$tag));
47  return $this;
48  }
49 
50  public function ‪getTag(): string
51  {
52  return ‪$this->tag;
53  }
54 
55  public function ‪getIcon(): ?‪Icon
56  {
57  return ‪$this->icon;
58  }
59 
60  public function ‪setIcon(?‪Icon ‪$icon): self
61  {
62  ‪$icon?->‪setSize(IconSize::SMALL);
63  $this->icon = ‪$icon;
64  return $this;
65  }
66 
67  public function ‪getLabel(): ?string
68  {
69  return ‪$this->label;
70  }
71 
72  public function ‪setLabel(?string ‪$label): self
73  {
74  $this->label = ‪$label;
75  return $this;
76  }
77 
78  public function ‪getTitle(): ?string
79  {
80  return $this->title ?? ‪$this->label;
81  }
82 
83  public function ‪setTitle(?string ‪$title): self
84  {
85  $this->title = ‪$title;
86  return $this;
87  }
88 
89  public function ‪getHref(): ?string
90  {
91  return ‪$this->href;
92  }
93 
94  public function ‪setHref(?string ‪$href): self
95  {
96  $this->href = ‪$href;
97  return $this;
98  }
99 
100  public function ‪getClasses(): string
101  {
102  return ‪$this->classes;
103  }
104 
105  public function ‪setClasses(string ‪$classes): self
106  {
107  $this->classes = ‪$classes;
108  return $this;
109  }
110 
114  public function ‪setAttributes(array ‪$attributes): self
115  {
116  $this->‪attributes = ‪$attributes;
117  return $this;
118  }
119 
123  public function getAttributes(): array
124  {
126  }
127 
128  public function ‪getShowLabelText(): bool
129  {
131  }
132 
133  public function ‪setShowLabelText(bool ‪$showLabelText): self
134  {
135  $this->showLabelText = ‪$showLabelText;
136  return $this;
137  }
138 
139  public function ‪isValid(): bool
140  {
141  return trim($this->‪getLabel()) !== ''
142  && $this->‪getType() === self::class
143  && $this->‪getIcon() !== null;
144  }
145 
146  public function ‪getType(): string
147  {
148  return static::class;
149  }
150 
151  protected function ‪getAttributesString(): string
152  {
153  ‪$attributes = $this->getAttributes();
154  ‪$attributes['class'] = rtrim('btn btn-sm btn-default ' . $this->‪getClasses());
155  if ($this->‪getHref()) {
156  $attributes['href'] = $this->‪getHref();
157  }
158  if ($this->‪getTitle()) {
159  $attributes['title'] = $this->‪getTitle();
160  }
161 
162  return GeneralUtility::implodeAttributes(‪$attributes, true);
163  }
164 
165  public function ‪render(): string
166  {
167  $labelText = '';
168  if ($this->‪getShowLabelText()) {
169  $labelText = $this->‪getLabel();
170  }
171 
172  return '<' . $this->‪getTag() . ' ' . $this->‪getAttributesString() . '>'
173  . ($this->‪getIcon() ? $this->‪getIcon()->render() : '')
174  . htmlspecialchars($labelText)
175  . '</' . $this->‪getTag() . '>';
176  }
177 
178  public function ‪__toString(): string
179  {
180  return $this->‪render();
181  }
182 }
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$tag
‪string $tag
Definition: GenericButton.php:35
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setAttributes
‪setAttributes(array $attributes)
Definition: GenericButton.php:114
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$title
‪string $title
Definition: GenericButton.php:38
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$classes
‪string $classes
Definition: GenericButton.php:40
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton
Definition: GenericButton.php:34
‪TYPO3\CMS\Core\Imaging\Icon\setSize
‪setSize(string|IconSize $size)
Definition: Icon.php:193
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:27
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getTag
‪getTag()
Definition: GenericButton.php:50
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\__toString
‪__toString()
Definition: GenericButton.php:178
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setShowLabelText
‪setShowLabelText(bool $showLabelText)
Definition: GenericButton.php:133
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setTitle
‪setTitle(?string $title)
Definition: GenericButton.php:83
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getIcon
‪getIcon()
Definition: GenericButton.php:55
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setClasses
‪setClasses(string $classes)
Definition: GenericButton.php:105
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$attributes
‪array $attributes
Definition: GenericButton.php:41
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\attributes
‪array< string, function getAttributes():array { return $this-> attributes
Definition: GenericButton.php:125
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setLabel
‪setLabel(?string $label)
Definition: GenericButton.php:72
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setIcon
‪setIcon(?Icon $icon)
Definition: GenericButton.php:60
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getLabel
‪getLabel()
Definition: GenericButton.php:67
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$href
‪string $href
Definition: GenericButton.php:39
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$icon
‪Icon $icon
Definition: GenericButton.php:36
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setTag
‪setTag(string $tag)
Definition: GenericButton.php:44
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getType
‪getType()
Definition: GenericButton.php:146
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\isValid
‪isValid()
Definition: GenericButton.php:139
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getShowLabelText
‪getShowLabelText()
Definition: GenericButton.php:128
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getAttributesString
‪getAttributesString()
Definition: GenericButton.php:151
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\render
‪render()
Definition: GenericButton.php:165
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getTitle
‪getTitle()
Definition: GenericButton.php:78
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$label
‪string $label
Definition: GenericButton.php:37
‪TYPO3\CMS\Backend\Template\Components\Buttons
Definition: AbstractButton.php:16
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getClasses
‪getClasses()
Definition: GenericButton.php:100
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\getHref
‪getHref()
Definition: GenericButton.php:89
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\setHref
‪setHref(?string $href)
Definition: GenericButton.php:94
‪TYPO3\CMS\Backend\Template\Components\Buttons\GenericButton\$showLabelText
‪bool $showLabelText
Definition: GenericButton.php:42
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:22