‪TYPO3CMS  ‪main
AttributesViewHelper.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 
22 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
25 
31 final class ‪AttributesViewHelper extends AbstractViewHelper
32 {
33  use CompileWithRenderStatic;
34 
35  public function ‪initializeArguments(): void
36  {
37  $this->registerArgument('class', ToolbarItemInterface::class, 'Class being converted to a string for usage as id attribute', true);
38  }
39 
43  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
44  {
45  $additionalAttributes = [
46  'class' => 'toolbar-item',
47  ];
48 
49  $toolbarItem = $arguments['class'] ?? null;
50  if ($toolbarItem instanceof ‪ToolbarItemInterface) {
51  $additionalAttributes['class'] .= ' ' . ($toolbarItem->getAdditionalAttributes()['class'] ?? '');
52  $additionalAttributes['id'] = ‪self::convertClassNameToIdAttribute(\get_class($toolbarItem));
53  }
54 
55  return GeneralUtility::implodeAttributes($additionalAttributes);
56  }
57 
58  private static function ‪convertClassNameToIdAttribute(string $fullyQualifiedClassName): string
59  {
60  $className = ‪GeneralUtility::underscoredToLowerCamelCase($fullyQualifiedClassName);
62 
63  return str_replace(['_', '\\'], '-', $className);
64  }
65 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\underscoredToLowerCamelCase
‪static string underscoredToLowerCamelCase($string)
Definition: GeneralUtility.php:671
‪TYPO3\CMS\Backend\Toolbar\ToolbarItemInterface
Definition: ToolbarItemInterface.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility\camelCaseToLowerCaseUnderscored
‪static string camelCaseToLowerCaseUnderscored($string)
Definition: GeneralUtility.php:683
‪TYPO3\CMS\Backend\ViewHelpers\Toolbar\AttributesViewHelper
Definition: AttributesViewHelper.php:32
‪TYPO3\CMS\Backend\ViewHelpers\Toolbar\AttributesViewHelper\initializeArguments
‪initializeArguments()
Definition: AttributesViewHelper.php:34
‪TYPO3\CMS\Backend\ViewHelpers\Toolbar\AttributesViewHelper\convertClassNameToIdAttribute
‪static convertClassNameToIdAttribute(string $fullyQualifiedClassName)
Definition: AttributesViewHelper.php:57
‪TYPO3\CMS\Backend\ViewHelpers\Toolbar
Definition: AttributesViewHelper.php:18
‪TYPO3\CMS\Backend\ViewHelpers\Toolbar\AttributesViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: AttributesViewHelper.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52