From ddbe4c7a668481fc3d8eaa9dbc7fd31e8ec91bb9 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 17 May 2024 09:54:29 -0400 Subject: [PATCH 1/5] add extension events for the main relation widgets --- modules/backend/behaviors/RelationController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 3f8346df4e..2e7300d62a 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -397,6 +397,7 @@ public function initRelation($model, $field = null) */ if ($this->viewWidget = $this->makeViewWidget()) { $this->controller->relationExtendViewWidget($this->viewWidget, $this->field, $this->model); + $this->controller->fireEvent('relation.extendViewWidget', [$this->viewWidget, $this->field, $this->model]); $this->viewWidget->bindToController(); } @@ -405,6 +406,7 @@ public function initRelation($model, $field = null) */ if ($this->manageWidget = $this->makeManageWidget()) { $this->controller->relationExtendManageWidget($this->manageWidget, $this->field, $this->model); + $this->controller->fireEvent('relation.extendManageWidget', [$this->manageWidget, $this->field, $this->model]); $this->manageWidget->bindToController(); } @@ -413,6 +415,7 @@ public function initRelation($model, $field = null) */ if ($this->manageMode === 'pivot' && $this->pivotWidget = $this->makePivotWidget()) { $this->controller->relationExtendPivotWidget($this->pivotWidget, $this->field, $this->model); + $this->controller->fireEvent('relation.extendPivotWidget', [$this->pivotWidget, $this->field, $this->model]); $this->pivotWidget->bindToController(); } } From 89c44b0daad0dff7d9e372798ac379e1f28a6511 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 17 May 2024 10:08:36 -0400 Subject: [PATCH 2/5] make the new event as flexible as possible --- modules/backend/behaviors/RelationController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 2e7300d62a..fbbe36568b 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -396,8 +396,8 @@ public function initRelation($model, $field = null) * View widget */ if ($this->viewWidget = $this->makeViewWidget()) { - $this->controller->relationExtendViewWidget($this->viewWidget, $this->field, $this->model); - $this->controller->fireEvent('relation.extendViewWidget', [$this->viewWidget, $this->field, $this->model]); + $this->controller->bindEvent('relation.extendViewWidget', [$this, 'relationExtendViewWidget']); + $this->controller->fireEvent('relation.extendViewWidget', [$this->viewWidget, $this->field, $this->model], halt:true); $this->viewWidget->bindToController(); } @@ -405,8 +405,8 @@ public function initRelation($model, $field = null) * Manage widget */ if ($this->manageWidget = $this->makeManageWidget()) { - $this->controller->relationExtendManageWidget($this->manageWidget, $this->field, $this->model); - $this->controller->fireEvent('relation.extendManageWidget', [$this->manageWidget, $this->field, $this->model]); + $this->controller->bindEvent('relation.extendManageWidget', [$this, 'relationExtendManageWidget']); + $this->controller->fireEvent('relation.extendManageWidget', [$this->manageWidget, $this->field, $this->model], halt:true); $this->manageWidget->bindToController(); } @@ -414,8 +414,8 @@ public function initRelation($model, $field = null) * Pivot widget */ if ($this->manageMode === 'pivot' && $this->pivotWidget = $this->makePivotWidget()) { - $this->controller->relationExtendPivotWidget($this->pivotWidget, $this->field, $this->model); - $this->controller->fireEvent('relation.extendPivotWidget', [$this->pivotWidget, $this->field, $this->model]); + $this->controller->bindEvent('relation.extendPivotWidget', [$this, 'relationExtendPivotWidget']); + $this->controller->fireEvent('relation.extendPivotWidget', [$this->pivotWidget, $this->field, $this->model], halt:true); $this->pivotWidget->bindToController(); } } From 94d8b10ac1ae3c258fb04e68d0c537f285b17bda Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 17 May 2024 10:16:07 -0400 Subject: [PATCH 3/5] handler must be called on the controller --- modules/backend/behaviors/RelationController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index fbbe36568b..a39370202c 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -396,7 +396,7 @@ public function initRelation($model, $field = null) * View widget */ if ($this->viewWidget = $this->makeViewWidget()) { - $this->controller->bindEvent('relation.extendViewWidget', [$this, 'relationExtendViewWidget']); + $this->controller->bindEvent('relation.extendViewWidget', [$this->controller, 'relationExtendViewWidget']); $this->controller->fireEvent('relation.extendViewWidget', [$this->viewWidget, $this->field, $this->model], halt:true); $this->viewWidget->bindToController(); } @@ -405,7 +405,7 @@ public function initRelation($model, $field = null) * Manage widget */ if ($this->manageWidget = $this->makeManageWidget()) { - $this->controller->bindEvent('relation.extendManageWidget', [$this, 'relationExtendManageWidget']); + $this->controller->bindEvent('relation.extendManageWidget', [$this->controller, 'relationExtendManageWidget']); $this->controller->fireEvent('relation.extendManageWidget', [$this->manageWidget, $this->field, $this->model], halt:true); $this->manageWidget->bindToController(); } @@ -414,7 +414,7 @@ public function initRelation($model, $field = null) * Pivot widget */ if ($this->manageMode === 'pivot' && $this->pivotWidget = $this->makePivotWidget()) { - $this->controller->bindEvent('relation.extendPivotWidget', [$this, 'relationExtendPivotWidget']); + $this->controller->bindEvent('relation.extendPivotWidget', [$this->controller, 'relationExtendPivotWidget']); $this->controller->fireEvent('relation.extendPivotWidget', [$this->pivotWidget, $this->field, $this->model], halt:true); $this->pivotWidget->bindToController(); } From 192f01ff1b42f3231998b3ed04640479b1943f97 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 17 May 2024 11:04:49 -0400 Subject: [PATCH 4/5] add events docblocks --- .../backend/behaviors/RelationController.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index a39370202c..7cd74a475b 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -394,6 +394,19 @@ public function initRelation($model, $field = null) /* * View widget + * + * @event relation.extendViewWidget + * Called when the relation viewWidget is created + * + * Example usage: + * + * $controller->bindEvent('relation.extendViewWidget', function ($widget, $field, $model) { + * if ($field === 'myRelationField') { + * $widget->addColumns([ + * 'myNewColumn' => ['label' => 'My New Column'], + * ]); + * } + * }); */ if ($this->viewWidget = $this->makeViewWidget()) { $this->controller->bindEvent('relation.extendViewWidget', [$this->controller, 'relationExtendViewWidget']); @@ -403,6 +416,21 @@ public function initRelation($model, $field = null) /* * Manage widget + * + * @event relation.extendManageWidget + * Called when the relation manageWidget is created + * + * Example usage: + * + * $controller->bindEvent('relation.extendManageWidget', function ($widget, $field, $model) { + * if ($field === 'myRelationField') { + * $widget->model->bindEvent('model.form.filterFields', function ($widget, $fields, $context) { + * if (isset($fields->myFormField)) { + * $fields->myFormField->hidden = true; + * } + * }); + * } + * }); */ if ($this->manageWidget = $this->makeManageWidget()) { $this->controller->bindEvent('relation.extendManageWidget', [$this->controller, 'relationExtendManageWidget']); @@ -412,6 +440,19 @@ public function initRelation($model, $field = null) /* * Pivot widget + * + * @event relation.extendPivotWidget + * Called when the relation pivotWidget is created + * + * Example usage: + * + * $controller->bindEvent('relation.extendPivotWidget', function ($widget, $field, $model) { + * if ($field === 'myRelationField') { + * $widget->addFields([ + * 'myNewPivotField' => ['label' => 'My New Pivot Field'], + * }); + * } + * }); */ if ($this->manageMode === 'pivot' && $this->pivotWidget = $this->makePivotWidget()) { $this->controller->bindEvent('relation.extendPivotWidget', [$this->controller, 'relationExtendPivotWidget']); From f34b5ebe905e9878a6367b4f98db5f94890b968b Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 17 May 2024 11:31:31 -0400 Subject: [PATCH 5/5] add handler method signatures *without* parenthesis --- .../backend/behaviors/RelationController.php | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 7cd74a475b..08342ee347 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -400,11 +400,13 @@ public function initRelation($model, $field = null) * * Example usage: * - * $controller->bindEvent('relation.extendViewWidget', function ($widget, $field, $model) { + * $controller->bindEvent('relation.extendViewWidget', function (\Backend\Widgets\Lists|\Backend\Widgets\Form $widget, string $field, \Winter\Storm\Database\Model $model) { * if ($field === 'myRelationField') { - * $widget->addColumns([ - * 'myNewColumn' => ['label' => 'My New Column'], - * ]); + * $widget->model->bindEvent('list.extendColumns', function ($widget) { + * $widget->addColumns([ + * 'myNewColumn' => ['label' => 'My New Column'], + * ]); + * }); * } * }); */ @@ -422,7 +424,7 @@ public function initRelation($model, $field = null) * * Example usage: * - * $controller->bindEvent('relation.extendManageWidget', function ($widget, $field, $model) { + * $controller->bindEvent('relation.extendManageWidget', function (\Backend\Widgets\Lists|\Backend\Widgets\Form $widget, string $field, \Winter\Storm\Database\Model $model) { * if ($field === 'myRelationField') { * $widget->model->bindEvent('model.form.filterFields', function ($widget, $fields, $context) { * if (isset($fields->myFormField)) { @@ -446,10 +448,12 @@ public function initRelation($model, $field = null) * * Example usage: * - * $controller->bindEvent('relation.extendPivotWidget', function ($widget, $field, $model) { + * $controller->bindEvent('relation.extendPivotWidget', function (\Backend\Widgets\Form $widget, string $field, \Winter\Storm\Database\Model $model) { * if ($field === 'myRelationField') { - * $widget->addFields([ - * 'myNewPivotField' => ['label' => 'My New Pivot Field'], + * $widget->model->bindEvent('form.extendFields', function ($widget) { + * $widget->addFields([ + * 'myNewPivotField' => ['label' => 'My New Pivot Field'], + * ]); * }); * } * });