‪TYPO3CMS  9.5
AbstractButtonViewHelper.php
Go to the documentation of this file.
1 <?php
2 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 
24 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
28 use TYPO3Fluid\Fluid\View\Exception;
29 
30 abstract class ‪AbstractButtonViewHelper extends AbstractViewHelper
31 {
32  use CompileWithRenderStatic;
33 
39  public function ‪initializeArguments(): void
40  {
41  $this->registerArgument('icon', 'string', 'Icon identifier for the button');
42  $this->registerArgument('title', 'string', 'Title of the button');
43  $this->registerArgument('disabled', 'bool', 'Whether the button is disabled', false, false);
44  $this->registerArgument('showLabel', 'bool', 'Defines whether to show the title as a label within the button', false, false);
45  $this->registerArgument('position', 'string', 'Position of the button (left or right)');
46  $this->registerArgument('group', 'integer', 'Button group of the button');
47  }
48 
56  public static function ‪renderStatic(
57  array $arguments,
58  \Closure $renderChildrenClosure,
59  RenderingContextInterface $renderingContext
60  ): void {
61  $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
62  ‪self::ensureProperNesting($viewHelperVariableContainer);
63 
65  $moduleTemplate = $viewHelperVariableContainer->get(ModuleLayoutViewHelper::class, ModuleTemplate::class);
66  $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
67 
68  $position = $arguments['position'] ?? ‪ButtonBar::BUTTON_POSITION_LEFT;
69  $group = $arguments['group'] ?? 1;
70  $button = static::createButton($buttonBar, $arguments, $renderingContext);
71  if ($button instanceof ‪AbstractButton) {
72  ‪self::addDefaultAttributes($button, $arguments, $renderingContext);
73  }
74  $buttonBar->addButton($button, $position, $group);
75  }
76 
77  abstract protected static function ‪createButton(‪ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext): ‪ButtonInterface;
78 
83  private static function ‪ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer): void
84  {
85  if (!$viewHelperVariableContainer->exists(ModuleLayoutViewHelper::class, ModuleTemplate::class)) {
86  throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout> ViewHelper', self::class), 1531216505);
87  }
88  }
89 
90  private static function ‪addDefaultAttributes(‪AbstractButton $button, array $arguments, RenderingContextInterface $renderingContext): void
91  {
92  $button->‪setShowLabelText($arguments['showLabel']);
93  if (isset($arguments['title'])) {
94  $button->‪setTitle($arguments['title']);
95  }
97  $moduleTemplate = $renderingContext->getViewHelperVariableContainer()->get(ModuleLayoutViewHelper::class, ModuleTemplate::class);
98  $button->‪setIcon($moduleTemplate->getIconFactory()->getIcon($arguments['icon'], ‪Icon::SIZE_SMALL));
99  }
100 }
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:29
‪TYPO3\CMS\Backend\Template\Components\ButtonBar\BUTTON_POSITION_LEFT
‪const BUTTON_POSITION_LEFT
Definition: ButtonBar.php:35
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:31
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton\setIcon
‪$this setIcon(Icon $icon)
Definition: AbstractButton.php:93
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper
Definition: AbstractButtonViewHelper.php:31
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:25
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton
Definition: AbstractButton.php:24
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:40
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\initializeArguments
‪initializeArguments()
Definition: AbstractButtonViewHelper.php:38
‪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:3
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayoutViewHelper
Definition: ModuleLayoutViewHelper.php:46
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\addDefaultAttributes
‪static addDefaultAttributes(AbstractButton $button, array $arguments, RenderingContextInterface $renderingContext)
Definition: AbstractButtonViewHelper.php:89
‪TYPO3\CMS\Backend\Template\Components\AbstractControl\setTitle
‪$this setTitle($title)
Definition: AbstractControl.php:108
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\ensureProperNesting
‪static ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer)
Definition: AbstractButtonViewHelper.php:82
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: AbstractButtonViewHelper.php:55
‪TYPO3\CMS\Backend\Template\Components\Buttons\ButtonInterface
Definition: ButtonInterface.php:21
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton\setShowLabelText
‪$this setShowLabelText($showLabelText)
Definition: AbstractButton.php:60