‪TYPO3CMS  11.5
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 
28 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
29 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
30 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
31 use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
32 use TYPO3Fluid\Fluid\View\Exception;
33 
37 abstract class ‪AbstractButtonViewHelper extends AbstractViewHelper
38 {
39  use CompileWithRenderStatic;
40 
46  public function ‪initializeArguments(): void
47  {
48  $this->registerArgument('icon', 'string', 'Icon identifier for the button');
49  $this->registerArgument('title', 'string', 'Title of the button');
50  $this->registerArgument('disabled', 'bool', 'Whether the button is disabled', false, false);
51  $this->registerArgument('showLabel', 'bool', 'Defines whether to show the title as a label within the button', false, false);
52  $this->registerArgument('position', 'string', 'Position of the button (left or right)');
53  $this->registerArgument('group', 'integer', 'Button group of the button');
54  }
55 
63  public static function ‪renderStatic(
64  array $arguments,
65  \Closure $renderChildrenClosure,
66  RenderingContextInterface $renderingContext
67  ): void {
68  trigger_error(__CLASS__ . ' will be removed in TYPO3 v12.', E_USER_DEPRECATED);
69  $viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
70  ‪self::ensureProperNesting($viewHelperVariableContainer);
71 
73  $moduleTemplate = $viewHelperVariableContainer->get(ModuleLayoutViewHelper::class, ModuleTemplate::class);
74  $buttonBar = $moduleTemplate->getDocHeaderComponent()->getButtonBar();
75 
76  $position = $arguments['position'] ?? ‪ButtonBar::BUTTON_POSITION_LEFT;
77  $group = $arguments['group'] ?? 1;
78  $button = static::createButton($buttonBar, $arguments, $renderingContext);
79  if ($button instanceof ‪AbstractButton) {
80  ‪self::addDefaultAttributes($button, $arguments, $renderingContext);
81  }
82  $buttonBar->addButton($button, $position, $group);
83  }
84 
85  abstract protected static function ‪createButton(‪ButtonBar $buttonBar, array $arguments, RenderingContextInterface $renderingContext): ‪ButtonInterface;
86 
91  private static function ‪ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer): void
92  {
93  if (!$viewHelperVariableContainer->exists(ModuleLayoutViewHelper::class, ModuleTemplate::class)) {
94  throw new Exception(sprintf('%s must be nested in <f.be.moduleLayout> ViewHelper', self::class), 1531216505);
95  }
96  }
97 
98  private static function ‪addDefaultAttributes(‪AbstractButton $button, array $arguments, RenderingContextInterface $renderingContext): void
99  {
100  $button->‪setShowLabelText($arguments['showLabel']);
101  if (isset($arguments['title'])) {
102  $button->‪setTitle($arguments['title']);
103  }
104  $button->‪setIcon(GeneralUtility::makeInstance(IconFactory::class)->getIcon($arguments['icon'], ‪Icon::SIZE_SMALL));
105  }
106 }
‪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:38
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Template\Components\Buttons\AbstractButton
Definition: AbstractButton.php:25
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Template\ModuleTemplate
Definition: ModuleTemplate.php:46
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\initializeArguments
‪initializeArguments()
Definition: AbstractButtonViewHelper.php:45
‪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:52
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\addDefaultAttributes
‪static addDefaultAttributes(AbstractButton $button, array $arguments, RenderingContextInterface $renderingContext)
Definition: AbstractButtonViewHelper.php:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Template\Components\AbstractControl\setTitle
‪$this setTitle($title)
Definition: AbstractControl.php:128
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\ensureProperNesting
‪static ensureProperNesting(ViewHelperVariableContainer $viewHelperVariableContainer)
Definition: AbstractButtonViewHelper.php:90
‪TYPO3\CMS\Backend\ViewHelpers\ModuleLayout\Button\AbstractButtonViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: AbstractButtonViewHelper.php:62
‪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