Skip to content

Commit a9fce19

Browse files
committed
wip: New form builder
1 parent f43a5e5 commit a9fce19

9 files changed

Lines changed: 124 additions & 18 deletions

File tree

Component/Form/FormViewModel.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ public function getJsData(): array
5151

5252
private function getItemData(): array
5353
{
54-
$item = $this->getRepository()->getItem();
54+
try {
55+
$item = $this->getRepository()->getItem();
56+
} catch(\Throwable $e) {
57+
$this->getContext()->getMessageManager()->addErrorMessage($e->getMessage());
58+
return [];
59+
}
60+
5561
if (false === $item instanceof DataObject) {
5662
return [];
5763
}

Component/Grid/GridViewModel.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,13 @@ public function getItems(): array
9393
// @todo: Move this to child-class EntitySelectViewModel
9494
public function getCurrentItem(int|string $currentId)
9595
{
96-
return $this->getRepository()->getProviderHandler()->getItem($this->getRepository()->getProvider(), $currentId);
96+
try {
97+
return $this->getRepository()->getProviderHandler()->getItem($this->getRepository()->getProvider(), $currentId);
98+
} catch(\Throwable $e) {
99+
$this->getContext()->getMessageManager()->addErrorMessage($e->getMessage());
100+
}
101+
102+
return null;
97103
}
98104

99105
public function applyStaticFilters(): void

Form/Fieldset/Fieldset.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ public function hasFields(): bool
3939
return !empty($this->fields);
4040
}
4141

42-
public function addFields(array $fields): void
42+
public function addFields(array $fields): Fieldset
4343
{
4444
foreach ($fields as $field) {
4545
$this->addField($field);
4646
}
47+
48+
return $this;
4749
}
4850

49-
public function addField(Field $field): void
51+
public function addField(Field $field): Fieldset
5052
{
5153
$this->fields[] = $field;
54+
return $this;
5255
}
5356
}

Form/Form.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,57 @@
33

44
namespace Loki\AdminComponents\Form;
55

6+
use Loki\AdminComponents\Form\Field\Field;
7+
use Loki\AdminComponents\Form\Fieldset\Fieldset;
8+
use Loki\AdminComponents\Ui\Button;
9+
610
class Form
711
{
8-
public function getButtons(): array
12+
/**
13+
* @var Field[]
14+
*/
15+
private array $fields = [];
16+
17+
/**
18+
* @var Fieldset[]
19+
*/
20+
private array $fieldsets = [];
21+
22+
/**
23+
* @var Button[]
24+
*/
25+
private array $buttons = [];
26+
27+
public function addField(Field $field): Form
928
{
10-
return [];
29+
$this->fields[] = $field;
30+
return $this;
1131
}
1232

1333
public function getFields(): array
1434
{
15-
return [];
35+
return $this->fields;
36+
}
37+
38+
public function addFieldset(Fieldset $fieldset): Form
39+
{
40+
$this->fieldsets[] = $fieldset;
41+
return $this;
42+
}
43+
44+
public function getFieldsets(): array
45+
{
46+
return $this->fieldsets;
47+
}
48+
49+
public function addButton(Button $button): Form
50+
{
51+
$this->buttons[] = $button;
52+
return $this;
53+
}
54+
55+
public function getButtons(): array
56+
{
57+
return $this->buttons;
1658
}
1759
}

Form/FormBuilder.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44

55
use Loki\AdminComponents\Form\Field\Field;
66
use Loki\AdminComponents\Form\Field\FieldFactory;
7+
use Loki\AdminComponents\Form\Fieldset\Fieldset;
8+
use Loki\AdminComponents\Form\Fieldset\FieldsetFactory;
9+
use Loki\AdminComponents\Ui\Button;
10+
use Loki\AdminComponents\Ui\ButtonFactory;
711
use Magento\Framework\View\Element\AbstractBlock;
12+
use Magento\Framework\View\LayoutInterface;
813

