Skip to content

Commit

Permalink
Add collection grid source type processor
Browse files Browse the repository at this point in the history
Add collection specific source type processor to allow joining fields
before the available grid columns are extracted.

Fixes issue hyva-themes#45.
  • Loading branch information
Vinai committed May 14, 2021
1 parent bd5a2b4 commit 318c93d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
23 changes: 23 additions & 0 deletions Api/HyvaGridCollectionProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Hyva\Admin\Api;

use Magento\Framework\Data\Collection\AbstractDb as AbstractDbCollection;

/**
* Collection grid source type specific processor interface.
*/
interface HyvaGridCollectionProcessorInterface extends HyvaGridSourceProcessorInterface
{
/**
* This interface provides an afterInitSelect() callback that is only applicable to collection grid sources.
*
* This callback is triggered every time the collection grid source is instantiated, before the search
* criteria is applied. It is intended to allow joining additional fields that will then be available
* as grid columns.
*
* @param AbstractDbCollection $source
* @param string $gridName
*/
public function afterInitSelect(AbstractDbCollection $source, string $gridName): void;
}
2 changes: 1 addition & 1 deletion Api/HyvaGridSourceProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function beforeLoad($source, SearchCriteriaInterface $searchCriteria, str
* The method must return the new result or null. The $result type depends on the grid configuration.
* If null is returned, the result value from before afterLoad is used.
*
* Do not mutate $searchCriteria since that will cause multiple loads (because )it's signature change,
* Do not mutate $searchCriteria since that will cause multiple loads (because its signature changes,
* see note on beforeLoad above).
*
* @param mixed $rawResult
Expand Down
11 changes: 10 additions & 1 deletion Model/GridSourceType/CollectionGridSourceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function array_unique as unique;
use function array_values as values;

use Hyva\Admin\Api\HyvaGridCollectionProcessorInterface;
use Hyva\Admin\Api\HyvaGridSourceProcessorInterface;
use Hyva\Admin\Model\GridSourceType\CollectionSourceType\GridSourceCollectionFactory;
use Hyva\Admin\Model\GridTypeReflection;
Expand Down Expand Up @@ -211,7 +212,15 @@ public function extractTotalRowCount(RawGridSourceContainer $rawGridData): int

private function getCollectionInstance(): AbstractDb
{
return $this->gridSourceCollectionFactory->create($this->getCollectionConfig());
return reduce(
$this->processors,
function (AbstractDb $collection, HyvaGridSourceProcessorInterface $processor): AbstractDb {
if ($processor instanceof HyvaGridCollectionProcessorInterface) {
$processor->afterInitSelect($collection, $this->gridName);
}
return $collection;
},
$this->gridSourceCollectionFactory->create($this->getCollectionConfig()));
}

private function isTypeReflectionField(string $key): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

namespace Hyva\Admin\Test\Integration\Model\GridSourceType;

use Hyva\Admin\Api\HyvaGridCollectionProcessorInterface;
use Hyva\Admin\Api\HyvaGridSourceProcessorInterface;
use Hyva\Admin\Model\DataType\ArrayDataType;
use Hyva\Admin\Model\DataType\BooleanDataType;
use Hyva\Admin\Model\DataType\IntDataType;
use Hyva\Admin\Model\DataType\TextDataType;
use Hyva\Admin\Model\GridSource\AbstractGridSourceProcessor;
use Hyva\Admin\Model\GridSourceType\CollectionGridSourceType;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
use Magento\Cms\Model\ResourceModel\Page\Collection as CmsPageCollection;
use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Data\Collection\AbstractDb as AbstractDbCollection;
use Magento\Framework\DB\Select;
use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OrderGridCollection;
use Magento\Sales\Setup\SalesSetup;
Expand Down Expand Up @@ -288,4 +292,41 @@ public function afterLoad($rawResult, SearchCriteriaInterface $searchCriteria, s
$records = $sut->extractRecords($rawGridData);
$this->assertCount(1, $records);
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Catalog/_files/products_list.php
*/
public function testAfterInitSelectProcessorCanJoinFieldsForGrid(): void
{
$processor = new class() extends AbstractGridSourceProcessor implements HyvaGridCollectionProcessorInterface {

public function afterInitSelect(AbstractDbCollection $source, string $gridName): void
{
$select = $source->getSelect();
// add select expression
$select->columns(['foo' => new \Zend_Db_Expr('foo')]);
// add field from joined table
$source->getSelect()->joinLeft(
'catalog_category_product',
'e.entity_id = catalog_category_product.product_id',
['test_field' => 'catalog_category_product.entity_id']
);
}
};

$args = [
'gridName' => 'test',
'processors' => [$processor],
'sourceConfiguration' => ['collection' => ProductCollection::class],
];
/** @var CollectionGridSourceType $sut */
$sut = ObjectManager::getInstance()->create(CollectionGridSourceType::class, $args);

$columnKeys = $sut->getColumnKeys();
$this->assertContains('foo', $columnKeys); // select expression
$this->assertContains('test_field', $columnKeys); // joined field
$this->assertContains('sku', $columnKeys); // entity table attribute
$this->assertContains('color', $columnKeys); // eav attribute
}
}

0 comments on commit 318c93d

Please sign in to comment.