Skip to content

Commit df6adc6

Browse files
committed
wip
1 parent f6fb8c9 commit df6adc6

File tree

10 files changed

+525
-12
lines changed

10 files changed

+525
-12
lines changed

src/CrudPanelManager.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ public function setupCrudPanel(string $controller, ?string $operation = null): C
6969
$crud->setOperation($operation);
7070

7171
$primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest();
72-
if (! $crud->isInitialized()) {
72+
if (! $crud->isInitialized() || ! $this->isOperationInitialized($controller::class, $operation)) {
7373
self::setActiveController($controller::class);
74+
$crud->initialized = false;
7475
$controller->initializeCrudPanel($primaryControllerRequest, $crud);
7576
self::unsetActiveController();
7677
$crud = $this->cruds[$controller::class];
@@ -106,6 +107,14 @@ public function getInitializedOperations(string $controller): array
106107
return $this->initializedOperations[$controller] ?? [];
107108
}
108109

110+
/**
111+
* Check if a specific operation has been initialized for a controller.
112+
*/
113+
public function isOperationInitialized(string $controller, string $operation): bool
114+
{
115+
return in_array($operation, $this->getInitializedOperations($controller), true);
116+
}
117+
109118
/**
110119
* Store a CrudPanel instance for a specific controller.
111120
*/

src/app/Http/Controllers/Operations/CreateOperation.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ protected function setupCreateRoutes($segment, $routeName, $controller)
2727
'uses' => $controller.'@store',
2828
'operation' => 'create',
2929
]);
30+
31+
Route::get($segment.'/create-form', [
32+
'as' => $routeName.'.create-form',
33+
'uses' => $controller.'@createForm',
34+
'operation' => 'create',
35+
]);
3036
}
3137

3238
/**
@@ -63,6 +69,30 @@ public function create()
6369
return view($this->crud->getCreateView(), $this->data);
6470
}
6571

72+
public function createForm()
73+
{
74+
$this->crud->hasAccessOrFail('create');
75+
76+
// if the request isn't an AJAX request, return a 404
77+
if (! request()->ajax()) {
78+
abort(404);
79+
}
80+
81+
return view(
82+
$this->crud->getFirstFieldView('form.create_form'),
83+
[
84+
'fields' => $this->crud->getCreateFields(),
85+
'action' => 'create',
86+
'crud' => $this->crud,
87+
'modalClass' => request()->get('modal_class'),
88+
'parentLoadedAssets' => request()->get('parent_loaded_assets'),
89+
]
90+
);
91+
92+
}
93+
94+
95+
6696
/**
6797
* Store a newly created resource in the database.
6898
*

src/app/View/Components/Form.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\View\Components;
4+
5+
use Backpack\CRUD\CrudManager;
6+
use Illuminate\View\Component;
7+
8+
class Form extends Component
9+
{
10+
public $crud;
11+
12+
/**
13+
* Create a new component instance.
14+
*
15+
* @param string $controller The CRUD controller class name
16+
* @param string $operation The operation to use (create, update, etc.)
17+
* @param string|null $action Custom form action URL
18+
* @param string $method Form method (post, put, etc.)
19+
*/
20+
public function __construct(
21+
public string $controller,
22+
public string $operation = 'create',
23+
public ?string $formAction = null,
24+
public string $formMethod = 'post'
25+
) {
26+
// Get CRUD panel instance from the controller
27+
$this->crud = CrudManager::setupCrudPanel($controller, $operation);
28+
$this->operation = $operation;
29+
$this->formAction = $action ?? url($this->crud->route);
30+
}
31+
32+
/**
33+
* Get the view / contents that represent the component.
34+
*
35+
* @return \Illuminate\Contracts\View\View|\Closure|string
36+
*/
37+
public function render()
38+
{
39+
return view('crud::components.form.form', [
40+
'crud' => $this->crud,
41+
'operation' => $this->operation,
42+
'formAction' => $this->formAction,
43+
'formMethod' => $this->formMethod,
44+
]);
45+
}
46+
}