914
class FormBuilder
1015
{
1116
public function __construct(
1217
private FormFactory $formFactory,
13-
private FieldFactory $fieldFactory
18+
private FieldFactory $fieldFactory,
19+
private FieldsetFactory $fieldsetFactory,
20+
private ButtonFactory $buttonFactory,
21+
private LayoutInterface $layout
1422
) {
1523
}
1624

@@ -19,8 +27,28 @@ public function createForm(string $code): Form
1927
return $this->formFactory->create($code);
2028
}
2129

22-
public function createField(AbstractBlock $block, array $data = []): Field
30+
public function createField(array $data = []): Field
2331
{
32+
$block = $this->layout->createBlock(AbstractBlock::class);
2433
return $this->fieldFactory->create($block, $data);
2534
}
35+
36+
public function createFieldset(string $code, string $label = '', array $fields = []): Fieldset
37+
{
38+
return $this->fieldsetFactory->create($code, $label, $fields);
39+
}
40+
41+
public function createButton(
42+
string $method,
43+
string $label,
44+
string $cssClass = '',
45+
string $url = '',
46+
array $subButtons = [],
47+
bool $primary = false,
48+
): Button
49+
{
50+
return $this->buttonFactory->create(
51+
$method, $label, $cssClass, $url, $subButtons, $primary
52+
);
53+
}
2654
}

Provider/FormProviderInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Loki\AdminComponents\Provider;
5+
6+
use Loki\AdminComponents\Form\Form;
7+
use Loki\AdminComponents\Grid\Column\Column;
8+
9+
interface FormProviderInterface
10+
{
11+
/**
12+
* @return Column[]
13+
*/
14+
public function getForm(): Form;
15+
public function getData(): array;
16+
}

ProviderHandler/RepositoryHandler.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Api\SearchCriteriaBuilder;
1414
use Magento\Framework\Api\SortOrder;
1515
use Magento\Framework\Api\SortOrderFactory;
16+
use Magento\Framework\Exception\NoSuchEntityException;
1617
use Magento\Framework\Message\Manager as MessageManager;
1718
use Magento\Framework\Model\ResourceModel\Db\AbstractDb as AbstractResourceModel;
1819
use Magento\Framework\DataObject;
@@ -65,12 +66,16 @@ public function getItem(object $provider, int|string $identifier): DataObject
6566
throw new RuntimeException('Empty identifier');
6667
}
6768

68-
if (method_exists($provider, 'getById')) {
69-
return call_user_func([$provider, 'getById'], $identifier);
70-
}
71-
72-
if (method_exists($provider, 'get')) {
73-
return call_user_func([$provider, 'get'], $identifier);
69+
try {
70+
if (method_exists($provider, 'getById')) {
71+
return call_user_func([$provider, 'getById'], $identifier);
72+
}
73+
74+
if (method_exists($provider, 'get')) {
75+
return call_user_func([$provider, 'get'], $identifier);
76+
}
77+
} catch (NoSuchEntityException $e) {
78+
throw new RuntimeException('Repository call failed for identifier "'.$identifier.'": '.$e->getMessage());
7479
}
7580

7681
throw new RuntimeException('Repository has no load-method we know of.');

ViewModel/Form/Field/EntitySelect.php

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

33
namespace Loki\AdminComponents\ViewModel\Form\Field;
44

5+
use Loki\AdminComponents\Component\Grid\GridContext;
56
use Loki\AdminComponents\Component\Grid\GridRepository;
67
use Loki\AdminComponents\Component\Grid\GridViewModel;
78
use Loki\AdminComponents\Form\Field\Field;
@@ -99,7 +100,7 @@ private function getGridViewModel(): GridViewModel
99100
'name' => $block->getNameInLayout(),
100101
'viewModelClass' => GridViewModel::class,
101102
'repositoryClass' => GridRepository::class,
102-
'context' => ObjectManager::getInstance()->create(ComponentContext::class),
103+
'context' => ObjectManager::getInstance()->create(GridContext::class),
103104
]);
104105

105106
/** @var GridViewModel $gridViewModel */

etc/loki_components.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
<component
1818
name="loki_admin_components.entity_select"
19-
viewModel="Loki\AdminComponents\Component\Grid\GridViewModel"
20-
repository="Loki\AdminComponents\Component\Grid\GridRepository"
19+
group="loki_admin.grid"
2120
/>
2221
</components>

0 commit comments

Comments
 (0)