Files
typo3-13/packages/ep_theme/Classes/ViewHelpers/Stimulus/ActionViewHelper.php
T
frommeandClaude Opus 5.5 a46918de9d chore: copy ep_products and ep_theme from EP/website@9593cc8
Copied unchanged from public/typo3conf/ext/ of EP/website at
9593cc8631354f8a363d6609de8b7433f669a9a0 (Git-tracked files only).
History stays in the old repository. Only composer.json files are
added so the extensions install as path packages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-21 14:01:20 +02:00

89 lines
2.9 KiB
PHP

<?php
namespace EP\EpTheme\ViewHelpers\Stimulus;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* This code is adapted from the Symfony WebpackEncoreBundle package.
* (c) Fabien Potencier <[email protected]>
*/
class ActionViewHelper extends AbstractStimulusViewHelper
{
use CompileWithRenderStatic;
protected $escapeOutput = false;
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('controller', 'mixed', 'Controller name(s)', true);
$this->registerArgument('action', 'string', 'Action name', false);
$this->registerArgument('event', 'string', 'Event name', false);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
)
{
$dataOrControllerName = $arguments['controller'];
$actionName = $arguments['action'];
$eventName = $arguments['event'];
if (\is_string($dataOrControllerName)) {
$data = [$dataOrControllerName => null === $eventName ? [[$actionName]] : [[$eventName => $actionName]]];
} else {
if ($actionName || $eventName) {
throw new \InvalidArgumentException('You cannot pass a string to the second or third argument while passing an array to the first argument of stimulus_action(): check the documentation.');
}
$data = $dataOrControllerName;
if (!$data) {
return '';
}
}
$actions = [];
foreach ($data as $controllerName => $controllerActions) {
$controllerName = static::normalizeControllerName($controllerName);
if (\is_string($controllerActions)) {
$controllerActions = [[$controllerActions]];
}
foreach ($controllerActions as $possibleEventName => $controllerAction) {
if (\is_string($possibleEventName) && \is_string($controllerAction)) {
$controllerAction = [$possibleEventName => $controllerAction];
} elseif (\is_string($controllerAction)) {
$controllerAction = [$controllerAction];
}
foreach ($controllerAction as $eventName => $actionName) {
$action = $controllerName.'#'.htmlspecialchars($actionName, ENT_QUOTES);
if (\is_string($eventName)) {
$action = $eventName.'->'.$action;
}
$actions[] = $action;
}
}
}
return 'data-action="'.implode(' ', $actions).'"';
}
}