Skip to content

Commit add1291

Browse files
committed
Exclude ids column
1 parent 5d2bfa4 commit add1291

4 files changed

Lines changed: 84 additions & 33 deletions

File tree

Component/Grid/GridViewModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function getItems(): array
9595
// @todo: Move this to child-class EntitySelectViewModel
9696
public function getCurrentItem(int|string $currentId)
9797
{
98-
return $this->getRepository()->getProviderHandler()->getItem($this->getBlock()->getProvider(), $currentId);
98+
return $this->getRepository()->getProviderHandler()->getItem($this->getRepository()->getProvider(), $currentId);
9999
}
100100

101101
public function applyStaticFilters(): void

ViewModel/Form/Field/EntitySelect.php

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
use Loki\AdminComponents\Component\Grid\GridRepository;
66
use Loki\AdminComponents\Component\Grid\GridViewModel;
77
use Loki\AdminComponents\Form\Field\Field;
8+
use Loki\AdminComponents\Grid\Column\Column;
89
use Loki\Components\Component\Component;
910
use Loki\Components\Component\ComponentContext;
10-
use Magento\Customer\Model\ResourceModel\Customer;
11-
use Magento\Customer\Model\ResourceModel\Customer\Collection;
1211
use Magento\Framework\App\ObjectManager;
1312
use Magento\Framework\View\Element\AbstractBlock;
1413
use Magento\Framework\View\Element\Block\ArgumentInterface;
@@ -36,9 +35,22 @@ public function getButtonLabel(): string
3635
return 'Select entity';
3736
}
3837

38+
public function getModalTitle(): string
39+
{
40+
$modalTitle = $this->getField()->getModalTitle();
41+
if (!empty($modalTitle)) {
42+
return $modalTitle;
43+
}
44+
45+
return $this->getButtonLabel();
46+
}
47+
3948
public function getColumns(): array
4049
{
41-
return $this->getGridViewModel()->getColumns();
50+
$columns = $this->getGridViewModel()->getColumns();
51+
return array_filter($columns, function (Column $column) {
52+
return $column->getCode() !== 'ids';
53+
});
4254
}
4355

4456
public function getCurrentItem()
@@ -48,19 +60,45 @@ public function getCurrentItem()
4860
return $currentItem;
4961
}
5062

