Skip to content

Commit 08fa3b9

Browse files
authored
[5.2] Add languages API endpoint (#42136)
* GET languages * correct fix * cs * Update api/components/com_installer/src/View/Languages/JsonapiView.php Co-authored-by: Quy <[email protected]> * __DEPLOY_VERSION__ * __DEPLOY_VERSION__ * 2023 * cs * cs
1 parent e16a78f commit 08fa3b9

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

administrator/components/com_installer/src/Model/LanguagesModel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected function getLanguages()
155155
}
156156

157157
$languages = [];
158-
$search = strtolower($this->getState('filter.search'));
158+
$search = strtolower($this->getState('filter.search', ''));
159159

160160
foreach ($updateSiteXML->extension as $extension) {
161161
$language = new \stdClass();
@@ -183,9 +183,9 @@ protected function getLanguages()
183183
usort(
184184
$languages,
185185
function ($a, $b) use ($that) {
186-
$ordering = $that->getState('list.ordering');
186+
$ordering = $that->getState('list.ordering', 'name');
187187

188-
if (strtolower($that->getState('list.direction')) === 'asc') {
188+
if (strtolower($that->getState('list.direction', 'asc')) === 'asc') {
189189
return StringHelper::strcmp($a->$ordering, $b->$ordering);
190190
}
191191

@@ -195,9 +195,9 @@ function ($a, $b) use ($that) {
195195

196196
// Count the non-paginated list
197197
$this->languageCount = \count($languages);
198-
$limit = ($this->getState('list.limit') > 0) ? $this->getState('list.limit') : $this->languageCount;
198+
$limit = ($this->getState('list.limit', 20) > 0) ? $this->getState('list.limit', 20) : $this->languageCount;
199199

200-
return \array_slice($languages, $this->getStart(), $limit);
200+
return \array_slice($languages, $this->getStart() ?? 0, $limit);
201201
}
202202

203203
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.API
5+
* @subpackage com_installer
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Component\Installer\Api\Controller;
12+
13+
use Joomla\CMS\MVC\Controller\ApiController;
14+
15+
// phpcs:disable PSR1.Files.SideEffects
16+
\defined('_JEXEC') or die;
17+
// phpcs:enable PSR1.Files.SideEffects
18+
19+
/**
20+
* The manage controller
21+
*
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
class LanguagesController extends ApiController
25+
{
26+
/**
27+
* The content type of the item.
28+
*
29+
* @var string
30+
* @since __DEPLOY_VERSION__
31+
*/
32+
protected $contentType = 'languages';
33+
34+
/**
35+
* The default view for the display method.
36+
*
37+
* @var string
38+
* @since __DEPLOY_VERSION__
39+
*/
40+
protected $default_view = 'languages';
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.API
5+
* @subpackage com_installer
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Component\Installer\Api\View\Languages;
12+
13+
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
14+
15+
// phpcs:disable PSR1.Files.SideEffects
16+
\defined('_JEXEC') or die;
17+
// phpcs:enable PSR1.Files.SideEffects
18+
19+
/**
20+
* The languages view
21+
*
22+
* @since __DEPLOY_VERSION__
23+
*/
24+
class JsonapiView extends BaseApiView
25+
{
26+
/**
27+
* The fields to render item in the documents
28+
*
29+
* @var array
30+
* @since __DEPLOY_VERSION__
31+
*/
32+
protected $fieldsToRenderList = [
33+
'name',
34+
'element',
35+
'version',
36+
'type',
37+
'detailsurl',
38+
];
39+
40+
protected $i = 0;
41+
42+
/**
43+
* Prepare item before render.
44+
*
45+
* @param object $item The model item
46+
*
47+
* @return object
48+
*
49+
* @since __DEPLOY_VERSION__
50+
*/
51+
protected function prepareItem($item)
52+
{
53+
$item->id = ++$this->i;
54+
55+
return parent::prepareItem($item);
56+
}
57+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
describe('Test that languages API endpoint', () => {
2+
it('can deliver a list of languages', () => {
3+
cy.api_get('/languages')
4+
.then((response) => cy.wrap(response).its('body').its('data.0').its('type')
5+
.should('include', 'languages'));
6+
});
7+
});

0 commit comments

Comments
 (0)