diff --git a/src/app/Http/Controllers/CrudController.php b/src/app/Http/Controllers/CrudController.php index c331be9a36..92422c3a34 100644 --- a/src/app/Http/Controllers/CrudController.php +++ b/src/app/Http/Controllers/CrudController.php @@ -117,22 +117,17 @@ protected function setupConfigurationForCurrentOperation() /* * FIRST, run all Operation Closures for this operation. * - * It's preferred for this to closures first, because + * It's preferred for this to run closures first, because * (1) setup() is usually higher in a controller than any other method, so it's more intuitive, * since the first thing you write is the first thing that is being run; * (2) operations use operation closures themselves, inside their setupXxxDefaults(), and * you'd like the defaults to be applied before anything you write. That way, anything you * write is done after the default, so you can remove default settings, etc; */ - if (! LifecycleHook::has($operationName.':setup_operation_config')) { - LifecycleHook::hookInto($operationName.':setup_operation_config', function () use ($operationName) { - return 'backpack.operations.'.$operationName; - }); - } + LifecycleHook::trigger($operationName.':before_setup', [$this]); - $this->crud->loadDefaultOperationSettingsFromConfig(LifecycleHook::trigger($operationName.':setup_operation_config', [$this, $operationName])); + $this->crud->applyConfigurationFromSettings($operationName); - LifecycleHook::trigger($operationName.':before_setup', [$this]); /* * THEN, run the corresponding setupXxxOperation if it exists. */ diff --git a/src/app/Http/Controllers/Operations/Concerns/HasForm.php b/src/app/Http/Controllers/Operations/Concerns/HasForm.php index 5775f4165c..7cf6a4ef94 100644 --- a/src/app/Http/Controllers/Operations/Concerns/HasForm.php +++ b/src/app/Http/Controllers/Operations/Concerns/HasForm.php @@ -39,11 +39,16 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li // Access $this->crud->allowAccess($operationName); - LifecycleHook::hookInto('crud:setup_operation_config', function () use ($operationName) { - return config()->has('backpack.operations.'.$operationName) ? 'backpack.operations.'.$operationName : 'backpack.operations.form'; - }); - - LifecycleHook::hookInto($operationName.':before_setup', function () use ($operationName) { + LifecycleHook::hookInto($operationName.':before_setup', function() use ($operationName) { + // if the backpack.operations.{operationName} config exists, use that one + // otherwise, use the generic backpack.operations.form config + if (config()->has('backpack.operations.'.$operationName)) { + $this->crud->loadDefaultOperationSettingsFromConfig(); + } else { + $this->crud->loadDefaultOperationSettingsFromConfig('backpack.operations.form'); + } + + // add a reasonable "save and back" save action $this->crud->addSaveAction([ 'name' => 'save_and_back', 'visible' => function ($crud) use ($operationName) { diff --git a/src/app/Http/Controllers/Operations/ListOperation.php b/src/app/Http/Controllers/Operations/ListOperation.php index f9ea830507..11b678b95d 100644 --- a/src/app/Http/Controllers/Operations/ListOperation.php +++ b/src/app/Http/Controllers/Operations/ListOperation.php @@ -2,6 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\Route; trait ListOperation @@ -43,7 +44,7 @@ protected function setupListDefaults() { $this->crud->allowAccess('list'); - $this->crud->operation('list', function () { + LifecycleHook::hookInto('list:before_setup', function () { $this->crud->loadDefaultOperationSettingsFromConfig(); }); } diff --git a/src/app/Http/Controllers/Operations/ReorderOperation.php b/src/app/Http/Controllers/Operations/ReorderOperation.php index 0fe547f78a..90fcee0efd 100644 --- a/src/app/Http/Controllers/Operations/ReorderOperation.php +++ b/src/app/Http/Controllers/Operations/ReorderOperation.php @@ -2,6 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\Route; trait ReorderOperation @@ -36,7 +37,7 @@ protected function setupReorderDefaults() $this->crud->set('reorder.enabled', true); $this->crud->allowAccess('reorder'); - $this->crud->operation('reorder', function () { + LifecycleHook::hookInto('reorder:before_setup', function () { $this->crud->loadDefaultOperationSettingsFromConfig(); $this->crud->setOperationSetting('reorderColumnNames', [ 'parent_id' => 'parent_id', @@ -46,7 +47,7 @@ protected function setupReorderDefaults() ]); }); - $this->crud->operation('list', function () { + LifecycleHook::hookInto('list:before_setup', function () { $this->crud->addButton('top', 'reorder', 'view', 'crud::buttons.reorder'); }); } diff --git a/src/app/Http/Controllers/Operations/ShowOperation.php b/src/app/Http/Controllers/Operations/ShowOperation.php index d2eedc72d8..e1ea3ab53b 100644 --- a/src/app/Http/Controllers/Operations/ShowOperation.php +++ b/src/app/Http/Controllers/Operations/ShowOperation.php @@ -2,6 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\Route; trait ShowOperation @@ -30,7 +31,7 @@ protected function setupShowDefaults() $this->crud->allowAccess('show'); $this->crud->setOperationSetting('setFromDb', true); - $this->crud->operation('show', function () { + LifecycleHook::hookInto('show:before_setup', function () { $this->crud->loadDefaultOperationSettingsFromConfig(); if (! method_exists($this, 'setupShowOperation')) { @@ -38,11 +39,11 @@ protected function setupShowDefaults() } }); - $this->crud->operation('list', function () { + LifecycleHook::hookInto(['list:before_setup'], function () { $this->crud->addButton('line', 'show', 'view', 'crud::buttons.show', 'beginning'); }); - $this->crud->operation(['create', 'update'], function () { + LifecycleHook::hookInto(['create:before_setup', 'update:before_setup'], function () { $this->crud->addSaveAction([ 'name' => 'save_and_preview', 'visible' => function ($crud) { diff --git a/src/app/Http/Controllers/Operations/UpdateOperation.php b/src/app/Http/Controllers/Operations/UpdateOperation.php index f3c622a9c5..f0c656092c 100644 --- a/src/app/Http/Controllers/Operations/UpdateOperation.php +++ b/src/app/Http/Controllers/Operations/UpdateOperation.php @@ -2,6 +2,7 @@ namespace Backpack\CRUD\app\Http\Controllers\Operations; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; use Illuminate\Support\Facades\Route; trait UpdateOperation @@ -35,7 +36,7 @@ protected function setupUpdateDefaults() { $this->crud->allowAccess('update'); - $this->crud->operation('update', function () { + LifecycleHook::hookInto('update:before_setup', function () { $this->crud->loadDefaultOperationSettingsFromConfig(); if ($this->crud->getModel()->translationEnabled()) { @@ -49,7 +50,7 @@ protected function setupUpdateDefaults() $this->crud->setupDefaultSaveActions(); }); - $this->crud->operation(['list', 'show'], function () { + LifecycleHook::hookInto(['list:before_setup', 'show:before_setup'], function () { $this->crud->addButton('line', 'update', 'view', 'crud::buttons.update', 'end'); }); } diff --git a/src/app/Library/CrudPanel/CrudRouter.php b/src/app/Library/CrudPanel/CrudRouter.php index 0c1c1f464c..83a490128a 100644 --- a/src/app/Library/CrudPanel/CrudRouter.php +++ b/src/app/Library/CrudPanel/CrudRouter.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\App; use ReflectionClass; +use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; final class CrudRouter { @@ -18,7 +19,9 @@ public static function setupControllerRoutes(string $name, string $routeName, st if (empty($setupRoutesMethod->getAttributes(\Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime::class))) { // when the attribute is not found the developer has overwritten the method // we will keep the old behavior for backwards compatibility + LifecycleHook::trigger('crud:before_setup_routes', [$name, $routeName, $controller]); $setupRoutesMethod->invoke(App::make($namespacedController), $name, $routeName, $controller); + LifecycleHook::trigger('crud:after_setup_routes', [$name, $routeName, $controller]); return; } @@ -32,7 +35,10 @@ public static function setupControllerRoutes(string $name, string $routeName, st str_ends_with($method->getName(), 'Routes') ) { $method->setAccessible(true); + LifecycleHook::trigger('crud:before_setup_routes', [$name, $routeName, $controller]); $method->invoke($controllerInstance, $name, $routeName, $controller); + LifecycleHook::trigger('crud:after_setup_routes', [$name, $routeName, $controller]); + } } } diff --git a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php index 3ee79c2940..f9a100cdfa 100644 --- a/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php +++ b/src/app/Library/CrudPanel/Hooks/LifecycleHooks.php @@ -20,7 +20,9 @@ public function trigger(string|array $hooks, array $parameters): void foreach ($hooks as $hook) { if (isset($this->hooks[$hook])) { foreach ($this->hooks[$hook] as $callback) { - $callback(...$parameters); + if($callback instanceof \Closure) { + $callback(...$parameters); + } } } } diff --git a/src/app/Library/CrudPanel/Traits/Operations.php b/src/app/Library/CrudPanel/Traits/Operations.php index 2786037f26..9362fc3215 100644 --- a/src/app/Library/CrudPanel/Traits/Operations.php +++ b/src/app/Library/CrudPanel/Traits/Operations.php @@ -97,10 +97,9 @@ public function configureOperation($operations, $closure = false) * This is called when an operation does setCurrentOperation(). * * - * @param string|array $operations [description] + * @param string|array $operations * @return void * - * @deprecated use LifecycleHook::hookInto($operation.':before_setup', $closure) instead */ public function applyConfigurationFromSettings($operations) {