Skip to content

Commit

Permalink
Add backend.list.extendColumnsBefore event (#998)
Browse files Browse the repository at this point in the history
Add missing event fired before the list columns are created.
  • Loading branch information
mjauvin authored Oct 26, 2023
1 parent 05000d2 commit 80ef162
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
13 changes: 13 additions & 0 deletions modules/backend/behaviors/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public function makeList($definition = null)
*/
$widget = $this->makeWidget(\Backend\Widgets\Lists::class, $columnConfig);

$widget->bindEvent('list.extendColumnsBefore', function () use ($widget) {
$this->controller->listExtendColumnsBefore($widget);
});

$widget->bindEvent('list.extendColumns', function () use ($widget) {
$this->controller->listExtendColumns($widget);
});
Expand Down Expand Up @@ -468,6 +472,15 @@ public function listGetConfig(string $definition = null)
// Overrides
//

/**
* Called before the list columns are defined.
* @param \Backend\Widgets\Lists $host The hosting list widget
* @return void
*/
public function listExtendColumnsBefore($host)
{
}

/**
* Called after the list columns are defined.
* @param \Backend\Widgets\Lists $host The hosting list widget
Expand Down
51 changes: 51 additions & 0 deletions modules/backend/widgets/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,57 @@ protected function defineListColumns()
throw new ApplicationException(Lang::get('backend::lang.list.missing_columns', compact('class')));
}

/**
* @event backend.list.extendColumnsBefore
* Provides an opportunity to modify the columns of a List widget before the columns are created.
*
* Example usage:
*
* Event::listen('backend.list.extendColumnsBefore', function ($listWidget) {
* // Only for the User controller
* if (!$listWidget->getController() instanceof \Backend\Controllers\Users) {
* return;
* }
*
* // Only for the User model
* if (!$listWidget->model instanceof \Backend\Models\User) {
* return;
* }
*
* // Add a column in first position
* $listWidget->columns = array_merge([
* 'myColumn' => [
* 'type' => 'text',
* 'label' => 'My Column',
* ],
* ], $listWidget->columns);
* });
*
* Or
*
* $listWidget->bindEvent('list.extendColumnsBefore', function () use ($listWidget) {
* // Only for the User controller
* if (!$listWidget->getController() instanceof \Backend\Controllers\Users) {
* return;
* }
*
* // Only for the User model
* if (!$listWidget->model instanceof \Backend\Models\User) {
* return;
* }
*
* // Add a column in first position
* $listWidget->columns = array_merge([
* 'myColumn' => [
* 'type' => 'text',
* 'label' => 'My Column',
* ],
* ], $listWidget->columns);
* });
*
*/
$this->fireSystemEvent('backend.list.extendColumnsBefore');

$this->addColumns($this->columns);

/**
Expand Down

0 comments on commit 80ef162

Please sign in to comment.