Skip to content

Commit 14baa51

Browse files
committed
Finalize entity_select, add type product_select and customer_select
1 parent add1291 commit 14baa51

13 files changed

Lines changed: 157 additions & 24 deletions

File tree

Component/Grid/GridRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public function getProvider()
7171
return $provider;
7272
}
7373

74+
if (empty($provider)) {
75+
throw new RuntimeException('No provider for block "'.$this->getBlock()->getNameInLayout().'"');
76+
}
77+
7478
$provider = $this->objectManager->get($provider);
7579

7680
if (!empty($provider)) {

Component/Grid/GridViewModel.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ public function getColumnPositions(): array
179179
*/
180180
public function getAvailableColumns(): array
181181
{
182-
$columns = $this->columnLoader->getColumns($this->getNamespace());
182+
$columns = $this->columnLoader->getColumnsFromNamespace($this->getNamespace());
183183
if (!empty($columns)) {
184184
return $columns;
185185
}
186186

187-
$columns = $this->getRepository()->getProviderHandler()->getColumns($this->getRepository()->getProvider());
187+
$providerHandler = $this->getRepository()->getProviderHandler();
188+
$provider = $this->getRepository()->getProvider();
189+
$columns = $providerHandler->getColumns($provider);
188190
if (!empty($columns)) {
189191
return $columns;
190192
}
@@ -214,8 +216,8 @@ public function getAvailableColumns(): array
214216
public function getColumns(): array
215217
{
216218
$columns = $this->getAvailableColumns();
217-
218219
$columnsFromBlock = $this->columnLoader->getColumnsFromBlock($this->block);
220+
219221
if (!empty($columnsFromBlock)) {
220222
foreach ($columnsFromBlock as $columnFromBlock) {
221223
foreach ($columns as $column) {
@@ -231,6 +233,13 @@ public function getColumns(): array
231233
return $this->columnLoader->sortColumns($columns);
232234
}
233235

236+
public function getVisibleColumns(): array
237+
{
238+
return array_filter($this->getColumns(), function(Column $column) {
239+
return $column->isVisible();
240+
});
241+
}
242+
234243
public function getIndexUrl(): string
235244
{
236245
$indexUrl = $this->getBlock()->getData('index_url');

Form/Field/Field.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Loki\AdminComponents\Form\Field\FieldType\Editor;
77
use Magento\Framework\DataObject;
88
use Magento\Framework\View\Element\AbstractBlock;
9+
use RuntimeException;
910

1011
class Field extends DataObject
1112
{
@@ -57,7 +58,16 @@ public function getAlpineSetter(): string
5758

5859
public function getFieldType(): FieldTypeInterface
5960
{
60-
return $this->getData('field_type');
61+
$fieldType = $this->getData('field_type');
62+
if (false === $fieldType instanceof FieldTypeInterface) {
63+
throw new RuntimeException('Field type must be an instance of '.FieldTypeInterface::class);
64+
}
65+
66+
if (method_exists($fieldType, 'prepareField')) {
67+
$fieldType->prepareField($this);
68+
}
69+
70+
return $fieldType;
6171
}
6272

6373
public function getBlock(): AbstractBlock
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Loki\AdminComponents\Form\Field\FieldType;
5+
6+
use Loki\AdminComponents\Form\Field\Field;
7+
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
8+
use Magento\Customer\Model\ResourceModel\Customer\Collection;
9+
10+
class CustomerSelect implements FieldTypeInterface
11+
{
12+
public function getInputType(): string
13+
{
14+
return 'text';
15+
}
16+
17+
public function prepareField(Field $field): Field
18+
{
19+
$field->setProvider(Collection::class);
20+
$field->setButtonLabel('Select customer');
21+
$field->setNamespace('customer_listing');
22+
return $field;
23+
}
24+
25+
public function getTemplate(): string
26+
{
27+
return 'Loki_AdminComponents::form/field_type/entity_select.phtml';
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Loki\AdminComponents\Form\Field\FieldType;
5+
6+
use Loki\AdminComponents\Form\Field\Field;
7+
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
8+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
9+
10+
class ProductSelect implements FieldTypeInterface
11+
{
12+
public function getInputType(): string
13+
{
14+
return 'text';
15+
}
16+
17+
public function prepareField(Field $field): Field
18+
{
19+
$field->setProvider(Collection::class);
20+
$field->setButtonLabel('Select product');
21+
$field->setNamespace('product_listing');
22+
return $field;
23+
}
24+
25+
public function getTemplate(): string
26+
{
27+
return 'Loki_AdminComponents::form/field_type/entity_select.phtml';
28+
}
29+
}

Grid/Column/Column.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Loki\AdminComponents\Grid\Column;
44

5+
use JsonSerializable;
56
use Magento\Framework\DataObject;
67

7-
class Column extends DataObject implements \JsonSerializable
8+
class Column extends DataObject implements JsonSerializable
89
{
910
public function getLabel(): string
1011
{
@@ -41,6 +42,11 @@ public function getPosition(): int
4142
return (int)$this->getData('position');
4243
}
4344

45+
public function isVisible(): bool
46+
{
47+
return (bool)$this->getData('visible');
48+
}
49+
4450
public function __toString(): string
4551
{
4652
return $this->getCode();

Grid/ColumnLoader.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getColumnsFromBlock(AbstractBlock $block): array
4242
*
4343
* @return Column[]
4444
*/
45-
public function getColumns(string $namespace): array
45+
public function getColumnsFromNamespace(string $namespace): array
4646
{
4747
try {
4848
$bookmark = $this->bookmarkLoader->getBookmark($namespace);
@@ -52,8 +52,11 @@ public function getColumns(string $namespace): array
5252
return [];
5353
}
5454

55-
if (isset($bookmarkData['views'][$bookmarkIdentifier]['data']['paging'])) {
56-
$paging = $bookmarkData['views'][$bookmarkIdentifier]['data']['paging'];
55+
$currentBookmark = $bookmarkData['views'][$bookmarkIdentifier];
56+
57+
// @todo: This should not be part of the ColumnLoader at all!
58+
if (isset($currentBookmark['data']['paging'])) {
59+
$paging = $currentBookmark['data']['paging'];
5760
$gridState = $this->stateManager->get($namespace);
5861

5962
if (isset($paging['pageSize'])) {
@@ -65,14 +68,15 @@ public function getColumns(string $namespace): array
6568
}
6669
}
6770

68-
if (false === isset($bookmarkData['views'][$bookmarkIdentifier]['data']['columns'])) {
71+
if (false === isset($currentBookmark['data']['columns'])) {
6972
return [];
7073
}
7174

72-
$positions = $bookmarkData['views'][$bookmarkIdentifier]['data']['positions'];
75+
$positions = $currentBookmark['data']['positions'];
76+
7377

7478
$columns = [];
75-
foreach ($bookmarkData['views'][$bookmarkIdentifier]['data']['columns'] as $columnName => $columnData) {
79+
foreach ($currentBookmark['data']['columns'] as $columnName => $columnData) {
7680
if ($columnName === 'actions') {
7781
continue;
7882
}

ProviderHandler/CollectionHandler.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Loki\AdminComponents\ProviderHandler;
55

6+
use Loki\AdminComponents\Grid\Column\ColumnFactory;
7+
use Loki\AdminComponents\Util\AbstractEntityColumnLoader;
8+
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
69
use Magento\Framework\Data\Collection\AbstractDb;
710
use Magento\Framework\DataObject;
811
use Magento\Framework\Model\AbstractModel;
@@ -14,7 +17,8 @@
1417
class CollectionHandler implements ProviderHandlerInterface
1518
{
1619
public function __construct(
17-
private ObjectManagerInterface $objectManager
20+
private ObjectManagerInterface $objectManager,
21+
private AbstractEntityColumnLoader $abstractEntityColumnLoader
1822
) {
1923
}
2024

@@ -123,8 +127,14 @@ public function duplicateItem(object $provider, DataObject $item)
123127

124128
public function getColumns(object $provider): array
125129
{
126-
/** @var AbstractDb $provider */
127-
// @todo: Use this to fetch database columns
130+
if (false === $provider instanceof AbstractDb) {
131+
return [];
132+
}
133+
134+
if ($provider instanceof AbstractCollection) {
135+
return $this->abstractEntityColumnLoader->getColumns($provider->getEntity());
136+
}
137+
128138
return [];
129139
}
130140

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\AdminComponents\Util;
4+
5+
use Loki\AdminComponents\Grid\Column\ColumnFactory;
6+
use Magento\Eav\Model\Entity\AbstractEntity;
7+
8+
class AbstractEntityColumnLoader
9+
{
10+
public function __construct(
11+
private ColumnFactory $columnFactory
12+
) {
13+
}
14+
15+
public function getColumns(AbstractEntity $entity): array
16+
{
17+
$entity->loadAllAttributes();
18+
$attributes = $entity->getAttributesByCode();
19+
20+
$columns = [];
21+
foreach ($attributes as $attributeCode => $attribute) {
22+
if (!$attribute->getIsVisibleInGrid()) {
23+
continue;
24+
}
25+
26+
$columns[] = $this->columnFactory->create([
27+
'code' => $attributeCode,
28+
'label' => $attribute->getFrontendLabel(),
29+
]);
30+
}
31+
32+
return $columns;
33+
}
34+
}

ViewModel/Form/Field/EntitySelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getModalTitle(): string
4747

4848
public function getColumns(): array
4949
{
50-
$columns = $this->getGridViewModel()->getColumns();
50+
$columns = $this->getGridViewModel()->getVisibleColumns();
5151
return array_filter($columns, function (Column $column) {
5252
return $column->getCode() !== 'ids';
5353
});

0 commit comments

Comments
 (0)