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