Skip to content

Commit 23d6395

Browse files
committed
wip
1 parent 50e63d5 commit 23d6395

File tree

9 files changed

+199
-89
lines changed

9 files changed

+199
-89
lines changed

src/app/Library/Support/DataformCache.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct()
2424
* @param CrudPanel $crud The CRUD panel instance to update with form_id
2525
* @return bool Whether the operation was successful
2626
*/
27-
public function cacheForComponent(string $formId, string $controllerClass, array $fieldsConfig, ?string $name = null, ?CrudPanel $crud = null): bool
27+
public function cacheForComponent(string $formId, string $controllerClass, array $fieldsConfig, ?CrudPanel $crud = null): bool
2828
{
2929
if (empty($fieldsConfig)) {
3030
return false;
@@ -45,8 +45,7 @@ public function cacheForComponent(string $formId, string $controllerClass, array
4545
$formId,
4646
$controllerClass,
4747
$parentController,
48-
$parentEntry,
49-
$name
48+
$parentEntry
5049
);
5150

5251
Cache::put($this->cachePrefix.$formId.'_fields', $fieldsConfig, now()->addMinutes($this->cacheDuration));
@@ -63,7 +62,6 @@ public static function applyAndStoreSetupClosure(
6362
string $formId,
6463
string $controllerClass,
6564
\Closure $setupClosure,
66-
?string $name = null,
6765
?CrudPanel $crud = null,
6866
$parentEntry = null
6967
): bool {
@@ -77,7 +75,7 @@ public static function applyAndStoreSetupClosure(
7775
}
7876

7977
// Cache the field configuration (not the closure, since it won't persist across requests)
80-
$cached = $instance->cacheForComponent($formId, $controllerClass, $fieldsAfterSetup, $name, $crud);
78+
$cached = $instance->cacheForComponent($formId, $controllerClass, $fieldsAfterSetup, $crud);
8179

8280
return $cached;
8381
}

src/app/View/Components/Dataform.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class Dataform extends Component implements IsolatesOperationSetup
1111
{
1212
public $crud;
13+
1314

1415
/**
1516
* Standalone forms do NOT isolate their operation setup.
@@ -32,10 +33,10 @@ public function shouldIsolateOperationSetup(): bool
3233
public function __construct(
3334
public string $controller,
3435
private string $id = 'backpack-form-',
35-
public string $name = '',
3636
public string $formOperation = 'create',
37+
public ?string $formUrl = null,
3738
public ?string $formAction = null,
38-
public string $formMethod = 'post',
39+
public ?string $formMethod = 'post',
3940
public bool $hasUploadFields = false,
4041
public $entry = null,
4142
public ?Closure $setup = null,
@@ -57,8 +58,10 @@ public function __construct(
5758
$this->formMethod = 'put';
5859
$this->crud->entry = $this->entry;
5960
$this->crud->setOperationSetting('fields', $this->crud->getUpdateFields());
61+
$this->formUrl = url($this->crud->route.'/'.$this->entry->getKey().'/edit');
6062
} else {
6163
$this->formAction = $formAction ?? url($this->crud->route);
64+
$this->formUrl = url($this->crud->route.'/create');
6265
}
6366

6467
$this->hasUploadFields = $this->crud->hasUploadFields($this->formOperation, $this->entry?->getKey());
@@ -104,7 +107,6 @@ public function render()
104107
'crud' => $this->crud,
105108
'saveAction' => $this->crud->getSaveAction(),
106109
'id' => $this->id,
107-
'name' => $this->name,
108110
'formOperation' => $this->formOperation,
109111
'formAction' => $this->formAction,
110112
'formMethod' => $this->formMethod,

src/app/View/Components/DataformModal.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ public function shouldIsolateOperationSetup(): bool
2020

2121
public function __construct(
2222
public string $controller,
23-
public ?string $route = null,
2423
public string $id = 'backpack-form',
2524
public string $formOperation = 'create',
26-
public string $name = '',
27-
public string $formUrl = 'create',
25+
public ?string $formUrl = null,
2826
public ?string $formAction = null,
29-
public string $formMethod = 'post',
27+
public ?string $formMethod = null,
3028
public bool $hasUploadFields = false,
3129
public $entry = null,
3230
public ?Closure $setup = null,
@@ -35,16 +33,9 @@ public function __construct(
3533
public string $classes = 'modal-dialog modal-lg',
3634
public bool $refreshDatatable = false,
3735
) {
38-
// If route is not provided (e.g., when rendering via AJAX), get it from CRUD panel
39-
if ($this->route === null) {
40-
\Backpack\CRUD\CrudManager::setActiveController($controller);
41-
$tempCrud = \Backpack\CRUD\CrudManager::setupCrudPanel($controller, $this->formOperation);
42-
$this->route = $tempCrud->route;
43-
\Backpack\CRUD\CrudManager::unsetActiveController();
44-
}
45-
46-
// keep backwards compatible behavior for resolving route when not provided
47-
$this->formAction = $this->formAction ?? url($this->route);
36+
\Backpack\CRUD\CrudManager::setActiveController($controller);
37+
$this->crud = \Backpack\CRUD\CrudManager::setupCrudPanel($controller, $this->formOperation);
38+
\Backpack\CRUD\CrudManager::unsetActiveController();
4839
}
4940

5041
/**
@@ -61,7 +52,6 @@ protected function cacheSetupClosure(): void
6152
$this->hashedFormId, // Use the hashed ID that matches what Dataform component generates
6253
$this->controller,
6354
$this->setup,
64-
$this->name,
6555
$tempCrud,
6656
$this->entry
6757
);
@@ -77,9 +67,14 @@ protected function cacheSetupClosure(): void
7767
public function render()
7868
{
7969
$this->hashedFormId = $this->id.md5($this->formAction.$this->formOperation.'post'.$this->controller);
70+
71+
if (empty($this->formUrl)) {
72+
$this->formUrl = isset($this->entry) ? url($this->crud->route.'/'.$this->entry->getKey().'/edit') : url($this->crud->route.'/create');
73+
}
8074

81-
if ($this->entry) {
82-
$this->formUrl = $this->formUrl ?? url($this->route.'/'.$this->entry->getKey().'/edit');
75+
if (empty($this->formAction)) {
76+
$this->formAction = isset($this->entry) ? url($this->crud->route.'/'.$this->entry->getKey()) : url($this->crud->route);
77+
$this->formMethod = $this->formMethod ?? (isset($this->entry) ? 'put' : 'post');
8378
}
8479

8580
// Cache the setup closure if provided (for retrieval during AJAX request)
@@ -100,7 +95,6 @@ public function render()
10095
'classes' => $this->classes,
10196
'hashedFormId' => $this->hashedFormId,
10297
'controller' => $this->controller,
103-
'route' => $this->route, // Pass the route for building URLs in the template
10498
]);
10599
}
106100
}

src/resources/views/crud/buttons/create_in_modal.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@if ($crud->hasAccess('create'))
22
@php
3-
$controllerClass = get_class(app('request')->route()->getController());
3+
$controllerClass = $crud->controller ??get_class(app('request')->route()->getController());
44
@endphp
55

66
{{-- Create button that opens modal form --}}

src/resources/views/crud/buttons/update_in_modal.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@if ($crud->hasAccess('update', $entry))
22
@php
3-
$controllerClass = get_class(app('request')->route()->getController());
3+
$controllerClass = $crud->controller ?? get_class(app('request')->route()->getController());
44
@endphp
55

66
{{-- Update button that opens modal form --}}

src/resources/views/crud/components/dataform/form.blade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<form method="post"
55
action="{{ $formAction }}"
66
id="{{ $id }}"
7-
@if(!empty($name)) name="{{ $name }}" @endif
87
@if($hasUploadFields) enctype="multipart/form-data" @endif
98
>
109
{!! csrf_field() !!}

0 commit comments

Comments
 (0)