Skip to content

Commit

Permalink
Module list now includes module disk AND installed versions thanks to…
Browse files Browse the repository at this point in the history
… a custom ModuleGridDataFactory
  • Loading branch information
jolelievre committed Nov 5, 2024
1 parent 9336567 commit d294398
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/admin/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
- 'hook'

prestashop.core.grid.data_factory.module:
class: '%prestashop.core.grid.data.factory.doctrine_grid_data_factory%'
class: PrestaShop\Module\APIResources\List\ModuleGridDataFactory
public: true
arguments:
- '@PrestaShop\Module\APIResources\List\ModuleQueryBuilder'
Expand Down
65 changes: 65 additions & 0 deletions src/List/ModuleGridDataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\Module\APIResources\List;

use PrestaShop\PrestaShop\Core\Grid\Data\Factory\DoctrineGridDataFactory;
use PrestaShop\PrestaShop\Core\Grid\Data\GridData;
use PrestaShop\PrestaShop\Core\Grid\Query\DoctrineQueryBuilderInterface;
use PrestaShop\PrestaShop\Core\Grid\Query\QueryParserInterface;
use PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection;
use PrestaShop\PrestaShop\Core\Grid\Search\SearchCriteriaInterface;
use PrestaShop\PrestaShop\Core\Hook\HookDispatcherInterface;
use PrestaShop\PrestaShop\Core\Module\ModuleRepository;

/**
* Custom factory to enrich the data received from database.
*/
class ModuleGridDataFactory extends DoctrineGridDataFactory
{
public function __construct(
DoctrineQueryBuilderInterface $gridQueryBuilder,
HookDispatcherInterface $hookDispatcher,
QueryParserInterface $queryParser,
string $gridId,
protected ModuleRepository $moduleRepository,
) {
parent::__construct($gridQueryBuilder, $hookDispatcher, $queryParser, $gridId);
}

public function getData(SearchCriteriaInterface $searchCriteria)
{
$gridData = parent::getData($searchCriteria);
$newModules = [];
foreach ($gridData->getRecords() as $moduleRecord) {
$module = $this->moduleRepository->getModule($moduleRecord['technicalName']);
$moduleRecord['moduleVersion'] = $module->disk->get('version');
$newModules[] = $moduleRecord;
}

return new GridData(new RecordCollection($newModules), $gridData->getRecordsTotal(), $gridData->getQuery());
}
}
2 changes: 1 addition & 1 deletion src/List/ModuleQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
public function getSearchQueryBuilder(SearchCriteriaInterface $searchCriteria)
{
$builder = $this->getModuleQueryBuilder($searchCriteria)
->select('m.id_module AS moduleId, m.name AS technicalName, m.active AS enabled, m.version');
->select('m.id_module AS moduleId, m.name AS technicalName, m.active AS enabled, m.version AS installedVersion');

$this->searchCriteriaApplicator
->applySorting($searchCriteria, $builder)
Expand Down

0 comments on commit d294398

Please sign in to comment.