*/ class TargetViewHelper extends AbstractStimulusViewHelper { use CompileWithRenderStatic; protected $escapeOutput = false; public function initializeArguments() { parent::initializeArguments(); $this->registerArgument('controller', 'mixed', 'Controller name(s)', true); $this->registerArgument('targets', 'string', 'Target names', 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']; $targetNames = $arguments['target']; if (\is_string($dataOrControllerName)) { $data = [$dataOrControllerName => $targetNames]; } else { if ($targetNames) { throw new \InvalidArgumentException('You cannot pass a string to the second argument while passing an array to the first argument of stimulus_target(): check the documentation.'); } $data = $dataOrControllerName; if (!$data) { return ''; } } $targets = []; foreach ($data as $controllerName => $targetNames) { $controllerName = static::normalizeControllerName($controllerName); $targets['data-'.$controllerName.'-target'] = htmlspecialchars($targetNames, ENT_QUOTES); } return implode(' ', array_map(static function (string $attribute, string $value): string { return $attribute.'="'.$value.'"'; }, array_keys($targets), $targets)); } }