Skip to content

Commit 7d2cad6

Browse files
committed
separate main script from the main template of category selection
1 parent 834c053 commit 7d2cad6

8 files changed

Lines changed: 326 additions & 278 deletions

File tree

Data/CategoryTreeNode.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ public function toUiArray(): array
3030
);
3131
}
3232
}
33-

Form/Field/FieldType/CategorySelection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function getMaxCategoryLevel(array $categoryTreeNodes): int
3838
return $this->categoryTreeBuilder->calculateMaxLevel($categoryTreeNodes);
3939
}
4040

41+
public function renderMainScript(Field $field): string
42+
{
43+
return $this->categoryTreeRenderer->renderMainScript($field);
44+
}
45+
4146
public function renderChildCategoryNode(CategoryTreeNode $categoryTreeNode, Field $field): string
4247
{
4348
return $this->categoryTreeRenderer->renderChildNode($categoryTreeNode, $field);

Service/CategorySelection/CategoryTreeBuilder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ private function convertArrayToTreeNodes(array $treeData): array
226226
{
227227
return array_map(fn (array $nodeData) =>
228228
new CategoryTreeNode(
229-
id: $nodeData['id'],
230-
label: $nodeData['label'],
231-
isActive: $nodeData['isActive'],
232-
level: $nodeData['level'],
233-
parentId: $nodeData['parentId'],
234-
parentIds: $nodeData['parentIds'],
235-
children: $this->convertArrayToTreeNodes($nodeData['children'] ?? [])
229+
id: $nodeData['id'],
230+
label: $nodeData['label'],
231+
isActive: $nodeData['isActive'],
232+
level: $nodeData['level'],
233+
parentId: $nodeData['parentId'],
234+
parentIds: $nodeData['parentIds'],
235+
children: $this->convertArrayToTreeNodes($nodeData['children'] ?? [])
236236
),
237237
$treeData
238238
);

Service/CategorySelection/CategoryTreeRenderer.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class CategoryTreeRenderer
1313
{
1414
private const TEMPLATE_CHILD_ITEM =
1515
'Loki_AdminComponents::form/field_type/category_selection/select_inner_item.phtml';
16+
private const MAIN_SCRIPT =
17+
'Loki_AdminComponents::form/field_type/category_selection/main_js.phtml';
1618
private const TEMPLATE_BREADCRUMB = 'Loki_AdminComponents::form/field_type/category_selection/crumbs.phtml';
1719
private const TEMPLATE_SEARCH = 'Loki_AdminComponents::form/field_type/category_selection/search_input.phtml';
1820

@@ -21,6 +23,14 @@ public function __construct(
2123
) {
2224
}
2325

26+
public function renderMainScript(Field $field): string
27+
{
28+
return $this->layout->createBlock(Template::class, 'category_selection_main_script')
29+
->setData('field', $field)
30+
->setTemplate($this->getMainScript())
31+
->toHtml();
32+
}
33+
2434
public function renderChildNode(CategoryTreeNode $categoryTreeNode, Field $field): string
2535
{
2636
return $this->layout->createBlock(Template::class, 'category_selection_inner_item_' . $categoryTreeNode->id)
@@ -44,18 +54,35 @@ public function renderSearchInput(): string
4454
->toHtml();
4555
}
4656

47-
private function getChildItemTemplate(): string
57+
/**
58+
* Make it public to provide facility to overwrite template.
59+
*/
60+
public function getChildItemTemplate(): string
4861
{
4962
return self::TEMPLATE_CHILD_ITEM;
5063
}
5164

52-
private function getBreadcrumbsTemplate(): string
65+
/**
66+
* Make it public to provide facility to overwrite template.
67+
*/
68+
public function getBreadcrumbsTemplate(): string
5369
{
5470
return self::TEMPLATE_BREADCRUMB;
5571
}
5672

57-
private function getSearchInputTemplate(): string
73+
/**
74+
* Make it public to provide facility to overwrite template.
75+
*/
76+
public function getSearchInputTemplate(): string
5877
{
5978
return self::TEMPLATE_SEARCH;
6079
}
80+
81+
/**
82+
* Make it public to provide facility to overwrite template.
83+
*/
84+
public function getMainScript(): string
85+
{
86+
return self::MAIN_SCRIPT;
87+
}
6188
}

0 commit comments

Comments
 (0)