Skip to content

Commit 3dbd983

Browse files
authored
Merge pull request #14 from LibreCodeCoop/feat/select-field-type
feat: select field type
2 parents 392364b + df418a9 commit 3dbd983

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1339
-38
lines changed
44.6 KB
Loading

img/screenshots/admin-catalog.png

83.7 KB
Loading
4.91 KB
Loading
7.53 KB
Loading
19 KB
Loading
23.4 KB
Loading
1.29 KB
Loading
-7.04 KB
Loading

lib/Controller/FieldDefinitionApiController.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function index(): DataResponse {
6161
* @param string $initialVisibility Initial visibility applied to new values
6262
* @param int $sortOrder Display order used in admin and profile forms
6363
* @param bool $active Whether the definition is currently active
64+
* @param list<string> $options Allowed values for select fields (ignored for other types)
6465
* @return DataResponse<Http::STATUS_CREATED, ProfileFieldsDefinition, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}>
6566
*
6667
* 201: Field definition created successfully
@@ -77,9 +78,10 @@ public function create(
7778
string $initialVisibility = 'private',
7879
int $sortOrder = 0,
7980
bool $active = true,
81+
array $options = [],
8082
): DataResponse {
8183
try {
82-
$definition = $this->fieldDefinitionService->create([
84+
$payload = [
8385
'field_key' => $fieldKey,
8486
'label' => $label,
8587
'type' => $type,
@@ -89,7 +91,11 @@ public function create(
8991
'initial_visibility' => $initialVisibility,
9092
'sort_order' => $sortOrder,
9193
'active' => $active,
92-
]);
94+
];
95+
if ($options !== []) {
96+
$payload['options'] = $options;
97+
}
98+
$definition = $this->fieldDefinitionService->create($payload);
9399

94100
return new DataResponse($definition->jsonSerialize(), Http::STATUS_CREATED);
95101
} catch (InvalidArgumentException $exception) {
@@ -111,6 +117,7 @@ public function create(
111117
* @param string $initialVisibility Initial visibility applied to new values
112118
* @param int $sortOrder Display order used in admin and profile forms
113119
* @param bool $active Whether the definition is currently active
120+
* @param list<string> $options Allowed values for select fields (ignored for other types)
114121
* @return DataResponse<Http::STATUS_OK, ProfileFieldsDefinition, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
115122
*
116123
* 200: Field definition updated successfully
@@ -128,14 +135,15 @@ public function update(
128135
string $initialVisibility = 'private',
129136
int $sortOrder = 0,
130137
bool $active = true,
138+
array $options = [],
131139
): DataResponse {
132140
$existing = $this->fieldDefinitionService->findById($id);
133141
if ($existing === null) {
134142
return new DataResponse(['message' => 'Field definition not found'], Http::STATUS_NOT_FOUND);
135143
}
136144

137145
try {
138-
$definition = $this->fieldDefinitionService->update($existing, [
146+
$payload = [
139147
'label' => $label,
140148
'type' => $type,
141149
'admin_only' => $adminOnly,
@@ -144,7 +152,11 @@ public function update(
144152
'initial_visibility' => $initialVisibility,
145153
'sort_order' => $sortOrder,
146154
'active' => $active,
147-
]);
155+
];
156+
if ($options !== []) {
157+
$payload['options'] = $options;
158+
}
159+
$definition = $this->fieldDefinitionService->update($existing, $payload);
148160

149161
return new DataResponse($definition->jsonSerialize(), Http::STATUS_OK);
150162
} catch (InvalidArgumentException $exception) {

lib/Db/FieldDefinition.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* @method void setSortOrder(int $value)
3333
* @method bool getActive()
3434
* @method void setActive(bool $value)
35+
* @method string|null getOptions()
36+
* @method void setOptions(?string $value)
3537
* @method \DateTimeInterface getCreatedAt()
3638
* @method void setCreatedAt(\DateTimeInterface $value)
3739
* @method \DateTimeInterface getUpdatedAt()
@@ -47,6 +49,7 @@ class FieldDefinition extends Entity {
4749
protected $initialVisibility;
4850
protected $sortOrder;
4951
protected $active;
52+
protected $options;
5053
protected $createdAt;
5154
protected $updatedAt;
5255

@@ -73,11 +76,14 @@ public function __construct() {
7376
* initial_visibility: string,
7477
* sort_order: int,
7578
* active: bool,
79+
* options: list<string>|null,
7680
* created_at: string,
7781
* updated_at: string,
7882
* }
7983
*/
8084
public function jsonSerialize(): array {
85+
$rawOptions = $this->getOptions();
86+
8187
return [
8288
'id' => $this->getId(),
8389
'field_key' => $this->getFieldKey(),
@@ -89,6 +95,7 @@ public function jsonSerialize(): array {
8995
'initial_visibility' => $this->getInitialVisibility(),
9096
'sort_order' => $this->getSortOrder(),
9197
'active' => $this->getActive(),
98+
'options' => $rawOptions !== null ? (json_decode($rawOptions, true) ?? null) : null,
9299
'created_at' => $this->getCreatedAt()->format(DATE_ATOM),
93100
'updated_at' => $this->getUpdatedAt()->format(DATE_ATOM),
94101
];

0 commit comments

Comments
 (0)