Skip to content

Commit eca7e82

Browse files
author
antoine
committed
Handle remote autocomplete in embed form
1 parent ddd6b1d commit eca7e82

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

resources/js/form/Form.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ export class Form implements FormData, CommandFormData {
3939

4040
entityKey: string;
4141
instanceId: string | number;
42+
embedKey?: string;
4243

43-
constructor(data: FormData | EmbedFormData, entityKey: string, instanceId: string | number) {
44+
constructor(data: FormData | EmbedFormData, entityKey: string, instanceId: string | number, embedKey?: string) {
4445
Object.assign(this, data);
4546
this.entityKey = entityKey;
4647
this.instanceId = instanceId;
48+
this.embedKey = embedKey;
4749
this.serializedData = this.data;
4850
}
4951

resources/js/form/components/fields/Autocomplete.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
route('code16.sharp.api.form.autocomplete.index', {
5858
entityKey: form.entityKey,
5959
autocompleteFieldKey: field.key,
60+
embedKey: form.embedKey,
6061
endpoint: field.remoteEndpoint,
6162
search: query,
6263
}), {

resources/js/form/components/fields/editor/extensions/embed/EditorEmbedModal.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
modalEmbed.value = {
5252
id,
5353
embed,
54-
form: new Form(embedForm, parentForm.entityKey, parentForm.instanceId),
54+
form: new Form(embedForm, parentForm.entityKey, parentForm.instanceId, embed.key),
5555
}
5656
modalOpen.value = true;
5757
}

src/Http/Controllers/Api/ApiFormAutocompleteController.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44

55
use Code16\Sharp\Exceptions\SharpInvalidConfigException;
66
use Code16\Sharp\Form\Fields\SharpFormAutocompleteRemoteField;
7+
use Code16\Sharp\Http\Controllers\Api\Embeds\HandleEmbed;
78
use Code16\Sharp\Utils\Transformers\ArrayConverter;
89
use Illuminate\Http\Request;
910
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Facades\Route;
1112

1213
class ApiFormAutocompleteController extends ApiController
1314
{
14-
public function index(string $entityKey, string $autocompleteFieldKey)
15+
use HandleEmbed;
16+
17+
public function index(string $entityKey, string $autocompleteFieldKey, ?string $embedKey = null)
1518
{
16-
$entity = $this->entityManager->entityFor($entityKey);
17-
$form = $entity->getFormOrFail(sharp_normalize_entity_key($entityKey)[1]);
18-
$field = $form->findFieldByKey($autocompleteFieldKey);
19+
if($embedKey) {
20+
$formOrEmbed = $this->getEmbedFromKey($embedKey);
21+
} else {
22+
$entity = $this->entityManager->entityFor($entityKey);
23+
$formOrEmbed = $entity->getFormOrFail(sharp_normalize_entity_key($entityKey)[1]);
24+
}
25+
26+
$field = $formOrEmbed->findFieldByKey($autocompleteFieldKey);
1927

2028
if ($field === null) {
2129
throw new SharpInvalidConfigException('Remote autocomplete field '.$autocompleteFieldKey.' was not found in form.');
@@ -26,15 +34,15 @@ public function index(string $entityKey, string $autocompleteFieldKey)
2634
if ($callback = $field->getRemoteCallback()) {
2735
$formData = request()->input('formData')
2836
? collect(request()->input('formData'))
29-
->filter(fn ($value, $key) => in_array($key, $form->getDataKeys()))
30-
->map(function ($value, $key) use ($form) {
31-
if (! $field = $form->findFieldByKey($key)) {
37+
->filter(fn ($value, $key) => in_array($key, $formOrEmbed->getDataKeys()))
38+
->map(function ($value, $key) use ($formOrEmbed) {
39+
if (! $field = $formOrEmbed->findFieldByKey($key)) {
3240
return $value;
3341
}
3442

3543
return $field
3644
->formatter()
37-
->setDataLocalizations($form->getDataLocalizations())
45+
->setDataLocalizations($formOrEmbed->getDataLocalizations())
3846
->fromFront($field, $key, $value);
3947
})
4048
->toArray()

src/routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@
9393
Route::post('/upload', [ApiFormUploadController::class, 'store'])
9494
->name('code16.sharp.api.form.upload');
9595

96-
Route::post('/form/autocomplete/{entityKey}/{autocompleteFieldKey}', [ApiFormAutocompleteController::class, 'index'])
96+
Route::post('/form/autocomplete/{entityKey}/{autocompleteFieldKey}/{embedKey?}', [ApiFormAutocompleteController::class, 'index'])
9797
->name('code16.sharp.api.form.autocomplete.index');
9898
});

0 commit comments

Comments
 (0)