Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle hooks for CrudPanel functionality #5687

Merged
merged 14 commits into from
Feb 7, 2025
Next Next commit
wip
  • Loading branch information
pxpm committed Feb 5, 2025
commit 6a54117ee82d173856bf3f9c318eed616e60388c
13 changes: 12 additions & 1 deletion src/app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Backpack\CRUD\app\Http\Controllers;

use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime;
use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks;
use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -116,13 +118,22 @@ protected function setupConfigurationForCurrentOperation()
* 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;
*/
$this->crud->applyConfigurationFromSettings($operationName);
if (! BackpackHooks::has(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName)) {
BackpackHooks::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
});
}

$this->crud->loadDefaultOperationSettingsFromConfig(BackpackHooks::run(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, [$this]));

BackpackHooks::run(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, [$this]);
/*
* THEN, run the corresponding setupXxxOperation if it exists.
*/
if (method_exists($this, $setupClassName)) {
$this->{$setupClassName}();
}

BackpackHooks::run(OperationHooks::AFTER_OPERATION_SETUP, $operationName, [$this]);
}
}
23 changes: 9 additions & 14 deletions src/app/Http/Controllers/Operations/Concerns/HasForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Backpack\CRUD\app\Http\Controllers\Operations\Concerns;

use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks;
use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -38,19 +40,13 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li
// Access
$this->crud->allowAccess($operationName);

// Config
$this->crud->operation($operationName, 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
BackpackHooks::register(OperationHooks::SETUP_OPERATION_FROM_CONFIG, $operationName, function () use ($operationName) {
return config()->has('backpack.operations.'.$operationName) ? 'backpack.operations.'.$operationName : 'backpack.operations.form';
});

BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operationName, function () use ($operationName) {
$this->crud->addSaveAction([
'name' => 'save_and_back',
'name' => 'save_and_back',
'visible' => function ($crud) use ($operationName) {
return $crud->hasAccess($operationName);
},
Expand All @@ -61,8 +57,7 @@ protected function formDefaults(string $operationName, string $buttonStack = 'li
]);
});

// Default Button
$this->crud->operation(['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) {
BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, ['list', 'show'], function () use ($operationName, $buttonStack, $buttonMeta) {
$this->crud->button($operationName)->view('crud::buttons.quick')->stack($buttonStack)->meta($buttonMeta);
});
}
Expand Down
7 changes: 4 additions & 3 deletions src/app/Http/Controllers/Operations/CreateOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Backpack\CRUD\app\Http\Controllers\Operations;

use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks;
use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks;
use Illuminate\Support\Facades\Route;

trait CreateOperation
Expand Down Expand Up @@ -35,12 +37,11 @@ protected function setupCreateDefaults()
{
$this->crud->allowAccess('create');

$this->crud->operation('create', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, 'create', function () {
$this->crud->setupDefaultSaveActions();
});

$this->crud->operation('list', function () {
BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, 'list', function () {
$this->crud->addButton('top', 'create', 'view', 'crud::buttons.create');
});
}
Expand Down
29 changes: 29 additions & 0 deletions src/app/Library/CrudPanel/Hooks/BackpackHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Backpack\CRUD\app\Library\CrudPanel\Hooks;

final class BackpackHooks
{
public array $hooks = [];

public static function register(string $hook, string|array $operations, callable $callback): void
{
$operations = is_array($operations) ? $operations : [$operations];

foreach ($operations as $operation) {
self::$hooks[$operation][$hook] = $callback;
}
}

public static function run(string $hook, string $operation, array $parameters): void
{
if (isset(self::$operationHooks[$operation][$hook])) {
self::$hooks[$operation][$hook](...$parameters);
}
}

public static function has(string $hook, string $operation): bool
{
return isset(self::$hooks[$operation][$hook]);
}
}
24 changes: 24 additions & 0 deletions src/app/Library/CrudPanel/Hooks/OperationHooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Backpack\CRUD\app\Library\CrudPanel\Hooks;

final class OperationHooks
{
const BEFORE_OPERATION_SETUP_ROUTES = 'beforeOperationSetupRoutes';

const AFTER_OPERATION_SETUP_ROUTES = 'afterOperationSetupRoutes';

const BEFORE_OPERATION_SETUP_DEFAULTS = 'beforeOperationSetupDefaults';

const AFTER_OPERATION_SETUP_DEFAULTS = 'afterOperationSetupDefaults';

const BEFORE_CONTROLLER_SETUP = 'beforeControllerSetup';

const AFTER_CONTROLLER_SETUP = 'afterControllerSetup';

const BEFORE_OPERATION_SETUP = 'beforeOperationSetup';

const AFTER_OPERATION_SETUP = 'afterOperationSetup';

const SETUP_OPERATION_FROM_CONFIG = 'setupOperationFromConfig';
}
10 changes: 8 additions & 2 deletions src/app/Library/CrudPanel/Traits/Operations.php
tabacitu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Backpack\CRUD\app\Library\CrudPanel\Traits;

use Backpack\CRUD\app\Library\CrudPanel\Hooks\BackpackHooks;
use Backpack\CRUD\app\Library\CrudPanel\Hooks\OperationHooks;

trait Operations
{
/*
Expand Down Expand Up @@ -59,20 +62,22 @@ public function setCurrentOperation($operation_name)
* @param string|array $operation Operation name in string form
* @param bool|\Closure $closure Code that calls CrudPanel methods.
* @return void
* @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead
*/
public function operation($operations, $closure = false)
{
return $this->configureOperation($operations, $closure);
BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operations, $closure);
}

/**
* Store a closure which configures a certain operation inside settings.
* Allc configurations are put inside that operation's namespace.
* All configurations are put inside that operation's namespace.
* Ex: show.configuration.
*
* @param string|array $operation Operation name in string form
* @param bool|\Closure $closure Code that calls CrudPanel methods.
* @return void
* @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead
*/
public function configureOperation($operations, $closure = false)
{
Expand All @@ -93,6 +98,7 @@ public function configureOperation($operations, $closure = false)
*
* @param string|array $operations [description]
* @return void
* @deprecated use BackpackHooks::register(OperationHooks::BEFORE_OPERATION_SETUP, $operation, $closure) instead
*/
public function applyConfigurationFromSettings($operations)
{
Expand Down