Skip to content

Commit 07e9167

Browse files
authored
Merge pull request LokiExtensions#9 from integer-net/3/select-field-options
LokiExtensions#3 Abstract Field Type class with Select field type options
2 parents c3890e0 + 266333b commit 07e9167

13 files changed

Lines changed: 66 additions & 18 deletions

File tree

Form/Field/Field.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function getAlpineSetter(): string
5858

5959
public function getFieldType(): FieldTypeInterface
6060
{
61-
$fieldType = $this->getData('field_type');
61+
$fieldType = $this->getData('field_type')->setField($this);
62+
6263
if (false === $fieldType instanceof FieldTypeInterface) {
6364
throw new RuntimeException('Field type must be an instance of '.FieldTypeInterface::class);
6465
}

Form/Field/FieldType/Editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
66

7-
class Editor implements FieldTypeInterface
7+
class Editor extends FieldTypeAbstract implements FieldTypeInterface
88
{
99
public function getTemplate(): string
1010
{

Form/Field/FieldType/EntitySelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
77

8-
class EntitySelect implements FieldTypeInterface
8+
class EntitySelect extends FieldTypeAbstract implements FieldTypeInterface
99
{
1010
public function getInputType(): string
1111
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Loki\AdminComponents\Form\Field\FieldType;
6+
7+
use Loki\AdminComponents\Form\Field\Field;
8+
9+
abstract class FieldTypeAbstract
10+
{
11+
protected ?Field $field = null;
12+
13+
public function setField(Field $field): self
14+
{
15+
$this->field = $field;
16+
return $this;
17+
}
18+
19+
public function getField(): ?Field
20+
{
21+
return $this->field;
22+
}
23+
}

Form/Field/FieldType/FromTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
77

8-
class FromTo implements FieldTypeInterface
8+
class FromTo extends FieldTypeAbstract implements FieldTypeInterface
99
{
1010
public function __construct(
1111
private int $maximumLength = 255,

Form/Field/FieldType/Input.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
66

7-
class Input implements FieldTypeInterface
7+
class Input extends FieldTypeAbstract implements FieldTypeInterface
88
{
99
public function getTemplate(): string
1010
{

Form/Field/FieldType/Pagebuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
66

7-
class Pagebuilder implements FieldTypeInterface
7+
class Pagebuilder extends FieldTypeAbstract implements FieldTypeInterface
88
{
99
public function getTemplate(): string
1010
{

Form/Field/FieldType/ProductId.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
77
use Loki\AdminComponents\ViewModel\Options\ProductOptions;
88

9-
class ProductId implements FieldTypeInterface
9+
class ProductId extends FieldTypeAbstract implements FieldTypeInterface
1010
{
1111
public function __construct(
1212
private ProductOptions $productOptions

Form/Field/FieldType/Select.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,41 @@
22

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

5+
use Loki\AdminComponents\Form\Field\Field;
56
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
7+
use Magento\Framework\Data\OptionSourceInterface;
8+
use Magento\Framework\ObjectManagerInterface;
69

7-
class Select implements FieldTypeInterface
10+
class Select extends FieldTypeAbstract implements FieldTypeInterface
811
{
12+
public function __construct(
13+
private readonly ObjectManagerInterface $objectManager,
14+
) {
15+
}
16+
917
public function getTemplate(): string
1018
{
1119
return 'Loki_AdminComponents::form/field_type/select.phtml';
1220
}
21+
22+
public function getOptions(): array
23+
{
24+
if (!$this->field) {
25+
return [];
26+
}
27+
28+
$options = $this->field->getOptions();
29+
30+
if (!$options) {
31+
return [];
32+
}
33+
34+
$options = $this->objectManager->get($options);
35+
36+
if (!$options instanceof OptionSourceInterface) {
37+
return [];
38+
}
39+
40+
return $options->toOptionArray();
41+
}
1342
}

Form/Field/FieldType/Switcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Loki\AdminComponents\Form\Field\FieldTypeInterface;
66

7-
class Switcher implements FieldTypeInterface
7+
class Switcher extends FieldTypeAbstract implements FieldTypeInterface
88
{
99
public function getTemplate(): string
1010
{

0 commit comments

Comments
 (0)