‪TYPO3CMS  10.4
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 (!empty(‪$items['primary']->‪getOnClick())) {
147  $attributes['onclick'] = ‪$items['primary']->getOnClick();
148  }
149  if (method_exists(‪$items['primary'], 'getForm') && !empty(‪$items['primary']->getForm())) {
150  $attributes['form'] = ‪$items['primary']->getForm();
151  }
152  $attributesString = '';
153  foreach ($attributes as $key => $value) {
154  $attributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
155  }
156  $content = '
157  <div class="btn-group t3js-splitbutton">
158  <button' . $attributesString . '>
159  ' . ‪$items['primary']->getIcon()->render('inline') . '
160  ' . htmlspecialchars(‪$items['primary']->‪getTitle()) . '
161  </button>
162  <button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
163  <span class="caret"></span>
164  <span class="sr-only">Toggle Dropdown</span>
165  </button>
166  <ul class="dropdown-menu">';
167 
169  foreach (‪$items['options'] as $option) {
170  if ($option instanceof ‪InputButton) {
171  // if the option is an InputButton we have to create a custom rendering
172  $optionAttributes = [
173  'href' => '#',
174  'data-name' => $option->getName(),
175  'data-value' => $option->getValue(),
176  'data-form' => $option->getForm()
177  ];
178 
179  if (!empty($option->getClasses())) {
180  $optionAttributes['class'] = $option->getClasses();
181  }
182  if (!empty($option->getOnClick())) {
183  $optionAttributes['onclick'] = $option->getOnClick();
184  }
185  $optionAttributesString = '';
186  foreach ($optionAttributes as $key => $value) {
187  $optionAttributesString .= ' ' . htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"';
188  }
189  $html = '<a' . $optionAttributesString . '>' . $option->getIcon()->render('inline') . ' '
190  . htmlspecialchars($option->getTitle()) . '</a>';
191  } else {
192  // for any other kind of button we simply use what comes along (e.g. LinkButton)
193  $html = $option->render();
194  }
195 
196  $content .= '
197  <li>
198  ' . $html . '
199  </li>
200  ';
201  }
202  $content .= '
203  </ul>
204  </div>
205  ';
206  return $content;
207  }
208 
214  public function ‪__toString()
215  {
216  return $this->‪render();
217  }
218 }
‪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\AbstractControl\getOnClick
‪string getOnClick()
Definition: AbstractControl.php:84
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton\isValid
‪bool isValid()
Definition: AbstractButton.php:128
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton
Definition: SplitButton.php:52
‪TYPO3\CMS\Backend\Template\Components\AbstractControl\getTitle
‪string getTitle()
Definition: AbstractControl.php:64
‪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:38
‪TYPO3\CMS\Backend\Template\Components\Buttons\SplitButton\__toString
‪string __toString()
Definition: SplitButton.php:212