Skip to content

Commit

Permalink
[5.2] Add languages API endpoint (#42136)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alikon authored Jun 4, 2024
1 parent e16a78f commit 08fa3b9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected function getLanguages()
}

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

foreach ($updateSiteXML->extension as $extension) {
$language = new \stdClass();
Expand Down Expand Up @@ -183,9 +183,9 @@ protected function getLanguages()
usort(
$languages,
function ($a, $b) use ($that) {
$ordering = $that->getState('list.ordering');
$ordering = $that->getState('list.ordering', 'name');

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

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

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @package Joomla.API
* @subpackage com_installer
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Installer\Api\Controller;

use Joomla\CMS\MVC\Controller\ApiController;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* The manage controller
*
* @since __DEPLOY_VERSION__
*/
class LanguagesController extends ApiController
{
/**
* The content type of the item.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $contentType = 'languages';

/**
* The default view for the display method.
*
* @var string
* @since __DEPLOY_VERSION__
*/
protected $default_view = 'languages';
}
57 changes: 57 additions & 0 deletions api/components/com_installer/src/View/Languages/JsonapiView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* @package Joomla.API
* @subpackage com_installer
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Installer\Api\View\Languages;

use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* The languages view
*
* @since __DEPLOY_VERSION__
*/
class JsonapiView extends BaseApiView
{
/**
* The fields to render item in the documents
*
* @var array
* @since __DEPLOY_VERSION__
*/
protected $fieldsToRenderList = [
'name',
'element',
'version',
'type',
'detailsurl',
];

protected $i = 0;

/**
* Prepare item before render.
*
* @param object $item The model item
*
* @return object
*
* @since __DEPLOY_VERSION__
*/
protected function prepareItem($item)
{
$item->id = ++$this->i;

return parent::prepareItem($item);
}
}
7 changes: 7 additions & 0 deletions tests/System/integration/api/com_languages/Languages.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('Test that languages API endpoint', () => {
it('can deliver a list of languages', () => {
cy.api_get('/languages')
.then((response) => cy.wrap(response).its('body').its('data.0').its('type')
.should('include', 'languages'));
});
});

0 comments on commit 08fa3b9

Please sign in to comment.