src/app/View/Components/FormModal.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\View\Components;
4+
5+
class FormModal extends Form
6+
{
7+
/**
8+
* Create a new component instance.
9+
*
10+
* @param string $controller The CRUD controller class name
11+
* @param string $operation The operation to use (create, update, etc.)
12+
* @param string|null $action Custom form action URL
13+
* @param string $method Form method (post, put, etc.)
14+
* @param string $buttonText Text to display on the button that opens the modal
15+
* @param string $modalTitle Title for the modal
16+
* @param string $buttonClass CSS classes for the button
17+
*/
18+
public function __construct(
19+
public string $controller,
20+
public string $operation = 'create',
21+
public ?string $action = null,
22+
public string $method = 'post',
23+
public string $buttonText = 'Open Form',
24+
public string $modalTitle = 'Form',
25+
public string $buttonClass = 'btn btn-primary'
26+
) {
27+
parent::__construct($controller, $operation, $action, $method);
28+
}
29+
30+
/**
31+
* Get the view / contents that represent the component.
32+
*
33+
* @return \Illuminate\Contracts\View\View|\Closure|string
34+
*/
35+
public function render()
36+
{
37+
return view('crud::components.form.modal_form', [
38+
'crud' => $this->crud,
39+
'operation' => $this->operation,
40+
'formAction' => $this->formAction,
41+
'formMethod' => $this->formMethod,
42+
'buttonText' => $this->buttonText,
43+
'modalTitle' => $this->modalTitle,
44+
'buttonClass' => $this->buttonClass,
45+
]);
46+
}
47+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<div class="card">
2+
<div class="card-header">
3+
<h3 class="card-title">{!! $crud->getSubheading() ?? trans('backpack::crud.add').' '.$crud->entity_name !!}</h3>
4+
</div>
5+
<div class="card-body">
6+
<div class="backpack-form">
7+
@include('crud::inc.grouped_errors')
8+
9+
<form method="{{ $formMethod }}"
10+
action="{{ $formAction }}"
11+
@if ($crud->hasUploadFields($operation))
12+
enctype="multipart/form-data"
13+
@endif
14+
>
15+
{!! csrf_field() !!}
16+
@if($formMethod !== 'post')
17+
@method($formMethod)
18+
@endif
19+
20+
{{-- Include the form fields --}}
21+
@include('crud::form_content', ['fields' => $crud->fields(), 'action' => $operation])
22+
23+
{{-- This makes sure that all field assets are loaded. --}}
24+
<div class="d-none" id="parentLoadedAssets">{{ json_encode(Basset::loaded()) }}</div>
25+
26+
{{-- Include form save buttons --}}
27+
@if(!isset($hideButtons) || !$hideButtons)
28+
<div class="form-group mt-3">
29+
<button type="submit" class="btn btn-success">
30+
<span class="la la-save" role="presentation" aria-hidden="true"></span> &nbsp;
31+
<span>{{ trans('backpack::crud.save') }}</span>
32+
</button>
33+
<a href="{{ url()->previous() }}" class="btn btn-secondary">{{ trans('backpack::crud.cancel') }}</a>
34+
</div>
35+
@endif
36+
</form>
37+
</div>
38+
</div>
39+
</div>
40+
41+
@push('after_scripts')
42+
<script>
43+
document.addEventListener('DOMContentLoaded', function() {
44+
console.log('Backpack Form Loaded');
45+
// Initialize the form fields after loading
46+
if (typeof initializeFieldsWithJavascript === 'function') {
47+
try {
48+
initializeFieldsWithJavascript(document.querySelector('.backpack-form'));
49+
} catch (e) {
50+
console.error('Error initializing form fields:', e);
51+
}
52+
}
53+
54+
// Focus on first focusable field when form is loaded
55+
const form = document.querySelector('.backpack-form form');
56+
if (form) {
57+
const firstField = form.querySelector('input:not([type=hidden]), select, textarea');
58+
if (firstField) {
59+
firstField.focus();
60+
}
61+
}
62+
});
63+
</script>
64+
@endpush

0 commit comments

Comments
 (0)