‪TYPO3CMS  ‪main
DropDownButton.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 
21 use TYPO3\CMS\Core\Imaging\IconSize;
23 
48 {
49  protected ?‪Icon ‪$icon = null;
50  protected string ‪$label = '';
51  protected ?string ‪$title = null;
52  protected array ‪$items = [];
53  protected bool ‪$showLabelText = false;
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 ‪getShowLabelText(): bool
90  {
92  }
93 
94  public function ‪setShowLabelText(bool ‪$showLabelText): self
95  {
96  $this->showLabelText = ‪$showLabelText;
97  return $this;
98  }
99 
100  public function ‪addItem(‪DropDownItemInterface $item): self
101  {
102  if (!$item->‪isValid()) {
103  throw new \InvalidArgumentException(
104  'Only valid items may be assigned to a DropdownButton. "' .
105  $item->‪getType() .
106  '" did not pass validation',
107  1667645426
108  );
109  }
110 
111  $this->items[] = clone $item;
112 
113  return $this;
114  }
115 
119  public function ‪getItems(): array
120  {
121  return ‪$this->items;
122  }
123 
127  public function ‪isValid()
128  {
129  return !empty($this->‪getLabel())
130  && ($this->‪getShowLabelText() || $this->‪getIcon())
131  && !empty($this->‪getItems());
132  }
133 
137  public function ‪getType()
138  {
139  return static::class;
140  }
141 
145  public function ‪render()
146  {
147  ‪$items = $this->‪getItems();
148 
152  $activeItems = array_filter(‪$items, function (‪DropDownItemInterface $item): bool {
153  return $item instanceof ‪DropDownRadio && $item->isActive();
154  });
155  if (!empty($activeItems)) {
156  $activeItem = array_shift($activeItems);
157  if ($activeItem->getIcon()) {
158  $this->‪setIcon($activeItem->getIcon());
159  }
160  }
161 
162  $attributes = [
163  'type' => 'button',
164  'class' => 'btn btn-sm btn-default dropdown-toggle',
165  'data-bs-toggle' => 'dropdown',
166  'aria-expanded' => 'false',
167  ];
168  if ($this->‪getTitle()) {
169  $attributes['title'] = $this->‪getTitle();
170  }
171 
172  $labelText = '';
173  if ($this->‪getShowLabelText()) {
174  $labelText = ' ' . $this->‪getLabel();
175  }
176 
177  $content = '<div class="btn-group">'
178  . '<button ' . GeneralUtility::implodeAttributes($attributes, true) . '>'
179  . ($this->‪getIcon() !== null ? $this->‪getIcon()->render() : '')
180  . htmlspecialchars($labelText)
181  . '</button>'
182  . '<ul class="dropdown-menu">';
183 
185  foreach (‪$items as $item) {
186  $content .= '<li>' . $item->render() . '</li>';
187  }
188  $content .= '
189  </ul>
190  </div>';
191  return $content;
192  }
193 
194  public function ‪__toString(): string
195  {
196  return $this->‪render();
197  }
198 }
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\$items
‪array $items
Definition: DropDownButton.php:52
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\$label
‪string $label
Definition: DropDownButton.php:50
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\addItem
‪addItem(DropDownItemInterface $item)
Definition: DropDownButton.php:100
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\$icon
‪Icon $icon
Definition: DropDownButton.php:49
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\getShowLabelText
‪getShowLabelText()
Definition: DropDownButton.php:89
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton
Definition: DropDownButton.php:48
‪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\DropDownButton\__toString
‪__toString()
Definition: DropDownButton.php:194
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\setShowLabelText
‪setShowLabelText(bool $showLabelText)
Definition: DropDownButton.php:94
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\setTitle
‪setTitle(?string $title)
Definition: DropDownButton.php:83
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\$title
‪string $title
Definition: DropDownButton.php:51
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\setIcon
‪setIcon(?Icon $icon)
Definition: DropDownButton.php:60
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\getTitle
‪getTitle()
Definition: DropDownButton.php:78
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\getIcon
‪getIcon()
Definition: DropDownButton.php:55
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\getLabel
‪getLabel()
Definition: DropDownButton.php:67
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownRadio
Definition: DropDownRadio.php:54
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\getType
‪string getType()
Definition: DropDownButton.php:137
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownItemInterface
Definition: DropDownItemInterface.php:19
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface\render
‪string render()
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\$showLabelText
‪bool $showLabelText
Definition: DropDownButton.php:53
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\setLabel
‪setLabel(string $label)
Definition: DropDownButton.php:72
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\isValid
‪bool isValid()
Definition: DropDownButton.php:127
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownItemInterface\getType
‪getType()
‪TYPO3\CMS\Backend\Template\Components\Buttons
Definition: AbstractButton.php:16
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDown\DropDownItemInterface\isValid
‪isValid()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Template\Components\Buttons\DropDownButton\getItems
‪DropDownItemInterface[] getItems()
Definition: DropDownButton.php:119
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:22