‪TYPO3CMS  ‪main
SplitButton.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 
52 {
59  protected ‪$containsPrimaryAction = false;
60 
66  protected ‪$items = [];
67 
78  public function ‪addItem(‪AbstractButton $item, $primaryAction = false)
79  {
80  if (!$item->‪isValid()) {
81  throw new \InvalidArgumentException(
82  'Only valid items may be assigned to a split Button. "' .
83  $item->‪getType() .
84  '" did not pass validation',
85  1441706330
86  );
87  }
88  if ($primaryAction && $this->containsPrimaryAction) {
89  throw new \InvalidArgumentException('A splitButton may only contain one primary action', 1441706340);
90  }
91  if ($primaryAction) {
92  $this->containsPrimaryAction = true;
93  $this->items['primary'] = clone $item;
94  } else {
95  $this->items['options'][] = clone $item;
96  }
97  return $this;
98  }
99 
105  public function ‪getButton()
106  {
107  if (!isset($this->items['primary']) && isset($this->items['options'])) {
108  $primaryAction = array_shift($this->items['options']);
109  $this->items['primary'] = $primaryAction;
110  }
111  return ‪$this->items;
112  }
113 
120  public function ‪isValid()
121  {
122  $subject = $this->‪getButton();
123  return isset($subject['primary'])
124  && ($subject['primary'] instanceof ‪AbstractButton)
125  && isset($subject['options']);
126  }
127 
133  public function ‪render()
134  {
135  ‪$items = $this->‪getButton();
136  $attributes = [
137  'type' => 'submit',
138  'class' => 'btn btn-sm btn-default ' . ‪$items['primary']->getClasses(),
139  ];
140  if (method_exists(‪$items['primary'], 'getName')) {
141  $attributes['name'] = ‪$items['primary']->getName();
142  }
143  if (method_exists(‪$items['primary'], 'getValue')) {
144  $attributes['value'] = ‪$items['primary']->getValue();
145  }
146  if (method_exists(‪$items['primary'], 'getForm') && !empty(‪$items['primary']->getForm())) {
147  $attributes['form'] = ‪$items['primary']->getForm();
148  }
149  $attributesString = '';
150  foreach ($attributes as $key => $value) {
151  $attributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
152  }
153  $content = '
154  <div class="btn-group t3js-splitbutton">
155  <button' . $attributesString . '>
156  ' . ‪$items['primary']->getIcon()->render('inline') . '
157  ' . htmlspecialchars(‪$items['primary']->‪getTitle()) . '
158  </button>
159  <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
160  <span class="visually-hidden">Toggle Dropdown</span>
161  </button>
162  <ul class="dropdown-menu">';
163 
165  foreach (‪$items['options'] as $option) {
166  if ($option instanceof ‪InputButton) {
167  // if the option is an InputButton we have to create a custom rendering
168  $optionAttributes = [
169  'href' => '#',
170  'data-name' => $option->getName(),
171  'data-value' => $option->getValue(),
172  'data-form' => $option->getForm(),
173  ];
174 
175  if (!empty($option->getClasses())) {
176  $optionAttributes['class'] = $option->getClasses();
177  }
178  $optionAttributes['class'] = implode(' ', [$optionAttributes['class'] ?? '', 'dropdown-item']);
179  $optionAttributesString = '';
180  foreach ($optionAttributes as $key => $value) {
181  $optionAttributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
182  }
183  $html = '' .
184  '<a' . $optionAttributesString . '>' .
185  '<span class="dropdown-item-columns">' .
186  '<span class="dropdown-item-column dropdown-item-column-icon" aria-hidden="true">' .
187  $option->getIcon()->render('inline') .
188  '</span>' .
189  '<span class="dropdown-item-column dropdown-item-column-title">' .
190  htmlspecialchars($option->getTitle()) .
191  '</span>' .
192  '</span>' .
193  '</a>';
194  } else {
195  // for any other kind of button we simply use what comes along (e.g. LinkButton)
196  $html = $option->render();
197  }
198 
199  $content .= '
200  <li>
201  ' . $html . '
202  </li>
203  ';
204  }
205  $content .= '
206  </ul>
207  </div>
208  ';
209  return $content;
210  }
211 
215  public function ‪__toString(): string
216  {
217  return $this->‪render();
218  }
219 }
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton\getType
‪string getType()
Definition: AbstractButton.php:82
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\$containsPrimaryAction
‪bool $containsPrimaryAction
Definition: SplitButton.php:58
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\$items
‪array $items
Definition: SplitButton.php:64
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton
Definition: AbstractButton.php:25
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\isValid
‪bool isValid()
Definition: SplitButton.php:118
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton\isValid
‪bool isValid()
Definition: AbstractButton.php:123
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\__toString
‪__toString()
Definition: SplitButton.php:213
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton
Definition: SplitButton.php:52
‪TYPO3\CMS\Backend\Template\Components\AbstractControl\getTitle
‪string getTitle()
Definition: AbstractControl.php:57
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\addItem
‪$this addItem(AbstractButton $item, $primaryAction=false)
Definition: SplitButton.php:76
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\render
‪string render()
Definition: SplitButton.php:131
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\getButton
‪array getButton()
Definition: SplitButton.php:103
‪TYPO3\CMS\Backend\Template\Components\Buttons
Definition: AbstractButton.php:16
‪TYPO3\CMS\Backend\Template\Components\Buttons\InputButton
Definition: InputButton.php:40