63+
public function getJsData(): array
64+
{
65+
return [
66+
'ajaxUrl' => $this->getAjaxUrl(),
67+
'valueCode' => $this->getValueCode(),
68+
];
69+
}
70+
71+
public function getJsonData(): string
72+
{
73+
return json_encode($this->getJsData());
74+
}
75+
76+
private function getNamespace(): string
77+
{
78+
return (string)$this->getField()->getNamespace();
79+
}
80+
5181
private function getGridViewModel(): GridViewModel
5282
{
83+
$gridViewModel = null;
84+
if ($gridViewModel instanceof GridViewModel) {
85+
return $gridViewModel;
86+
}
87+
5388
$block = $this->block->getLayout()->createBlock(Template::class);
89+
$block->setNamespace($this->getNamespace());
5490

55-
/** @var Component $component */
56-
$namespace = $this->getField()->getNamespace();
57-
if ($namespace) {
58-
$block->setNamespace($namespace);
91+
$resourceModel = $this->getField()->getResourceModel();
92+
if ($resourceModel) {
93+
$block->setResourceModel($resourceModel);
5994
}
6095

61-
$block->setResourceModel(Customer::class);
62-
$block->setProvider(ObjectManager::getInstance()->get(Collection::class));
96+
$provider = $this->getField()->getProvider();
97+
if ($provider) {
98+
$block->setProvider($provider);
99+
}
63100

101+
/** @var Component $component */
64102
$component = ObjectManager::getInstance()->create(Component::class, [
65103
'name' => $block->getNameInLayout(),
66104
'viewModelClass' => GridViewModel::class,
@@ -72,4 +110,23 @@ private function getGridViewModel(): GridViewModel
72110
$gridViewModel = $component->getViewModel();
73111
return $gridViewModel;
74112
}
113+
114+
public function getValueCode(): string
115+
{
116+
return $this->getField()->getScope() . '.' . $this->getField()->getCode();
117+
}
118+
119+
public function getAjaxUrl(): string
120+
{
121+
return $this->block->getUrl('mui/index/render') . '?'.http_build_query([
122+
'namespace' => $this->getNamespace(),
123+
//'sorting[field]' => 'entity_id',
124+
//'sorting[direction]' => 'asc',
125+
'keywordUpdated' => false,
126+
'filters[placeholder]' => true,
127+
'paging[pageSize]' => 20,
128+
'paging[current]' => 1,
129+
'isAjax' => 'true',
130+
]);
131+
}
75132
}

view/adminhtml/templates/form/field_type/entity_select.phtml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ $fieldAttributes = $field->getFieldAttributes();
2727
$fieldType = $field->getFieldType();
2828
$inputType = $fieldType->getInputType();
2929
$buttonLabel = $entitySelect->getButtonLabel();
30+
$modalTitle = $entitySelect->getModalTitle();
3031

3132
if (isset($fieldAttributes['type'])) {
3233
$inputType = $fieldAttributes['type'];
@@ -35,7 +36,7 @@ if (isset($fieldAttributes['type'])) {
3536

3637
$columns = $entitySelect->getColumns();
3738
$currentItem = $entitySelect->getCurrentItem();
38-
$valueCode = $field->getScope() . '.' . $field->getCode();
39+
$valueCode = $entitySelect->getValueCode();
3940

4041
// @todo: Modal effect with _show does not animate
4142
?>
@@ -44,6 +45,8 @@ $valueCode = $field->getScope() . '.' . $field->getCode();
4445
@keyup.esc="closeWrapper"
4546
data-value-code="<?= $valueCode ?>"
4647
>
48+
<script x-ref="initialData" type="text/x-loki-init"><?= $entitySelect->getJsonData() ?></script>
49+
4750
<div class="input-with-button">
4851
<input
4952
class="admin__control-text"
@@ -71,7 +74,7 @@ $valueCode = $field->getScope() . '.' . $field->getCode();
7174
<div class="modal-inner-wrap" style="padding:20px;">
7275
<header class="modal-header">
7376
<h1 class="modal-title" data-role="title">
74-
Select an entity
77+
<?= $modalTitle ?>
7578
</h1>
7679
<button
7780
@click="closeWrapper"
@@ -96,8 +99,8 @@ $valueCode = $field->getScope() . '.' . $field->getCode();
9699
aria-label="Search by keyword"
97100
/>
98101

99-
<button class="action-submit" type="button" data-bind="attr: {'aria-label': $t('Search')}, click: apply.bind($data, false)" aria-label="Search">
100-
<span data-bind="i18n: 'Search'">Search</span>
102+
<button class="action-submit" type="button" aria-label="Search">
103+
<span>Search</span>
101104
</button>
102105
</div>
103106
</div>

view/adminhtml/templates/script/component/entity-select-component.phtml

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,21 @@ use Magento\Framework\View\Element\Template;
77
/** @version 0.4.4 */
88
/** @var Template $block */
99
/** @var ViewModelFactory $viewModelFactory */
10-
11-
$ajaxUrl = $block->getUrl('mui/index/render') . '?'.http_build_query([
12-
'namespace' => 'customer_listing',
13-
'sorting[field]' => 'entity_id',
14-
'sorting[direction]' => 'asc',
15-
'keywordUpdated' => false,
16-
'filters[placeholder]' => true,
17-
'paging[pageSize]' => 20,
18-
'paging[current]' => 1,
19-
'isAjax' => 'true',
20-
]);
21-
2210
?>
2311
<script>
2412
document.addEventListener('alpine:init', () => {
2513
Alpine.data('LokiAdminFormEntitySelectComponent', () => ({
14+
...LokiLoadDataComponentPartial,
2615
isModalWrapperVisible: false,
2716
currentEntityCode: '',
2817
currentEntityLabel: '',
2918
totalEntities: 0,
19+
ajaxUrl: '',
3020
valueCode: 'value',
3121
search: '',
3222
entities: [],
3323
init() {
24+
this.loadData();
3425
this.valueCode = this.$root.getAttribute('data-value-code');
3526

3627
if (this.isModalWrapperVisible) {
@@ -51,12 +42,7 @@ $ajaxUrl = $block->getUrl('mui/index/render') . '?'.http_build_query([
5142
return !this.hasEntities();
5243
},
5344
loadEntities() {
54-
let ajaxUrl = this.getAjaxUrl();
55-
if (this.search) {
56-
ajaxUrl += '&search=' + this.search;
57-
}
58-
59-
fetch(ajaxUrl, {
45+
fetch(this.getAjaxUrl(), {
6046
method: 'GET',
6147
headers: {
6248
'Content-Type': 'application/json',
@@ -79,7 +65,12 @@ $ajaxUrl = $block->getUrl('mui/index/render') . '?'.http_build_query([
7965
});
8066
},
8167
getAjaxUrl() {
82-
return '<?= $ajaxUrl ?>';
68+
let ajaxUrl = this.ajaxUrl;
69+
if (this.search) {
70+
ajaxUrl += '&search=' + this.search;
71+
}
72+
73+
return ajaxUrl;
8374
},
8475
showModalWrapper() {
8576
this.loadEntities();

0 commit comments

Comments
 (0)