From 867085afde41a0d7a1734d8d52ff803b18bddfd4 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 16:50:23 +0300 Subject: [PATCH 1/4] com_ajax Stringable interface --- migrations/51-52/new-features.md | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/migrations/51-52/new-features.md b/migrations/51-52/new-features.md index 246c7074..0d3709b1 100644 --- a/migrations/51-52/new-features.md +++ b/migrations/51-52/new-features.md @@ -16,3 +16,44 @@ This new feature adds a "total" counter at the bottom near the pagination in Joo displaying the number of items available after applying filters for easier item management. PR: [43575](https://github.com/joomla/joomla-cms/pull/43575) + +### com_ajax support Stringable result + +Allows to customise the response for com_ajax. + +PR: https://github.com/joomla/joomla-cms/pull/43530 + +Example of Module helper getAjax() method, with `&format=json` request. +**Before:** +```php +function getAjax() { + echo json_encode(['title' => 'Foo', 'text' => 'Bar']); + exit; +} +``` +**After:** +```php +function getAjax() { + $result = new class() implements \Joomla\CMS\String\StringableInterface { + public $title = ''; + public $text = ''; + } + + $result->title = 'Foo'; + $result->text => 'Bar'; + + return $result; +} +``` + +Example of customised `Joomla\CMS\Response\JsonResponse` response: + +```php +function getAjax() { + $data = $this->getData(); //... code to load your data + $result = new class($data, null, false, false) extends \Joomla\CMS\Response\JsonResponse implements \Joomla\CMS\String\StringableInterface {} + + return $result; +} +``` + From 80f4bb4cb25369eee24eca4b046bbc9365dc8444 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 16:51:44 +0300 Subject: [PATCH 2/4] com_ajax Stringable interface --- migrations/51-52/new-features.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/51-52/new-features.md b/migrations/51-52/new-features.md index 0d3709b1..dde5b8bd 100644 --- a/migrations/51-52/new-features.md +++ b/migrations/51-52/new-features.md @@ -19,7 +19,7 @@ PR: [43575](https://github.com/joomla/joomla-cms/pull/43575) ### com_ajax support Stringable result -Allows to customise the response for com_ajax. +Allows to customise the response for com_ajax, with help of `Joomla\CMS\String\StringableInterface`. PR: https://github.com/joomla/joomla-cms/pull/43530 From 828d669656d633f1d856e39b0164189f89f60761 Mon Sep 17 00:00:00 2001 From: Fedik Date: Sat, 15 Jun 2024 16:53:38 +0300 Subject: [PATCH 3/4] com_ajax Stringable interface --- migrations/51-52/new-features.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/migrations/51-52/new-features.md b/migrations/51-52/new-features.md index dde5b8bd..d9a69ae1 100644 --- a/migrations/51-52/new-features.md +++ b/migrations/51-52/new-features.md @@ -37,10 +37,10 @@ function getAjax() { $result = new class() implements \Joomla\CMS\String\StringableInterface { public $title = ''; public $text = ''; - } + }; $result->title = 'Foo'; - $result->text => 'Bar'; + $result->text = 'Bar'; return $result; } @@ -51,7 +51,7 @@ Example of customised `Joomla\CMS\Response\JsonResponse` response: ```php function getAjax() { $data = $this->getData(); //... code to load your data - $result = new class($data, null, false, false) extends \Joomla\CMS\Response\JsonResponse implements \Joomla\CMS\String\StringableInterface {} + $result = new class($data, null, false, false) extends \Joomla\CMS\Response\JsonResponse implements \Joomla\CMS\String\StringableInterface {}; return $result; } From 78bfc164c40bca16e6dc69a7bd6c6ce372a6a3be Mon Sep 17 00:00:00 2001 From: Fedik Date: Tue, 18 Jun 2024 18:45:08 +0300 Subject: [PATCH 4/4] com_ajax Stringable interface --- migrations/51-52/new-features.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/migrations/51-52/new-features.md b/migrations/51-52/new-features.md index d9a69ae1..eac7e785 100644 --- a/migrations/51-52/new-features.md +++ b/migrations/51-52/new-features.md @@ -37,6 +37,11 @@ function getAjax() { $result = new class() implements \Joomla\CMS\String\StringableInterface { public $title = ''; public $text = ''; + + public function __toString(): string + { + return json_encode($this); + } }; $result->title = 'Foo';