*/ 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).'"'; } }