Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com_ajax Stringable interface #274

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions migrations/51-52/new-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,49 @@ 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, with help of `Joomla\CMS\String\StringableInterface`.

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 = '';

public function __toString(): string
{
return json_encode($this);
}
};

$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;
}
```