‪TYPO3CMS  10.4
AbstractButtonViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
29 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
30 use TYPO3Fluid\Fluid\View\Exception;
31 
32 abstract class ‪AbstractButtonViewHelper extends AbstractViewHelper
33 {
34  use CompileWithRenderStatic;
35 
41  public function ‪initializeArguments(): void
42  {
43  $this->registerArgument('icon', 'string', 'Icon identifier for the button');
44  $this->registerArgument('title', 'string', 'Title of the button');
45  $this->registerArgument('disabled', 'bool', 'Whether the button is disabled', false, false);
46  $this->registerArgument('showLabel', 'bool', 'Defines whether to show the title as a label within the button', false, false);
47  $this->registerArgument('position', 'string', 'Position of the button (left or right)');
48  $this->registerArgument('group', 'integer', 'Button group of the button');
49  }
50 
58  public static function ‪renderStatic(
59  array $arguments,
60  \Closure $renderChildrenClosure,
61  RenderingContextInterface $renderingContext
62  ): void {
63  $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
64  ‪self::ensureProperNesting($viewHelperVariableContainer);
65 
67  $moduleTemplate = $viewHelperVariableContainer->get(ModuleLayoutViewHelper::class, ModuleTemplate::class);
68  $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
69 
70  $position = $arguments['position'] ?? ‪ButtonBar::BUTTON_POSITION_LEFT;
71  $group = $arguments['group'] ?? 1;
72  $button = static::createButton($buttonBar, $arguments, $renderingContext);
73  if ($button instanceof ‪AbstractButton) {
74  ‪self::addDefaultAttributes($button, $arguments, $renderingContext);
75  }
76  $buttonBar->addButton($button, $position, $group);
77  }
78 
79  abstract protected static function ‪createButton(‪ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext): ‪ButtonInterface;
80 
85  private static function ‪ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer): void
86  {
87  if (!$viewHelperVariableContainer->exists(ModuleLayoutViewHelper::class, ModuleTemplate::class)) {
88  throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout> ViewHelper', self::class), 1531216505);
89  }
90  }
91 
92  private static function ‪addDefaultAttributes(‪AbstractButton $button, array $arguments, RenderingContextInterface $renderingContext): void
93  {
94  $button->‪setShowLabelText($arguments['showLabel']);
95  if (isset($arguments['title'])) {
96  $button->‪setTitle($arguments['title']);
97  }
99  $moduleTemplate = $renderingContext->getViewHelperVariableContainer()->get(ModuleLayoutViewHelper::class, ModuleTemplate::class);
100  $button->‪setIcon($moduleTemplate->getIconFactory()->getIcon($arguments['icon'], ‪Icon::SIZE_SMALL));
101  }
102 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_LEFT
‪const BUTTON_POSITION_LEFT
Definition: ButtonBar.php:36
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:32
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton\setIcon
‪$this setIcon(Icon $icon)
Definition: AbstractButton.php:94
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper
Definition: AbstractButtonViewHelper.php:33
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton
Definition: AbstractButton.php:25
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:43
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\initializeArguments
‪initializeArguments()
Definition: AbstractButtonViewHelper.php:40
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\createButton
‪static createButton(ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext)
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button
Definition: AbstractButtonViewHelper.php:18
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayoutViewHelper
Definition: ModuleLayoutViewHelper.php:48
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\addDefaultAttributes
‪static addDefaultAttributes(AbstractButton $button, array $arguments, RenderingContextInterface $renderingContext)
Definition: AbstractButtonViewHelper.php:91
‪TYPO3\CMS\Backend\Template\Components\AbstractControl\setTitle
‪$this setTitle($title)
Definition: AbstractControl.php:109
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\ensureProperNesting
‪static ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer)
Definition: AbstractButtonViewHelper.php:84
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: AbstractButtonViewHelper.php:57
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:22
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton\setShowLabelText
‪$this setShowLabelText($showLabelText)
Definition: AbstractButton.php:61