From 1ae48f41c369ce758fe9502adebd0e8ece5fe9e4 Mon Sep 17 00:00:00 2001 From: Ben Croker Date: Wed, 21 Aug 2024 13:55:57 +0200 Subject: [PATCH] Allow ID to be overridden by event --- src/services/ComponentsService.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/ComponentsService.php b/src/services/ComponentsService.php index 88263ce..d969dab 100644 --- a/src/services/ComponentsService.php +++ b/src/services/ComponentsService.php @@ -212,10 +212,6 @@ public function create(string $value, array $variables = [], array|string $attri $attributes = ['id' => $attributes]; } - // Allow ID to be overridden, otherwise ensure random ID does not start with a digit (to avoid a JS error) - $id = $attributes['id'] ?? ('component-' . StringHelper::randomString(6)); - $config->id = $id; - $mergedVariables = array_merge( $variables, Sprig::$core->requests->getVariables() @@ -233,6 +229,10 @@ public function create(string $value, array $variables = [], array|string $attri $mergedVariables = $event->variables; $attributes = $event->attributes; + // Fall back to a random ID that does not start with a digit (to avoid a JS error) + $id = $attributes['id'] ?? ('component-' . StringHelper::randomString(6)); + $config->id = $id; + $componentObject = $this->createObject($value, $mergedVariables); if ($componentObject) {