From a476a2b90a90ea4186a036382011327801dfe23d Mon Sep 17 00:00:00 2001 From: Ben Croker Date: Thu, 8 Aug 2024 13:37:33 +0200 Subject: [PATCH] Add ability to pass any value into `s-method` --- CHANGELOG.md | 1 + src/services/ComponentsService.php | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 183e50c..f3160e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - Added the component configurations to a `Sprig` variable in the browser console when `devMode` is enabled. +- Added the ability to pass any value into `s-method`, including the `put`, `patch` and `delete` verbs. - Added the `sprig.registerJs(js)` function. ### Changed diff --git a/src/services/ComponentsService.php b/src/services/ComponentsService.php index d5f2347..1e38387 100644 --- a/src/services/ComponentsService.php +++ b/src/services/ComponentsService.php @@ -426,12 +426,10 @@ private function parseAttributes(array &$attributes): void */ private function parseSprigAttribute(array &$attributes): void { - $verb = 'get'; $params = []; + $method = strtolower($this->getSprigAttributeValue($attributes, 'method', 'get')); - $method = $this->getSprigAttributeValue($attributes, 'method'); - if (strtolower($method) == 'post') { - $verb = 'post'; + if ($method === 'post') { $this->mergeJsonAttributes($attributes, 'headers', [ Request::CSRF_HEADER => Craft::$app->getRequest()->getCsrfToken(), ]); @@ -444,7 +442,7 @@ private function parseSprigAttribute(array &$attributes): void ]); } - $attributes[self::HTMX_PREFIX . $verb] = $this->getSprigActionUrl($params); + $attributes[self::HTMX_PREFIX . $method] = $this->getSprigActionUrl($params); } /** @@ -566,7 +564,7 @@ private function getSprigAttributeName(string $key): string /** * Returns a Sprig attribute value if it exists. */ - private function getSprigAttributeValue(array $attributes, string $name): string + private function getSprigAttributeValue(array $attributes, string $name, string $default = ''): string { foreach (self::SPRIG_PREFIXES as $prefix) { if (!empty($attributes[$prefix . $name])) { @@ -578,7 +576,7 @@ private function getSprigAttributeValue(array $attributes, string $name): string } } - return ''; + return $default; } /**